-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
177 lines (163 loc) · 5.07 KB
/
client.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package wargaming
import (
"bytes"
"context"
"encoding/json"
"github.com/wows-tools/wows-stats/pester"
"io"
"net/http"
"net/url"
)
// GenericMeta contains all possible values which can be returned as metadata.
// Explicitly check against non nil, before using them.
// The returned meta values are not documented by Wargaming.
type GenericMeta struct {
Count *int `json:"count,omitempty"`
Hidden []int `json:"hidden,omitempty"`
Limit *int `json:"limit,omitempty"`
Page *int `json:"page,omitempty"`
PageTotal *int `json:"page_total,omitempty"`
Total *int `json:"total,omitempty"`
}
// response Wargaming response body fits always in this struct.
type response struct {
Status string `json:"status"`
Error *ResponseError `json:"error,omitempty"`
Data any `json:"data,omitempty"`
Meta any `json:"meta,omitempty"`
}
// Client for all api requests.
// As a quick start:
//
// // create client
// client := wargaming.NewClient("a7f838650dcb008552966db063eeeb35", nil)
// // get account list
// res, meta, err := client.Wot.AccountList(context.Background(), wargaming.realmEu, "Yzne", nil)
// // print out
// if err != nil {
// // handle a Wargaming Response Error, returned by the API itself
// var respErr *wargaming.ResponseError
// if errors.As(err, &respErr) {
// fmt.Println(respErr.Error())
// } else {
// // handle client or other error
// fmt.Println(err.Error())
// }
// } else {
// for _, value := range res {
// fmt.Println(value)
// }
// fmt.Println(*meta.Count)
// }
// // get account list with options
// res, _, err = client.Wot.AccountList(context.Background(), wargaming.realmEu, "Lichtgeschwindigkeit", &wot.AccountListOptions{
// Fields: []string{"nickname"},
// })
//
// or with a custom http.Client
//
// client := wargaming.NewClient("a7f838650dcb008552966db063eeeb35", &wargaming.ClientOptions{
// HTTPClient: &http.Client{
// Timeout: 10 * time.Second,
// },
// })
type Client struct {
wgServices
httpClient *pester.Client
// Wargaming.net application ID.
applicationId string
// Reuse a single struct instead of allocating one for each service on the heap.
common service
}
type ClientOptions struct {
HTTPClient *pester.Client
}
// NewClient creates a new API client.
// Pass nil as options if you want to use none.
func NewClient(applicationId string, options *ClientOptions) *Client {
client := &Client{
applicationId: applicationId,
}
client.httpClient = options.HTTPClient
client.common.client = client
client.wgServices = newWgServices(&client.common)
return client
}
func (client *Client) buildRequest(ctx context.Context, method string, section section, realm Realm, path string, data map[string]string) (req *http.Request) {
query := url.Values{}
query.Add("application_id", client.applicationId)
for key, value := range data {
query.Add(key, value)
}
if method == http.MethodPost {
req, _ = http.NewRequestWithContext(ctx, method, section.ApiUrl(realm)+path, bytes.NewBuffer([]byte(query.Encode())))
} else {
req, _ = http.NewRequestWithContext(ctx, method, section.ApiUrl(realm)+path, nil)
req.URL.RawQuery = query.Encode()
}
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
return req
}
func (client *Client) Request(req *http.Request, returnData any, metaData any) error {
resp, err := client.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return BadStatusCode(resp.StatusCode)
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
wgResp := &response{
Status: "",
Error: nil,
Data: returnData,
Meta: metaData,
}
err = json.Unmarshal(respBytes, wgResp)
if err != nil {
return err
}
// TEST
// Uncomment this to see what meta values are returned when running the tests.
/*
var metaDataTest map[string]json.RawMessage
wgRespTest := &response{
Status: "",
Error: nil,
Data: nil,
Meta: &metaDataTest,
}
err = json.Unmarshal(respBytes, wgRespTest)
if err != nil {
return err
}
if len(metaDataTest) != 0 {
fmt.Printf("%s contains metadata %v\n", req.URL, metaDataTest)
}*/
// TEST END
if wgResp.Error != nil {
return wgResp.Error
}
if wgResp.Status != "ok" {
return InvalidResponse
}
return nil
}
// postRequest set returnData to nil, if no response data is expected.
// set metaData to nil if no meta data is expected.
func (client *Client) postRequest(ctx context.Context, section section, realm Realm, path string, data map[string]string, returnData any, metaData any) error {
req := client.buildRequest(ctx, "POST", section, realm, path, data)
return client.Request(req, returnData, metaData)
}
// getRequest set returnData to nil, if no response data is expected.
// set metaData to nil if no meta data is expected.
func (client *Client) getRequest(ctx context.Context, section section, realm Realm, path string, data map[string]string, returnData any, metaData any) error {
req := client.buildRequest(ctx, "GET", section, realm, path, data)
return client.Request(req, returnData, metaData)
}