-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
340 lines (262 loc) · 6.76 KB
/
http.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package sailor
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)
type HTTPClient struct {
debug bool
isSchemeHTTP bool
timeout int
domain string
endpoint string
url string
method string
// Implement `curl --data-urlencode`
values url.Values
headers map[string]string
body interface{}
}
const (
UserAgent = "sailor-agent/1.0.0"
UserAgentKey = "User-Agent"
SchemeHTTP = "http://"
SchemeHTTPS = "https://"
)
func (client *HTTPClient) SetDebug() {
client.debug = true
}
func (client *HTTPClient) GetDebug() bool {
return client.debug
}
func (client *HTTPClient) SetDomain(domain string) {
// Trim scheme.
domain = strings.TrimPrefix(domain, SchemeHTTPS)
domain = strings.TrimPrefix(domain, SchemeHTTP)
client.domain = domain
}
func (client *HTTPClient) GetDomain() string {
return client.domain
}
func (client *HTTPClient) SetEndpoint(endpoint string) {
client.endpoint = endpoint
}
func (client *HTTPClient) GetEndpoint() string {
return client.endpoint
}
func (client *HTTPClient) SetURL(url string) {
client.url = url
}
func (client *HTTPClient) GetURL() string {
if client.url != "" {
return client.url
}
if client.isSchemeHTTP {
client.url = SchemeHTTP + client.GetDomain() + client.GetEndpoint()
} else {
client.url = SchemeHTTPS + client.GetDomain() + client.GetEndpoint()
}
return client.url
}
func (client *HTTPClient) SetTimeout(timeout int) {
client.timeout = timeout
}
func (client *HTTPClient) GetTimeout() int {
return client.timeout
}
func (client *HTTPClient) SetMethod(method string) {
client.method = method
}
func (client *HTTPClient) GetMethod() string {
return client.method
}
func (client *HTTPClient) SetHeaders(headers map[string]string) {
client.headers = headers
}
func (client *HTTPClient) GetHeaders() map[string]string {
return client.headers
}
func (client *HTTPClient) SetValues(values url.Values) {
client.values = values
}
func (client *HTTPClient) GetValues() url.Values {
return client.values
}
func (client *HTTPClient) SetBody(body interface{}) {
client.body = body
}
func (client *HTTPClient) GetBody() interface{} {
return client.body
}
func (client *HTTPClient) SetUserAgent(ua string) {
if client.headers == nil {
header := make(map[string]string)
client.headers = header
}
client.headers[UserAgentKey] = ua
}
func (client *HTTPClient) GetUserAgent() string {
return client.headers[UserAgentKey]
}
type RequestInterface interface {
SetDebug()
GetDebug() bool
SetDomain(string)
GetDomain() string
SetEndpoint(string)
GetEndpoint() string
SetURL(string)
GetURL() string
SetMethod(string)
GetMethod() string
SetTimeout(int)
GetTimeout() int
SetHeaders(headers map[string]string)
GetHeaders() map[string]string
SetValues(url.Values)
GetValues() url.Values
SetBody(interface{})
GetBody() interface{}
SetUserAgent(string)
GetUserAgent() string
}
type ResponseInterface interface{}
const defaultHTTPTimout = 60
func NewHTTPClient() *HTTPClient {
return &HTTPClient{
timeout: defaultHTTPTimout,
}
}
func (client *HTTPClient) HTTPRequest(requestInterface RequestInterface, responseInterface ResponseInterface) error {
if requestInterface.GetURL() == "" {
return errors.New("API url is null, please check")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(defaultHTTPTimout))
defer cancel()
switch requestInterface.GetMethod() {
case http.MethodGet:
return client.HTTPRequestGet(ctx, requestInterface, responseInterface)
case http.MethodPost, http.MethodPatch:
return client.HTTPRequestPost(ctx, requestInterface, responseInterface)
default:
requestInterface.SetMethod(http.MethodGet)
return client.HTTPRequestGet(ctx, requestInterface, responseInterface)
}
}
func (client *HTTPClient) HTTPRequestGet(
ctx context.Context,
requestInterface RequestInterface,
responseInterface ResponseInterface,
) error {
/*
var r *strings.Reader
if requestInterface.GetValues() != nil {
r = strings.NewReader(requestInterface.GetValues().Encode())
}
log.Println(r)
*/
request, err := http.NewRequestWithContext(ctx,
requestInterface.GetMethod(),
requestInterface.GetURL(),
nil)
if err != nil {
client.debugLog(err)
return err
}
if requestInterface.GetValues() != nil {
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
// The RawQuery should binding on `url`. Like follow:
// Query string `url:"q"`
body, err := query.Values(requestInterface.GetBody())
if err != nil {
return err
}
if len(body) > 0 {
request.URL.RawQuery = body.Encode()
}
return client.DoRequest(request, requestInterface, responseInterface)
}
func (client *HTTPClient) HTTPRequestPost(
ctx context.Context,
requestInterface RequestInterface,
responseInterface ResponseInterface,
) error {
payload, err := json.Marshal(requestInterface.GetBody())
if err != nil {
return err
}
request, err := http.NewRequestWithContext(
ctx,
requestInterface.GetMethod(),
requestInterface.GetURL(),
bytes.NewBuffer(payload))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/json")
return client.DoRequest(request, requestInterface, responseInterface)
}
func (client *HTTPClient) DoRequest(
request *http.Request,
requestInterface RequestInterface,
responseInterface ResponseInterface,
) error {
for headerKey, headerValue := range requestInterface.GetHeaders() {
request.Header.Set(headerKey, headerValue)
}
headers := requestInterface.GetHeaders()
if headers[UserAgentKey] == "" {
request.Header.Set(UserAgentKey, UserAgent)
}
_client := http.DefaultClient
resp, err := _client.Do(request)
if err != nil {
client.debugLog(err)
return err
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
// For debug.
client.debugLog(string(bodyBytes))
resp.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
switch resp.StatusCode {
case http.StatusOK:
return json.NewDecoder(resp.Body).Decode(&responseInterface)
default:
client.debugLog(resp.StatusCode)
return errors.Errorf("http status code is not 200.")
}
}
func (client *HTTPClient) debugLog(msg interface{}) {
if client.debug {
log.Println(msg)
}
}
func (client *HTTPClient) Downloader() (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, client.GetURL(), nil)
if err != nil {
client.debugLog(err)
return nil, err
}
transport := http.DefaultTransport.(*http.Transport)
resp, err := transport.RoundTrip(req)
if err != nil {
client.debugLog(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
client.debugLog("download status is not 200")
return nil, err
}
return resp.Body, nil
}