-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
601 lines (485 loc) · 12.9 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package toast
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/http/httputil"
"net/textproto"
"net/url"
"path"
"strconv"
"strings"
"text/template"
"github.com/omniboost/go-toast/utils"
"github.com/pkg/errors"
)
const (
libraryVersion = "0.0.1"
userAgent = "go-toast/" + libraryVersion
mediaType = "application/json"
charset = "utf-8"
)
var (
BaseURL = url.URL{
Scheme: "https",
Host: "ws-api.toasttab.com",
Path: "",
}
)
// NewClient returns a new Exact Globe Client client
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
client := &Client{}
client.SetHTTPClient(httpClient)
client.SetBaseURL(BaseURL)
client.SetDebug(false)
client.SetUserAgent(userAgent)
client.SetMediaType(mediaType)
client.SetCharset(charset)
return client
}
// Client manages communication with Exact Globe Client
type Client struct {
// HTTP client used to communicate with the Client.
http *http.Client
debug bool
baseURL url.URL
// credentials
clientID string
clientSecret string
toastRestaurantExternalID string
token Token
// User agent for client
userAgent string
mediaType string
charset string
disallowUnknownFields bool
// Optional function called after every successful request made to the DO Clients
beforeRequestDo BeforeRequestDoCallback
onRequestCompleted RequestCompletionCallback
}
type BeforeRequestDoCallback func(*http.Client, *http.Request, interface{})
// RequestCompletionCallback defines the type of the request callback function
type RequestCompletionCallback func(*http.Request, *http.Response)
func (c *Client) SetHTTPClient(client *http.Client) {
c.http = client
}
func (c Client) ClientID() string {
return c.clientID
}
func (c *Client) SetClientID(clientID string) {
c.clientID = clientID
}
func (c Client) ClientSecret() string {
return c.clientSecret
}
func (c *Client) SetClientSecret(clientSecret string) {
c.clientSecret = clientSecret
}
func (c Client) ToastRestaurantExternalID() string {
return c.toastRestaurantExternalID
}
func (c *Client) SetToastRestaurantExternalID(toastRestaurantExternalID string) {
c.toastRestaurantExternalID = toastRestaurantExternalID
}
func (c Client) Debug() bool {
return c.debug
}
func (c *Client) SetDebug(debug bool) {
c.debug = debug
}
func (c Client) BaseURL() url.URL {
return c.baseURL
}
func (c *Client) SetBaseURL(baseURL url.URL) {
c.baseURL = baseURL
}
func (c *Client) SetMediaType(mediaType string) {
c.mediaType = mediaType
}
func (c Client) MediaType() string {
return mediaType
}
func (c *Client) SetCharset(charset string) {
c.charset = charset
}
func (c Client) Charset() string {
return charset
}
func (c *Client) SetUserAgent(userAgent string) {
c.userAgent = userAgent
}
func (c Client) UserAgent() string {
return userAgent
}
func (c *Client) SetDisallowUnknownFields(disallowUnknownFields bool) {
c.disallowUnknownFields = disallowUnknownFields
}
func (c *Client) SetBeforeRequestDo(fun BeforeRequestDoCallback) {
c.beforeRequestDo = fun
}
func (c *Client) GetEndpointURL(p string, pathParams PathParams) url.URL {
clientURL := c.BaseURL()
parsed, err := url.Parse(p)
if err != nil {
log.Fatal(err)
}
q := clientURL.Query()
for k, vv := range parsed.Query() {
for _, v := range vv {
q.Add(k, v)
}
}
clientURL.RawQuery = q.Encode()
clientURL.Path = path.Join(clientURL.Path, parsed.Path)
tmpl, err := template.New("path").Parse(clientURL.Path)
if err != nil {
log.Fatal(err)
}
buf := new(bytes.Buffer)
params := pathParams.Params()
// params["administration_id"] = c.Administration()
err = tmpl.Execute(buf, params)
if err != nil {
log.Fatal(err)
}
clientURL.Path = buf.String()
return clientURL
}
func (c *Client) NewRequest(ctx context.Context, req Request) (*http.Request, error) {
// convert body struct to json
var body io.Reader
if req.RequestBodyInterface() != nil {
if r, ok := req.RequestBodyInterface().(io.Reader); ok {
body = r
} else if bb, ok := req.RequestBodyInterface().([]byte); ok {
body = bytes.NewReader(bb)
} else {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(req.RequestBodyInterface())
if err != nil {
return nil, err
}
body = buf
}
}
// create new http request
r, err := http.NewRequest(req.Method(), req.URL().String(), body)
if err != nil {
return nil, err
}
// values := url.Values{}
// err = utils.AddURLValuesToRequest(values, req, true)
// if err != nil {
// return nil, err
// }
// optionally pass along context
if ctx != nil {
r = r.WithContext(ctx)
}
// set other headers
r.Header.Add("Content-Type", fmt.Sprintf("%s; charset=%s", c.MediaType(), c.Charset()))
r.Header.Add("Accept", c.MediaType())
r.Header.Add("User-Agent", c.UserAgent())
if c.ToastRestaurantExternalID() != "" {
r.Header.Add("Toast-Restaurant-External-ID", c.ToastRestaurantExternalID())
}
return r, nil
}
func (c *Client) NewFormRequest(ctx context.Context, method string, URL url.URL, form Form) (*http.Request, error) {
body := &bytes.Buffer{}
w := multipart.NewWriter(body)
for k, vv := range form.Values() {
for _, v := range vv {
err := w.WriteField(k, v)
if err != nil {
return nil, err
}
}
}
for k, f := range form.Files() {
part, err := CreateFormFile(w, f.Content, k, f.Filename)
if err != nil {
return nil, err
}
_, err = io.Copy(part, f.Content)
}
err := w.Close()
if err != nil {
return nil, err
}
// create new http request
req, err := http.NewRequest(method, URL.String(), body)
if err != nil {
return nil, err
}
values := url.Values{}
err = utils.AddURLValuesToRequest(values, req, true)
if err != nil {
return nil, err
}
// optionally pass along context
if ctx != nil {
req = req.WithContext(ctx)
}
// set other headers
req.Header.Add("Content-Type", fmt.Sprintf("%s; charset=%s", w.FormDataContentType(), c.Charset()))
req.Header.Add("Accept", c.MediaType())
req.Header.Add("User-Agent", c.UserAgent())
return req, nil
}
// Do sends an Client request and returns the Client response. The Client response is json decoded and stored in the value
// pointed to by v, or returned as an error if an Client error has occurred. If v implements the io.Writer interface,
// the raw response will be written to v, without attempting to decode it.
func (c *Client) Do(req *http.Request, body interface{}) (*http.Response, error) {
if c.beforeRequestDo != nil {
c.beforeRequestDo(c.http, req, body)
}
if c.debug == true {
dump, _ := httputil.DumpRequestOut(req, true)
log.Println(string(dump))
}
httpResp, err := c.http.Do(req)
if err != nil {
return nil, err
}
if c.onRequestCompleted != nil {
c.onRequestCompleted(req, httpResp)
}
// close body io.Reader
defer func() {
if rerr := httpResp.Body.Close(); err == nil {
err = rerr
}
}()
if c.debug == true {
dump, _ := httputil.DumpResponse(httpResp, true)
log.Println(string(dump))
}
// check if the response isn't an error
err = CheckResponse(httpResp)
if err != nil {
return httpResp, err
}
// check the provided interface parameter
if httpResp == nil {
return httpResp, nil
}
if body == nil {
return httpResp, err
}
if httpResp.ContentLength == 0 {
return httpResp, nil
}
errResp := &ErrorResponse{Response: httpResp}
err = c.Unmarshal(httpResp.Body, body, errResp)
if err != nil {
return httpResp, err
}
if errResp.Error() != "" {
return httpResp, errResp
}
return httpResp, nil
}
func (c *Client) Unmarshal(r io.Reader, vv ...interface{}) error {
if len(vv) == 0 {
return nil
}
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}
errs := []error{}
for _, v := range vv {
r := bytes.NewReader(b)
dec := json.NewDecoder(r)
if c.disallowUnknownFields {
dec.DisallowUnknownFields()
}
err := dec.Decode(v)
if err != nil && err != io.EOF {
errs = append(errs, err)
}
}
if len(errs) == len(vv) {
// Everything errored
msgs := make([]string, len(errs))
for i, e := range errs {
msgs[i] = fmt.Sprint(e)
}
return errors.New(strings.Join(msgs, ", "))
}
return nil
}
// CheckResponse checks the Client response for errors, and returns them if
// present. A response is considered an error if it has a status code outside
// the 200 range. Client error responses are expected to have either no response
// body, or a json response body that maps to ErrorResponse. Any other response
// body will be silently ignored.
func CheckResponse(r *http.Response) error {
errorResponse := &ErrorResponse{Response: r}
// Don't check content-lenght: a created response, for example, has no body
// if r.Header.Get("Content-Length") == "0" {
// errorResponse.Errors.Message = r.Status
// return errorResponse
// }
if c := r.StatusCode; c >= 200 && c <= 299 {
return nil
}
// read data and copy it back
data, err := ioutil.ReadAll(r.Body)
r.Body = ioutil.NopCloser(bytes.NewReader(data))
if err != nil {
return errorResponse
}
err = checkContentType(r)
if err != nil {
return errors.WithStack(err)
}
if r.ContentLength == 0 {
return errors.New("response body is empty")
}
// convert json to struct
if len(data) != 0 {
err = json.Unmarshal(data, &errorResponse)
if err != nil {
return errors.WithStack(err)
}
}
if errorResponse.Error() != "" {
return errorResponse
}
return nil
}
// {
// "ErrorType": "AccountViewError",
// "ErrorNumbers": null,
// "ErrorMessage": ""
// }
type ErrorResponse struct {
// HTTP response that caused this error
Response *http.Response
Status int `json:"status"`
Code int `json:"code"`
Message string `json:"message"`
MessageKey string `json:"messageKey"`
FieldName string `json:"fieldName"`
Link string `json:"link"`
RequestID string `json:"requestId"`
DeveloperMessage string `json:"developerMessage"`
Errors []string `json:"errors"`
CanRetry interface{} `json:"canRetry"`
}
func (r *ErrorResponse) Error() string {
if r.Code == 0 {
return ""
}
return fmt.Sprintf("%d: %s", r.Code, r.Message)
}
func checkContentType(response *http.Response) error {
header := response.Header.Get("Content-Type")
contentType := strings.Split(header, ";")[0]
if contentType != mediaType {
return fmt.Errorf("Expected Content-Type \"%s\", got \"%s\"", mediaType, contentType)
}
return nil
}
func CreateFormFile(w *multipart.Writer, data io.Reader, fieldname, filename string) (io.Writer, error) {
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
escapeQuotes := func(s string) string {
return quoteEscaper.Replace(s)
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname), escapeQuotes(filename)))
contentType, err := GetFileContentType(data)
if err != nil {
return nil, err
}
h.Set("Content-Type", contentType)
return w.CreatePart(h)
}
func GetFileContentType(file io.Reader) (string, error) {
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err := file.Read(buffer)
if err != nil {
return "", err
}
// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
return contentType, nil
}
func (c *Client) InitToken(req *http.Request) error {
if c.token.AccessToken == "" {
req := c.NewLoginPostRequest()
req.RequestBody().ClientID = c.ClientID()
req.RequestBody().ClientSecret = c.ClientSecret()
resp, err := req.Do()
if err != nil {
return errors.WithStack(err)
}
c.token = resp.Token
}
if c.token.IsExpired() {
req := c.NewLoginPostRequest()
req.RequestBody().ClientID = c.ClientID()
req.RequestBody().ClientSecret = c.ClientSecret()
resp, err := req.Do()
if err != nil {
return errors.WithStack(err)
}
c.token = resp.Token
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.token.AccessToken))
return nil
}
func (c *Client) GetNextURL(resp *http.Response) (string, error) {
for _, h := range resp.Header.Values("Link") {
pieces := strings.Split(h, "; ")
if len(pieces) < 2 {
// do nothing
continue
}
if pieces[1] == `rel="next"` {
return strings.TrimRight(strings.TrimLeft(pieces[0], "<"), ">"), nil
}
}
return "", nil
}
func (c *Client) GetNextPage(resp *http.Response) (int, error) {
s, err := c.GetNextURL(resp)
if s == "" {
return 0, nil
}
u, err := url.Parse(s)
if err != nil {
return 0, err
}
s = u.Query().Get("page")
if s == "" {
return 0, nil
}
return strconv.Atoi(s)
}
func (c *Client) GetPageToken(resp *http.Response) (string, error) {
s, err := c.GetNextURL(resp)
if s == "" {
return "", nil
}
u, err := url.Parse(s)
if err != nil {
return "", err
}
return u.Query().Get("pageToken"), nil
}