-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_client.go
139 lines (112 loc) · 2.99 KB
/
http_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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package helpscout
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
)
var ErrorRateLimit = errors.New("")
var ErrorUnauthorized = errors.New("")
type httpClient struct {
http.Client
}
func newHttpClient() *httpClient {
return &httpClient{
http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
},
},
}
}
func (h *httpClient) doRequest(url string, method string,
headers map[string]string, query *url.Values,
reqData interface{}, respData interface{}) error {
var err error
var req *http.Request
if reqData != nil {
var jsonRaw []byte
if jsonRaw, err = json.Marshal(reqData); err != nil {
return errors.Wrap(err, "Unable to marshal request data")
}
reqDataBuffer := bytes.NewBuffer(jsonRaw)
req, err = http.NewRequest(method, url, reqDataBuffer)
} else {
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
return errors.Wrap(err, "Unable to prepare new request")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}
if query != nil {
req.URL.RawQuery = query.Encode()
}
response, err := h.Do(req)
if err != nil {
return errors.Wrap(err, "Unable to process request")
}
defer response.Body.Close()
if response.StatusCode == 201 {
return nil
}
if response.StatusCode != 200 {
if response.StatusCode == 429 {
return ErrorRateLimit
}
if response.StatusCode == 401 {
return ErrorUnauthorized
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "Unable to read response body to decode error")
}
if len(body) != 0 {
var errResp struct {
Message string `json:"message"`
Embedded struct {
Errors []struct {
Path string `json:"path"`
Message string `json:"message"`
Source string `json:"source"`
} `json:"errors"`
} `json:"_embedded"`
}
if err := json.Unmarshal(body, &errResp); err != nil {
return errors.Wrap(err, "Unable to parse error response-body as json")
}
return errors.Errorf("Remote server returned an error: %d [%+v]", response.StatusCode, errResp)
}
return errors.Errorf("Remote server returned an error: %d", response.StatusCode)
}
if !strings.Contains(response.Header.Get("Content-Type"), "application/json") &&
!strings.Contains(response.Header.Get("Content-Type"), "application/hal+json") {
return errors.Errorf("Remote server returned an invalid content type: %s",
response.Header.Get("Content-Type"))
}
if respData == nil {
return nil
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "Unable to read response body")
}
if err := json.Unmarshal(body, respData); err != nil {
return errors.Wrap(err, "Unable to parse response-body as json")
}
return nil
}