-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.go
83 lines (76 loc) · 1.69 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
package go_printify
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)
const (
contentType = "application/json;charset=utf-8"
baseURL = "api.printify.com"
scheme = "https"
)
type ApiRequest interface {
GetMethod() string
GetPath() string
GetBody() interface{}
GetResponseStruct() *interface{}
}
type Client struct {
BaseURL *url.URL
ApiVersion string
UserAgent string
httpClient *http.Client
apiKey string
}
func NewClient(apiKey string) *Client {
return &Client{
BaseURL: &url.URL{
Scheme: scheme,
Host: baseURL,
},
UserAgent: "go-printify v1.0",
httpClient: http.DefaultClient,
apiKey: apiKey,
}
}
func (c *Client) newRequest(method, path string, body interface{}) (*http.Request, error) {
rel := &url.URL{Path: fmt.Sprintf("%s/%s", c.ApiVersion, path)}
u := c.BaseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", contentType)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
return req, nil
}
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode >= 400 {
return resp, errors.New(fmt.Sprintf("%d", resp.StatusCode))
}
err = json.NewDecoder(resp.Body).Decode(v)
return resp, err
}