Skip to content

Commit ac0af44

Browse files
authored
Merge pull request #7 from wltu/master
Release v1.0.1
2 parents d988cd6 + 757eff1 commit ac0af44

File tree

11 files changed

+211
-39
lines changed

11 files changed

+211
-39
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ makefile
2121
main/
2222

2323
.vscode/
24+
25+
*.png

README.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ func main() {
3636
fmt.Println(client.ClientToken)
3737

3838
// Search for single card.
39-
client.SearchCard("52119-arch-villain-rafaam")
39+
40+
if card :=
41+
client.SearchCard("52119-arch-villain-rafaam"); card != nil {
42+
fmt.Println(client.CardImage(card, false))
43+
fmt.Println(client.GoldCardImage(card, false))
44+
}
4045

4146
// Search for single card back.
4247
client.SearchCardBack("155-pizza-stone")
@@ -46,9 +51,41 @@ func main() {
4651

4752
// Set optional parameters.
4853
// Visit card_collection.go for more info.
49-
client.SetCardTextFilter("lookout")
50-
51-
client.EndCardCollectionSearch()
54+
client.SetCardTextFilter("hydra")
55+
56+
if cards :=
57+
client.EndCardCollectionSearch(); cards != nil {
58+
fmt.Println(client.CardCollectionImage(cards, false))
59+
fmt.Println(client.GoldCardCollectionImage(cards, false))
60+
}
61+
62+
// Search for single battlegrounds card
63+
if card :=
64+
client.SearchBattlegroundsCard(
65+
"60040-zapp-slywick"); card != nil {
66+
fmt.Println(client.CardImage(card, true))
67+
fmt.Println(client.GoldCardImage(card, true))
68+
}
69+
70+
// Search for set of battlegrouds cards
71+
client.BeginCardCollectionSearch()
72+
client.SetCardGameMode("battlegrounds")
73+
client.SetCardTiers([]int{
74+
6,
75+
})
76+
77+
if cards :=
78+
client.EndCardCollectionSearch(); cards != nil {
79+
fmt.Println(client.CardCollectionImage(cards, true))
80+
fmt.Println(client.GoldCardCollectionImage(cards, true))
81+
}
82+
83+
// Search for a single card back
84+
if cardBack :=
85+
client.SearchCardBack(
86+
"155-pizza-stone"); cardBack != nil {
87+
fmt.Println(client.CardBackImage(cardBack))
88+
}
5289

5390
// Search for a set of card backs
5491
client.BeginCardBackCollectionSearch()
@@ -59,11 +96,16 @@ func main() {
5996

6097
client.SetCardTextFilter("lookout")
6198

62-
client.EndCardBackCollectionSearch()
99+
if cardBacks :=
100+
client.EndCardBackCollectionSearch(); cardBacks != nil {
101+
fmt.Println(client.CardBackCollectionImage(cardBacks))
102+
}
63103

64104
// Search for deck
65105
id := "AAECAQcG+wyd8AKS+AKggAOblAPanQMMS6IE/web8wLR9QKD+wKe+wKz/AL1gAOXlAOalAOSnwMA"
66-
client.SearchDeck(id)
106+
if deck := client.SearchDeck(id); deck != nil {
107+
fmt.Println(client.DeckImage(deck))
108+
}
67109

68110
} else {
69111
fmt.Println("Error in setting up HearthstoneAPI Client!")
@@ -72,7 +114,6 @@ func main() {
72114

73115
```
74116

75-
76117
## Project Structure
77118
This project follows loosely the proejct structure [here](https://github.com/golang-standards/project-layout).
78119

cmd/api/card.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ type Card struct {
4848
Error CardError `json:"error"`
4949
}
5050

51+
// CardImage download card image and return saved location
52+
func (client *HearthstoneAPI) CardImage(card *Card, battlegrounds bool) string {
53+
if battlegrounds {
54+
return getImage(client.heartstoneClient,
55+
card.Slug,
56+
card.Battlegrounds.Image,
57+
false)
58+
}
59+
60+
return getImage(client.heartstoneClient, card.Slug, card.Image, false)
61+
}
62+
63+
// GoldCardImage download golden card image and return saved location
64+
func (client *HearthstoneAPI) GoldCardImage(card *Card, battlegrounds bool) string {
65+
if battlegrounds {
66+
return getImage(client.heartstoneClient,
67+
card.Slug,
68+
card.Battlegrounds.ImageGold,
69+
true)
70+
}
71+
return getImage(client.heartstoneClient, card.Slug, card.ImageGold, true)
72+
}
73+
5174
type cardSearch struct {
5275
// Required Parameters
5376
url string

cmd/api/card_back.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ type CardBack struct {
2020
Slug string `json:"slug"`
2121
}
2222

23+
// CardBackImage download card back image and return saved location
24+
func (client *HearthstoneAPI) CardBackImage(cardBack *CardBack) string {
25+
return getImage(client.heartstoneClient, cardBack.Slug, cardBack.Image, false)
26+
}
27+
2328
func (client *HearthstoneAPI) newCardBackSearch(id string) cardBackSearch {
2429
// Required parameters
2530
return cardBackSearch{
@@ -49,12 +54,12 @@ func (search *cardBackSearch) execute(client *http.Client, token string) interfa
4954
}
5055

5156
// SearchCardBack make a API call to search for a card back with the given id
52-
func (client *HearthstoneAPI) SearchCardBack(id string) CardBack {
57+
func (client *HearthstoneAPI) SearchCardBack(id string) *CardBack {
5358
search := client.newCardBackSearch(id)
5459

5560
if output, ok := client.execute(&search).(CardBack); ok {
56-
return output
61+
return &output
5762
}
5863

59-
return CardBack{}
64+
return nil
6065
}

cmd/api/card_back_collection.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import (
55
"strconv"
66
)
77

8+
// CardBackCollection provide information of a set of Card Backs
9+
type CardBackCollection struct {
10+
CardBacks []CardBack `json:"cardBacks"`
11+
CardCount int `json:"cardCount"`
12+
PageCount int `json:"pageCount"`
13+
Page int `json:"page"`
14+
}
15+
16+
// CardBackCollectionImage download card back images and return saved location
17+
func (client *HearthstoneAPI) CardBackCollectionImage(cardBacks *CardBackCollection) []string {
18+
output := make([]string, len(cardBacks.CardBacks))
19+
for i, cardBack := range cardBacks.CardBacks {
20+
output[i] = getImage(client.heartstoneClient, cardBack.Slug, cardBack.Image, false)
21+
}
22+
23+
return output
24+
}
25+
826
type cardBackCollectionSearch struct {
927
// Required Parameters
1028
url string
@@ -15,14 +33,6 @@ type cardBackCollectionSearch struct {
1533
optionalInt map[string]int
1634
}
1735

18-
// CardBackCollection provide information of a set of Card Backs
19-
type CardBackCollection struct {
20-
CardBacks []CardBack `json:"cardBacks"`
21-
CardCount int `json:"cardCount"`
22-
PageCount int `json:"pageCount"`
23-
Page int `json:"page"`
24-
}
25-
2636
func (client *HearthstoneAPI) newCardBackCollectionSearch() *cardBackCollectionSearch {
2737
// Required parameters
2838
return &cardBackCollectionSearch{

cmd/api/card_collection.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ type CardCollection struct {
1313
Page int `json:"page"`
1414
}
1515

16+
// CardCollectionImage download card images and return saved location
17+
func (client *HearthstoneAPI) CardCollectionImage(cards *CardCollection, battlegrounds bool) []string {
18+
19+
output := make([]string, len(cards.Cards))
20+
for i, card := range cards.Cards {
21+
image := card.Image
22+
23+
if battlegrounds {
24+
image = card.Battlegrounds.Image
25+
}
26+
output[i] = getImage(client.heartstoneClient, card.Slug, image, false)
27+
}
28+
29+
return output
30+
}
31+
32+
// GoldCardCollectionImage download golden card images and return saved location
33+
func (client *HearthstoneAPI) GoldCardCollectionImage(cards *CardCollection, battlegrounds bool) []string {
34+
output := make([]string, len(cards.Cards))
35+
36+
for i, card := range cards.Cards {
37+
image := card.ImageGold
38+
39+
if battlegrounds {
40+
image = card.Battlegrounds.ImageGold
41+
}
42+
output[i] = getImage(client.heartstoneClient, card.Slug, image, true)
43+
}
44+
45+
return output
46+
}
47+
1648
// cardCollectionSearch provides parameters for a card collection search
1749
type cardCollectionSearch struct {
1850
// Required Parameters

cmd/api/deck.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ type Deck struct {
3939
CardCount int `json:"cardCount"`
4040
}
4141

42+
// DeckImage download all card images in a deck and return saved location
43+
func (client *HearthstoneAPI) DeckImage(deck *Deck) []string {
44+
45+
output := make([]string, len(deck.Cards))
46+
for i, card := range deck.Cards {
47+
output[i] = getImage(client.heartstoneClient, card.Slug, card.Image, false)
48+
}
49+
50+
return output
51+
}
52+
4253
func (client *HearthstoneAPI) newDeckSearch(id string) deckSearch {
4354
// Required parameters
4455
return deckSearch{
@@ -68,12 +79,12 @@ func (search *deckSearch) execute(client *http.Client, token string) interface{}
6879
}
6980

7081
// SearchDeck make a API call to search for a deck with the given id
71-
func (client *HearthstoneAPI) SearchDeck(id string) Deck {
82+
func (client *HearthstoneAPI) SearchDeck(id string) *Deck {
7283
search := client.newDeckSearch(id)
7384

7485
if output, ok := client.execute(&search).(Deck); ok {
75-
return output
86+
return &output
7687
}
7788

78-
return Deck{}
89+
return nil
7990
}

cmd/api/request.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package api
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"log"
89
"net/http"
10+
"os"
911
)
1012

1113
func (client *HearthstoneAPI) authorization(url string, authorization *Authorization) error {
@@ -40,6 +42,8 @@ func post(client *http.Client, url string, target interface{}) error {
4042
return err
4143
}
4244

45+
defer res.Body.Close()
46+
4347
return json.NewDecoder(res.Body).Decode(target)
4448
}
4549

@@ -55,10 +59,50 @@ func get(client *http.Client, url string, target interface{}) error {
5559
if err != nil {
5660
panic(err)
5761
}
62+
defer res.Body.Close()
5863

5964
return json.NewDecoder(res.Body).Decode(target)
6065
}
6166

67+
func getImage(client *http.Client, name, url string, golden bool) string {
68+
if url == "" {
69+
return "Not Available!"
70+
}
71+
72+
request, err := http.NewRequest("GET", url, nil)
73+
if err != nil {
74+
panic(err)
75+
}
76+
77+
res, err := client.Do(request)
78+
79+
if err != nil {
80+
panic(err)
81+
}
82+
83+
defer res.Body.Close()
84+
85+
fileName := "./images/" + name
86+
87+
if golden {
88+
fileName += "-golden.png"
89+
} else {
90+
fileName += ".png"
91+
}
92+
93+
imageFile, err := os.Create(fileName)
94+
95+
if err != nil {
96+
panic(err)
97+
}
98+
99+
defer imageFile.Close()
100+
101+
io.Copy(imageFile, res.Body)
102+
103+
return imageFile.Name()
104+
}
105+
62106
func readBody(res http.Response) {
63107
bodyBytes, err := ioutil.ReadAll(res.Body)
64108
if err != nil {

images/.gitignore

Whitespace-only changes.

internal/test/card_back_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ package hearthstone_go_test
33
import "testing"
44

55
func TestCardBack(t *testing.T) {
6-
cardBack := client.SearchCardBack("155-pizza-stone")
6+
if cardBack := client.SearchCardBack("155-pizza-stone"); cardBack != nil {
77

8-
if cardBack.Name != "Pizza Stone" {
9-
t.Errorf("Card back name should be %s, but got %s!", "Pizza Stone", cardBack.Name)
10-
}
8+
if cardBack.Name != "Pizza Stone" {
9+
t.Errorf("Card back name should be %s, but got %s!", "Pizza Stone", cardBack.Name)
10+
}
1111

12-
if cardBack.ID != 155 {
13-
t.Errorf("Card back ID should be %d, but got %d!", 155, cardBack.ID)
12+
if cardBack.ID != 155 {
13+
t.Errorf("Card back ID should be %d, but got %d!", 155, cardBack.ID)
14+
}
1415
}
1516
}
1617

1718
func TestFakeCardBack(t *testing.T) {
18-
cardBack := client.SearchCardBack("-1")
19+
if cardBack := client.SearchCardBack("-1"); cardBack != nil {
1920

20-
if cardBack.Name != "" {
21-
t.Errorf("Card back name should be %s, but got %s!", "", cardBack.Name)
22-
}
21+
if cardBack.Name != "" {
22+
t.Errorf("Card back name should be %s, but got %s!", "", cardBack.Name)
23+
}
2324

24-
if cardBack.ID != 0 {
25-
t.Errorf("Card back ID should be %d, but got %d!", 0, cardBack.ID)
25+
if cardBack.ID != 0 {
26+
t.Errorf("Card back ID should be %d, but got %d!", 0, cardBack.ID)
27+
}
2628
}
2729
}

0 commit comments

Comments
 (0)