forked from Gerifield/gobitpanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
92 lines (74 loc) · 1.88 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
package gobitpanda
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
)
// NewClient returns a new Client struct
func NewClient(APIBase string, APIToken string) (*Client, error) {
if APIBase == "" {
return nil, errors.New("APIBase is required to create a Client")
}
return &Client{
Client: &http.Client{},
APIBase: APIBase,
APIToken: APIToken,
}, nil
}
// Send makes a request to the API and tries to unmarshal the response
func (c *Client) Send(req *http.Request, i interface{}) error {
var (
err error
resp *http.Response
data []byte
)
req.Header.Set("Accept", "application/json")
if req.Header.Get("Content-type") == "" {
req.Header.Set("Content-type", "application/json")
}
resp, err = c.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
errResp := &ErrorResponse{Response: resp}
data, err = ioutil.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
json.Unmarshal(data, errResp)
}
return errors.New(errResp.Error)
}
if i == nil {
return nil
}
if w, ok := i.(io.Writer); ok {
io.Copy(w, resp.Body)
return nil
}
return json.NewDecoder(resp.Body).Decode(i)
}
// SendWithAuth makes a request to the API with an Auth header
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
if c.APIToken != "" {
req.Header.Set("Authorization", "Bearer "+c.APIToken)
} else {
return errors.New("Client has no API Key, please first set an API Key")
}
return c.Send(req, v)
}
// NewRequest creates a new request and convert and add data as JSON if given
func (c *Client) NewRequest(method, url string, data interface{}) (*http.Request, error) {
var buffer io.Reader
if data != nil {
b, err := json.Marshal(&data)
if err != nil {
return nil, err
}
buffer = bytes.NewBuffer(b)
}
return http.NewRequest(method, url, buffer)
}