-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblizzard.go
132 lines (108 loc) · 3.61 KB
/
blizzard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"errors"
"fmt"
"github.com/tidwall/gjson"
"gopkg.in/resty.v1"
)
// Functions to interact with Blizzard.
type Blizzard interface {
GetToonJson(toon Toon) (string, error)
GetClasses() ([]ToonClass, error)
GetRaces() ([]Race, error)
GetToon(toon *ToonDto) error
}
// Configuration information for interacting with Blizzard.
type BlizzardHttp struct {
ClientId string
ClientSecret string
AccessToken string
}
func NewBlizzard(clientId string, clientSecret string) (*BlizzardHttp, error) {
url := "https://us.battle.net/oauth/token"
resp, err := resty.R().SetBasicAuth(clientId, clientSecret).SetQueryParam("grant_type", "client_credentials").Get(url)
if err != nil {
return nil, err
}
body := resp.String()
if resp.StatusCode() != 200 {
errorDescription := gjson.Get(body, "error_description").String()
return nil, errors.New(fmt.Sprintf("Could not get auth token: %s", errorDescription))
}
accessToken := gjson.Get(body, "access_token").String()
blizzardHttp := &BlizzardHttp{ClientId: clientId, ClientSecret: clientSecret, AccessToken: accessToken}
return blizzardHttp, nil
}
func (blizzard *BlizzardHttp) GetToon(toon *ToonDto) error {
url := fmt.Sprintf("https://%s.api.blizzard.com/wow/character/%s/%s", toon.Region, toon.Realm, toon.Name)
resp, err := resty.R().SetAuthToken(blizzard.AccessToken).SetHeader("Accept", "application/json").Get(url)
if err != nil {
return err
}
if resp.StatusCode() != 200 {
return errors.New("could not find character, status code not 200")
}
body := resp.String()
toon.ClassID = gjson.Get(body, "class").Int()
toon.RaceID = gjson.Get(body, "race").Int()
toon.Gender = gjson.Get(body, "gender").Int()
toon.Name = gjson.Get(body, "name").String()
return nil
}
func (blizzard *BlizzardHttp) GetClasses() ([]ToonClass, error) {
resp, err := resty.R().SetAuthToken(blizzard.AccessToken).SetHeader("Accept", "application/json").Get("https://us.api.blizzard.com/wow/data/character/classes")
if err != nil {
return nil, err
}
respJson := resp.String()
result := gjson.Get(respJson, "classes")
var classes []ToonClass
for _, r := range result.Array() {
id := gjson.Get(r.String(), "id").Int()
mask := gjson.Get(r.String(), "mask").Int()
powerType := gjson.Get(r.String(), "powerType").String()
name := gjson.Get(r.String(), "name").String()
tmpClass := ToonClass{
ID: id,
Mask: mask,
PowerType: powerType,
Name: name,
}
classes = append(classes, tmpClass)
}
return classes, nil
}
func (blizzard *BlizzardHttp) GetToonJson(toon Toon) (string, error) {
url := fmt.Sprintf("https://%s.api.blizzard.com/wow/character/%s/%s", toon.Region, toon.Realm, toon.Name)
resp, err := resty.R().SetQueryParams(map[string]string{
"fields": "statistics,items,pets,mounts",
}).SetAuthToken(blizzard.AccessToken).SetHeader("Accept", "application/json").Get(url)
if err != nil {
return "", err
}
myJson := resp.String()
return myJson, nil
}
func (blizzard *BlizzardHttp) GetRaces() ([]Race, error) {
resp, err := resty.R().SetAuthToken(blizzard.AccessToken).SetHeader("Accept", "application/json").Get("https://us.api.blizzard.com/wow/data/character/races")
if err != nil {
return nil, err
}
respJson := resp.String()
result := gjson.Get(respJson, "races")
var races []Race
for _, r := range result.Array() {
id := gjson.Get(r.String(), "id").Int()
mask := gjson.Get(r.String(), "mask").Int()
side := gjson.Get(r.String(), "side").String()
name := gjson.Get(r.String(), "name").String()
race := Race{
ID: id,
Name: name,
Mask: mask,
Side: side,
}
races = append(races, race)
}
return races, nil
}