-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
89 lines (72 loc) · 2.14 KB
/
helpers.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
package apiary
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
)
func checkOk(response *http.Response) error {
if response.StatusCode != http.StatusOK {
return fmt.Errorf("Bad response code: %s", response.Status)
}
return nil
}
func readResponse(response *http.Response) ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, response.ContentLength))
n, err := buf.ReadFrom(response.Body)
if err != nil {
return nil, err
}
if n == 0 {
return nil, errors.New("Empty response")
}
return buf.Bytes(), nil
}
func bearerToken(token string) string {
buf := bytes.NewBuffer(make([]byte, 0, len(token)+7))
buf.Write([]byte(`bearer `))
buf.Write([]byte(token))
return buf.String()
}
func bearerTokenLegacy(token string) string {
buf := bytes.NewBuffer(make([]byte, 0, len(token)+6))
buf.Write([]byte(`Token `))
buf.Write([]byte(token))
return buf.String()
}
func (a *Apiary) request(method string, path string, headers map[string]string, body io.Reader) (response []byte, res *http.Response, err error) {
url := ApiaryAPIURL + path
req, err := http.NewRequest(method, url, body)
if err != nil {
return
}
for k, v := range headers {
req.Header.Add(k, v)
}
res, err = a.client.Do(req)
if err != nil {
return
}
response, err = readResponse(res)
return
}
func (a *Apiary) sendRequest(path string) (data []byte, response *http.Response, err error) {
headers := make(map[string]string)
headers["Authorization"] = bearerToken(a.options.Token)
data, response, err = a.request("GET", path, headers, nil)
return
}
func (a *Apiary) sendLegacyRequest(path string) (data []byte, response *http.Response, err error) {
headers := make(map[string]string)
headers["Authentication"] = bearerTokenLegacy(a.options.Token)
data, response, err = a.request("GET", path, headers, nil)
return
}
func (a *Apiary) sendLegacyPostRequest(path string, body io.Reader) (data []byte, response *http.Response, err error) {
headers := make(map[string]string)
headers["Authentication"] = bearerTokenLegacy(a.options.Token)
headers["Content-Type"] = "application/json; charset=utf-8"
data, response, err = a.request("POST", path, headers, body)
return
}