forked from intercom/intercom-go
/
http_client.go
172 lines (150 loc) · 4.12 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package interfaces
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/google/go-querystring/query"
)
type HTTPClient interface {
Get(string, interface{}) ([]byte, error)
Post(string, interface{}) ([]byte, error)
Patch(string, interface{}) ([]byte, error)
Delete(string, interface{}) ([]byte, error)
}
type IntercomHTTPClient struct {
*http.Client
BaseURI *string
AppID string
APIKey string
ClientVersion *string
Debug *bool
}
func NewIntercomHTTPClient(appID, apiKey string, baseURI, clientVersion *string, debug *bool) IntercomHTTPClient {
return IntercomHTTPClient{Client: &http.Client{}, AppID: appID, APIKey: apiKey, BaseURI: baseURI, ClientVersion: clientVersion, Debug: debug}
}
func (c IntercomHTTPClient) UserAgentHeader() string {
return fmt.Sprintf("intercom-go/%s", *c.ClientVersion)
}
func (c IntercomHTTPClient) Get(url string, queryParams interface{}) ([]byte, error) {
// Setup request
req, _ := http.NewRequest("GET", *c.BaseURI+url, nil)
req.SetBasicAuth(c.AppID, c.APIKey)
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", c.UserAgentHeader())
addQueryParams(req, queryParams)
if *c.Debug {
fmt.Printf("%s %s\n", req.Method, req.URL)
}
// Do request
resp, err := c.Client.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
// Read response
data, err := c.readAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, c.parseResponseError(data, resp.StatusCode)
}
return data, err
}
func addQueryParams(req *http.Request, params interface{}) {
v, _ := query.Values(params)
req.URL.RawQuery = v.Encode()
}
func (c IntercomHTTPClient) Patch(url string, body interface{}) ([]byte, error) {
return c.postOrPatch("PATCH", url, body)
}
func (c IntercomHTTPClient) Post(url string, body interface{}) ([]byte, error) {
return c.postOrPatch("POST", url, body)
}
func (c IntercomHTTPClient) postOrPatch(method, url string, body interface{}) ([]byte, error) {
// Marshal our body
buffer := bytes.NewBuffer([]byte{})
if err := json.NewEncoder(buffer).Encode(body); err != nil {
return nil, err
}
// Setup request
req, err := http.NewRequest(method, *c.BaseURI+url, buffer)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.AppID, c.APIKey)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", c.UserAgentHeader())
if *c.Debug {
fmt.Printf("%s %s %s\n", req.Method, req.URL, buffer)
}
// Do request
resp, err := c.Client.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
// Read response
data, err := c.readAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, c.parseResponseError(data, resp.StatusCode)
}
return data, err
}
func (c IntercomHTTPClient) Delete(url string, queryParams interface{}) ([]byte, error) {
// Setup request
req, _ := http.NewRequest("DELETE", *c.BaseURI+url, nil)
req.SetBasicAuth(c.AppID, c.APIKey)
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", c.UserAgentHeader())
addQueryParams(req, queryParams)
if *c.Debug {
fmt.Printf("%s %s\n", req.Method, req.URL)
}
// Do request
resp, err := c.Client.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
// Read response
data, err := c.readAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, c.parseResponseError(data, resp.StatusCode)
}
return data, err
}
type IntercomError interface {
Error() string
GetStatusCode() int
GetCode() string
GetMessage() string
}
func (c IntercomHTTPClient) parseResponseError(data []byte, statusCode int) IntercomError {
errorList := HTTPErrorList{}
err := json.Unmarshal(data, &errorList)
if err != nil {
return NewUnknownHTTPError()
}
httpError := errorList.Errors[0]
httpError.StatusCode = statusCode
return httpError // only care about the first
}
func (c IntercomHTTPClient) readAll(body io.Reader) ([]byte, error) {
b, err := ioutil.ReadAll(body)
if *c.Debug {
fmt.Println(string(b))
fmt.Println("")
}
return b, err
}