-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
125 lines (104 loc) · 2.63 KB
/
requests.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
package vctr
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
type requestClient struct {
rootEndpoint string
authHeader string
client *http.Client
}
func newRequestClient(rootEndpoint string, authHeader ...string) (c *requestClient) {
c = new(requestClient)
if !strings.HasPrefix(rootEndpoint, "https://") && !strings.HasPrefix(rootEndpoint, "http://") {
rootEndpoint = "https://" + rootEndpoint
}
c.rootEndpoint = strings.TrimSuffix(rootEndpoint, "/")
c.client = http.DefaultClient
if len(authHeader) > 0 && authHeader[0] != "" {
c.authHeader = authHeader[0]
}
return
}
func (c *requestClient) SetAuthHeader(header string) {
c.authHeader = header
}
func (c *requestClient) Get(path string, query url.Values, res interface{}) error {
return c.request("GET", path, nil, query, res)
}
func (c *requestClient) Post(path string, body interface{}, query url.Values, res interface{}) error {
return c.request("POST", path, body, query, res)
}
func (c *requestClient) Delete(path string, query url.Values) error {
return c.request("DELETE", path, nil, query, nil)
}
func (c *requestClient) request(
method, path string,
body interface{},
query url.Values,
response interface{},
) (err error) {
resource, err := url.Parse(fmt.Sprintf("%s/api/%s", c.rootEndpoint, strings.TrimPrefix(path, "/")))
if err != nil {
return
}
if query != nil {
resource.RawQuery = query.Encode()
}
var bodyReader io.ReadWriter
if body != nil {
bodyReader = bytes.NewBuffer([]byte{})
if err = json.NewEncoder(bodyReader).Encode(body); err != nil {
return
}
}
req, err := http.NewRequest(method, resource.String(), bodyReader)
if err != nil {
return
}
req.Header.Add("content-type", "application/json")
if c.authHeader != "" {
req.Header.Add("authorization", c.authHeader)
}
res, err := c.client.Do(req)
if err != nil {
return
}
if res.StatusCode >= 400 {
err = parseError(res)
return
}
if response != nil {
err = json.NewDecoder(res.Body).Decode(response)
}
return
}
func parseError(res *http.Response) (err *ResponseError) {
err = new(ResponseError)
err.Code = res.StatusCode
err.Message = res.Status
resBody := new(struct {
Error string `json:"error"`
Errors map[string][]string
})
if parseErr := json.NewDecoder(res.Body).Decode(resBody); parseErr != nil {
return
}
if resBody.Error != "" {
err.Message = resBody.Error
} else if resBody.Errors != nil && len(resBody.Errors) > 0 {
errors := make([]string, len(resBody.Errors))
i := 0
for k, v := range resBody.Errors {
errors[i] = k + ": " + v[0]
i++
}
err.Message = strings.Join(errors, "\n")
}
return
}