-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_client.go
214 lines (165 loc) · 4.43 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package toshl
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
// HTTPClient is an interface to define the client to access API resources
type HTTPClient interface {
Get(APIUrl, queryString string) (string, error)
Post(APIUrl, JSONPayload string) (string, error)
Update(APIUrl, JSONPayload string) (string, error)
Delete(APIUrl string) error
}
// RestHTTPClient is a real implementation of the HTTPClient
type RestHTTPClient struct {
BaseURL string
Token string
Client *http.Client
}
func (c *RestHTTPClient) setAuthenticationHeader(req *http.Request) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
}
func (c *RestHTTPClient) setJSONContentTypeHeader(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
}
func (c *RestHTTPClient) setUserAgentHeader(req *http.Request) {
req.Header.Set("User-Agent", GetUserAgentString())
}
func (c *RestHTTPClient) getIDFromLocationHeader(
response *http.Response) (string, error) {
locationHeader := response.Header.Get("Location")
id, err := c.parseIDFromLocationHeader(locationHeader)
if err != nil {
log.Fatal("Location URL parsing: ", err)
return "", err
}
return id, nil
}
func (c *RestHTTPClient) parseIDFromLocationHeader(
locationURL string) (string, error) {
guid, err := url.Parse(locationURL)
if err != nil {
log.Fatal("Location URL parsing: ", err)
return "", err
}
values := strings.Split(guid.Path, "/")
if len(values) > 1 {
id := values[len(values)-1]
return id, nil
}
return "", errors.New("Cannot parse resource ID")
}
// Get takes an API endpoint and return a JSON string
func (c *RestHTTPClient) Get(APIUrl, queryString string) (string, error) {
url := c.BaseURL + "/" + APIUrl
if queryString != "" {
url = url + "?" + queryString
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return "", err
}
// Set authorization token
c.setAuthenticationHeader(req)
// Set User-Agent header
c.setUserAgentHeader(req)
resp, err := c.Client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return "", err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("ReadAll: ", err)
return "", err
}
return string(bs), nil
}
// Post takes an API endpoint and a JSON payload and return string ID
func (c *RestHTTPClient) Post(APIUrl, JSONPayload string) (string, error) {
url := c.BaseURL + "/" + APIUrl
var jsonStr = []byte(JSONPayload)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
log.Fatal("NewRequest: ", err)
return "", err
}
// Set authorization token
c.setAuthenticationHeader(req)
// Set JSON content type
c.setJSONContentTypeHeader(req)
// Set User-Agent header
c.setUserAgentHeader(req)
resp, err := c.Client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return "", err
}
defer resp.Body.Close()
// Parse Location header to get ID
id, err := c.getIDFromLocationHeader(resp)
if err != nil {
log.Fatal("Do: ", err)
return "", err
}
return id, nil
}
// Update takes an API endpoint and a JSON payload and update the resource
func (c *RestHTTPClient) Update(APIUrl, JSONPayload string) (string, error) {
url := c.BaseURL + "/" + APIUrl
var jsonStr = []byte(JSONPayload)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
if err != nil {
log.Fatal("NewRequest: ", err)
return "", err
}
// Set authorization token
c.setAuthenticationHeader(req)
// Set JSON content type
c.setJSONContentTypeHeader(req)
// Set User-Agent header
c.setUserAgentHeader(req)
resp, err := c.Client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return "", err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("ReadAll: ", err)
return "", err
}
return string(bs), nil
}
// Delete removes the Account having the ID specified in the endpoint
func (c *RestHTTPClient) Delete(APIUrl string) error {
url := c.BaseURL + "/" + APIUrl
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return err
}
// Set authorization token
c.setAuthenticationHeader(req)
// Set User-Agent header
c.setUserAgentHeader(req)
_, err = c.Client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return err
}
return nil
}
func (c *RestHTTPClient) SetTimeoutSeconds(timeout int) {
c.Client.Timeout = time.Duration(timeout) * time.Second
}