-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_client.go
226 lines (211 loc) · 7.82 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
215
216
217
218
219
220
221
222
223
224
225
226
package http_goclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"bytes"
)
type HttpClientIface interface {
Get(url string, headers map[string]string, out interface{}) (*http.Response, error)
PostJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
Post(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
PutJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
Put(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
Delete(url string, headers map[string]string) (*http.Response, error)
}
// HttpClient represents an http wrapper which reduces the boiler plate needed to marshall/un-marshall request/response
// bodies by providing friendly CRUD http operations that allow in/out interfaces
type HttpClient struct {
HttpClient *http.Client
}
// Get issues a GET HTTP request to the specified URL including the headers passed in.
//
// The 'out' param interface is the un-marshall representation of the http response returned
//
// Example on how to invoke the method:
//
// type Out struct {
// Id string `json:"id"`
// Name string `json:"name"`
// Description string `json:"description"`
// }
//
// out := &Out{}
// headers := map[string]string{"header_example": "header_value"}
// HttpClient.Get("http://api.com/resource", headers, out)
//
func (httpClient *HttpClient) Get(url string, headers map[string]string, out interface{}) (*http.Response, error) {
if req, err := httpClient.prepareRequest(http.MethodGet, url, headers, nil); err != nil {
return nil, err
} else {
return httpClient.performRequest(req, out)
}
}
// PostJson issues a POST to the specified URL including the headers passed in.
// The content type of the body is set to application/json so it doesn't need to be added to the headers passed in
//
// The 'in' param interface is marshall and added to the htp request body.
// The 'out' param interface is the un-marshall representation of the http response returned
//
// Example on how to invoke the method:
//
// type In struct {
// Name string `json:"name"`
// Description string `json:"description"`
// }
//
// type Out struct {
// Id string `json:"id"`
// Name string `json:"name"`
// Description string `json:"description"`
// }
// in := &In{}
// out := &Out{}
// headers := map[string]string{"header_example": "header_value"}
// HttpClient.PostJson("http://api.com/resource", headers, in, out)
//
func (httpClient *HttpClient) PostJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error) {
httpClient.addJsonHeader(headers)
return httpClient.Post(url, headers, in, out)
}
// Post issues a POST HTTP request to the specified URL including the headers passed in.
//
// The in interface is marshall and added to the htp request body.
// The out interface is the un-marshall representation of the http response returned
//
// Example on how to invoke the method:
//
// type In struct {
// Name string `json:"name"`
// Description string `json:"description"`
// }
//
// type Out struct {
// Id string `json:"id"`
// Name string `json:"name"`
// Description string `json:"description"`
// }
// in := &In{}
// out := &Out{}
// headers := map[string]string{"header_example": "header_value"}
// HttpClient.Post("http://api.com/resource", headers, in, out)
//
func (httpClient *HttpClient) Post(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error) {
if req, err := httpClient.prepareRequest(http.MethodPost, url, headers, in); err != nil {
return nil, err
} else {
return httpClient.performRequest(req, out)
}
}
// PutJson issues a PUT HTTP request to the specified URL including the headers passed in.
// The content type of the body is set to application/json
//
// The 'in' param interface is marshall and added to the htp request body.
// The 'out' param interface is the un-marshall representation of the http response returned
//
// Example on how to invoke the method:
//
// type In struct {
// Name string `json:"name"`
// Description string `json:"description"`
// }
//
// type Out struct {
// Id string `json:"id"`
// Name string `json:"name"`
// Description string `json:"description"`
// }
// in := &In{}
// out := &Out{}
// headers := map[string]string{"header_example": "header_value"}
// HttpClient.PutJson("http://api.com/resource", headers, in, out)
//
func (httpClient *HttpClient) PutJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error) {
httpClient.addJsonHeader(headers)
return httpClient.Put(url, headers, in, out)
}
// Post issues a PUT HTTP request to the specified URL including the headers passed in.
//
// The in interface is marshall and added to the http request body.
// The out interface is the un-marshall representation of the http response returned
//
// Example on how to invoke the method:
//
// type In struct {
// Name string `json:"name"`
// Description string `json:"description"`
// }
//
// type Out struct {
// Id string `json:"id"`
// Name string `json:"name"`
// Description string `json:"description"`
// }
// in := &In{}
// out := &Out{}
// headers := map[string]string{"header_example": "header_value"}
// HttpClient.Put("http://api.com/resource", headers, in, out)
//
func (httpClient *HttpClient) Put(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error) {
if req, err := httpClient.prepareRequest(http.MethodPut, url, headers, in); err != nil {
return nil, err
} else {
return httpClient.performRequest(req, out)
}
}
// Delete issues a DELETE HTTP request to the specified URL including the headers passed in.
func (httpClient *HttpClient) Delete(url string, headers map[string]string) (*http.Response, error) {
if req, err := httpClient.prepareRequest(http.MethodDelete, url, headers, nil); err != nil {
return nil, err
} else {
return httpClient.performRequest(req, nil)
}
}
func (httpClient *HttpClient) prepareRequest(method, url string, headers map[string]string, in interface{}) (*http.Request, error) {
var body []byte
var err error
if in != nil {
body, err = json.Marshal(in)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, url, strings.NewReader(string(body)))
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Set(key, value)
}
return req, nil
}
func (httpClient *HttpClient) performRequest(req *http.Request, out interface{}) (*http.Response, error) {
resp, err := httpClient.HttpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request %s %s %s failed. Response Error: '%s'", req.Method, req.URL, req.Proto, err.Error())
}
if out != nil {
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return nil, err
}
resp.Body.Close() // close stream so connection is closed gracefully
resp.Body = ioutil.NopCloser(bytes.NewReader(body)) // create a new reader from bytes read in the response and set the response body (allowing the client to still be able to do res.Body afterwards)
if len(body) > 0 {
if err = json.Unmarshal(body, &out); err != nil {
return resp, fmt.Errorf("unable to unmarshal response body ['%s'] for request = '%s %s %s'. Response = '%s'", err.Error(), req.Method, req.URL, req.Proto, resp.Status)
}
} else {
return resp, fmt.Errorf("expected a response body but response body received was empty for request = '%s %s %s'. Response = '%s'", req.Method, req.URL, req.Proto, resp.Status)
}
}
return resp, nil
}
func (httpClient *HttpClient) addJsonHeader(headers map[string]string) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/json"
}