-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_client.go
319 lines (281 loc) · 7.57 KB
/
api_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package gopify
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
defaultApiVersion = "2021-10"
defaultTimeout = 10 * time.Second
defaultRetries = 2
)
var (
ErrRateLimit = errors.New("API rate limit exeeded")
)
// ResponseError represnts any Shopify REST API response error
type ResponseError struct {
Errors any
}
func (err ResponseError) Error() string {
return fmt.Sprintf("%v", err.Errors)
}
// GraphqlErr is a general graphql error
type GraphqlError struct {
msg string
}
func (err GraphqlError) Error() string {
return err.msg
}
type RestResponse struct {
Headers http.Header
Pagination *Pagination
}
// Option is for configuring the Api client
type Option func(c *Client)
// WithVersion sets the Api version
func WithVersion(version string) Option {
return func(c *Client) {
c.version = version
}
}
// WithTimeout sets a custom http timeout
func WithTimeout(seconds int) Option {
return func(c *Client) {
c.client.Timeout = time.Duration(seconds) * time.Second
}
}
// WithRetries tells the Api client how many retries to perform when hit a rate limit
func WithRetry(tries int) Option {
return func(c *Client) {
c.tries = tries
}
}
// Body is an API request/response body
type Body map[string]any
// shopify API client
type Client struct {
client *http.Client
domain string
baseUrl string
accessToken string
version string
tries int
availableLimit int // used for handling rate limits
}
// Create a new shopify Api client
// the domain parameter is the shop domain
func NewClient(domain, accessToken string, opts ...Option) *Client {
client := http.Client{
Timeout: defaultTimeout,
}
c := &Client{
client: &client,
domain: domain,
accessToken: accessToken,
version: defaultApiVersion,
tries: defaultRetries,
availableLimit: 0,
}
for _, opt := range opts {
opt(c)
}
baseUrl := fmt.Sprintf("https://%s/admin/api/%s", domain, c.version)
c.baseUrl = baseUrl
return c
}
func (c *Client) newRequest(method string, path string, queryParams url.Values, requestBody any) (*http.Request, error) {
u, err := url.Parse(fmt.Sprintf("%s/%s", c.baseUrl, path))
if err != nil {
return nil, err
}
if queryParams != nil {
u.RawQuery = queryParams.Encode()
}
b, err := []byte(nil), error(nil)
if requestBody != nil {
b, err = json.Marshal(&requestBody)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(b))
if err != nil {
return nil, err
}
req.Header.Add("X-Shopify-Access-Token", c.accessToken)
req.Header.Add("Content-Type", "application/json")
return req, nil
}
func (c *Client) rest(method string, path string, queryParams url.Values, requestBody any, responseBody any) (*RestResponse, error) {
threshold := 2 // rate limit threshold
req, err := c.newRequest(method, path, queryParams, requestBody)
if err != nil {
return nil, err
}
var res *http.Response
for t := 1; t <= c.tries; t++ {
res, err = c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode == http.StatusTooManyRequests {
retryAfter := res.Header.Get("Retry-After")
if t == c.tries {
return nil, ErrRateLimit
}
r, _ := strconv.Atoi(retryAfter)
time.Sleep(time.Second * time.Duration(r))
continue
}
if res.StatusCode >= http.StatusMultipleChoices {
return nil, parseResponseError(res)
}
limit := res.Header.Get("X-Shopify-Shop-Api-Call-Limit")
if s := strings.Split(limit, "/"); len(s) == 2 {
bucketSize, _ := strconv.Atoi(s[1])
requestCount, _ := strconv.Atoi(s[0])
c.availableLimit = bucketSize - requestCount
}
if c.availableLimit < threshold {
time.Sleep(time.Second * 2)
continue
}
}
if err := json.NewDecoder(res.Body).Decode(&responseBody); err != nil {
return nil, err
}
restResponse := &RestResponse{
Headers: res.Header,
}
return restResponse, nil
}
func parseResponseError(res *http.Response) error {
var responseError ResponseError
if err := json.NewDecoder(res.Body).Decode(&responseError); err != nil {
return err
}
b, err := json.MarshalIndent(responseError.Errors, "", " ")
if err != nil {
return err
}
responseError.Errors = string(b)
return responseError
}
// checks if a graphql response has a rate limit error and return it if exists
func (c *Client) rateLimitError(errors []any) bool {
for _, err := range errors {
if ext, ok := err.(map[string]any); ok {
if e, ok := ext["extensions"].(map[string]any); ok {
code, ok := e["code"].(string)
if ok && (code == "MAX_COST_EXCEEDED" || code == "THROTTLED") {
return true
}
}
}
}
return false
}
// get the current available rate limit from the graphql response
func (c *Client) graphqlAvailableLimit(extentions map[string]any) int {
if cost, ok := extentions["cost"].(map[string]any); ok {
if throttleStatus, ok := cost["throttleStatus"].(map[string]any); ok {
if currentlyAvailable, ok := throttleStatus["currentlyAvailable"].(float64); ok {
return int(currentlyAvailable)
}
}
}
return -1
}
func (c *Client) graphql(body Body) (Body, error) {
threshold := 50
req, err := c.newRequest(http.MethodPost, "graphql.json", nil, body)
if err != nil {
return nil, err
}
for t := 1; t <= c.tries; t++ {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected server response: %s", res.Status)
}
result := make(map[string]any)
dec := json.NewDecoder(res.Body)
if err := dec.Decode(&result); err != nil {
return nil, err
}
// handle errors
if e, ok := result["errors"]; ok {
errs := e.([]any)
// rate limit error
if c.rateLimitError(errs) {
if t == c.tries {
return nil, ErrRateLimit
}
time.Sleep(2 * time.Second)
continue
} else {
if errMessage, ok := errs[0].(map[string]any)["message"].(string); ok {
return nil, GraphqlError{msg: errMessage}
}
}
}
// check available rate limit
if e, ok := result["extensions"]; ok {
l := c.graphqlAvailableLimit(e.(map[string]any))
if l != -1 {
c.availableLimit = l
}
}
if c.availableLimit < threshold {
time.Sleep(2 * time.Second)
continue
}
return result["data"].(map[string]any), nil
}
return nil, nil
}
// Get performs a get request and returns the result
func (c *Client) Get(path string, queryParams url.Values, responseBody any) (*RestResponse, error) {
r, err := c.rest(http.MethodGet, path, queryParams, nil, responseBody)
if err != nil {
return nil, err
}
pagination, err := extractPagination(r.Headers.Get("Link"))
if err != nil {
return nil, err
}
r.Pagination = pagination
return r, nil
}
// post performs a post request and returns the result
func (c *Client) Post(path string, requestBody any, responseBody any) (*RestResponse, error) {
return c.rest(http.MethodPost, path, nil, requestBody, requestBody)
}
// Put performs a put request and returns the result
func (c *Client) Put(path string, requestBody any, responseBody any) (*RestResponse, error) {
return c.rest(http.MethodPut, path, nil, requestBody, responseBody)
}
// Delete performs a delete request
func (c *Client) Delete(path string) (*RestResponse, error) {
return c.rest(http.MethodDelete, path, nil, nil, nil)
}
// Graphql sends a graphql query to shopify admin api.
func (c *Client) Graphql(query string, variables map[string]any) (Body, error) {
body := map[string]any{
"query": query,
}
if variables != nil {
body["variables"] = variables
}
return c.graphql(body)
}