-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
92 lines (81 loc) · 2.18 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 adafruitio
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
const APIEndpoint = "https://io.adafruit.com/api/v2"
type Client struct {
authToken string
apiEndpoint string
}
func NewClient(token string) *Client {
return &Client{
authToken: token,
apiEndpoint: APIEndpoint,
}
}
func (c *Client) do(method, path string, body io.Reader, headers *map[string]string) (*http.Response, error) {
endpoint := c.apiEndpoint + path
req, err := http.NewRequest(method, endpoint, body)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
if headers != nil {
for k, v := range *headers {
req.Header.Set(k, v)
}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-AIO-Key", c.authToken)
req.Header.Set("User-Agent", "go-client")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if (resp.StatusCode >= 200) && (resp.StatusCode < 300) {
return resp, nil
}
d, e := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if e != nil {
return resp, fmt.Errorf("HTTP Code: %d. Failed to read response body. Error: %s", resp.StatusCode, e)
}
return resp, fmt.Errorf(string(d))
}
func (c *Client) decodeJSON(resp *http.Response, payload interface{}) error {
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
return decoder.Decode(payload)
}
func (c *Client) get(path string) (*http.Response, error) {
return c.do("GET", path, nil, nil)
}
func (c *Client) delete(path string) (*http.Response, error) {
return c.do("DELETE", path, nil, nil)
}
func (c *Client) post(path string, payload interface{}) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return c.do("POST", path, bytes.NewBuffer(data), nil)
}
func (c *Client) put(path string, payload interface{}) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return c.do("PUT", path, bytes.NewBuffer(data), nil)
}
func (c *Client) patch(path string, payload interface{}) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return c.do("PATCH", path, bytes.NewBuffer(data), nil)
}