This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
http.go
303 lines (267 loc) · 6.46 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
package kucoin
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// A Request represents a HTTP request.
type Request struct {
fullURL string
requestURI string
BaseURI string
Method string
Path string
Query url.Values
Body []byte
Header http.Header
Timeout time.Duration
SkipVerifyTls bool
}
// NewRequest creates a instance of Request.
func NewRequest(method, path string, params interface{}) *Request {
r := &Request{
Method: method,
Path: path,
Query: make(url.Values),
Header: make(http.Header),
Body: []byte{},
Timeout: 30 * time.Second,
}
if r.Path == "" {
r.Path = "/"
}
if r.Method == "" {
r.Method = http.MethodGet
}
r.addParams(params)
return r
}
func (r *Request) addParams(p interface{}) {
if p == nil {
return
}
switch r.Method {
case http.MethodGet, http.MethodDelete:
if v, ok := p.(url.Values); ok {
r.Query = v
return
}
for key, value := range p.(map[string]string) {
r.Query.Add(key, value)
}
default:
b, err := json.Marshal(p)
if err != nil {
log.Panic("Cannot marshal params to JSON string:", err.Error())
}
r.Body = b
}
}
// RequestURI returns the request uri.
func (r *Request) RequestURI() string {
if r.requestURI != "" {
return r.requestURI
}
fu := r.FullURL()
u, err := url.Parse(fu)
if err != nil {
r.requestURI = "/"
} else {
r.requestURI = u.RequestURI()
}
return r.requestURI
}
// FullURL returns the full url.
func (r *Request) FullURL() string {
if r.fullURL != "" {
return r.fullURL
}
r.fullURL = fmt.Sprintf("%s%s", r.BaseURI, r.Path)
if len(r.Query) > 0 {
if strings.Contains(r.fullURL, "?") {
r.fullURL += "&" + r.Query.Encode()
} else {
r.fullURL += "?" + r.Query.Encode()
}
}
return r.fullURL
}
// HttpRequest creates a instance of *http.Request.
func (r *Request) HttpRequest() (*http.Request, error) {
req, err := http.NewRequest(r.Method, r.FullURL(), bytes.NewBuffer(r.Body))
if err != nil {
return nil, err
}
for key, values := range r.Header {
for _, value := range values {
req.Header.Add(key, value)
}
}
return req, nil
}
// Requester contains Request() method, can launch a http request.
type Requester interface {
Request(ctx context.Context, request *Request, timeout time.Duration) (*Response, error)
}
// A BasicRequester represents a basic implement of Requester by http.Client.
type BasicRequester struct {
}
// Request makes a http request.
func (br *BasicRequester) Request(ctx context.Context, request *Request, timeout time.Duration) (*Response, error) {
tr := http.DefaultTransport
tc := tr.(*http.Transport).TLSClientConfig
if tc == nil {
tc = &tls.Config{InsecureSkipVerify: request.SkipVerifyTls}
} else {
tc.InsecureSkipVerify = request.SkipVerifyTls
}
cli := http.DefaultClient
cli.Transport, cli.Timeout = tr, timeout
req, err := request.HttpRequest()
if err != nil {
return nil, err
}
// Prevent re-use of TCP connections
// req.Close = true
rid := time.Now().UnixNano()
if DebugMode {
dump, _ := httputil.DumpRequest(req, true)
logrus.Debugf("Sent a HTTP request#%d: %s", rid, string(dump))
}
rsp, err := cli.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
if DebugMode {
dump, _ := httputil.DumpResponse(rsp, true)
logrus.Debugf("Received a HTTP response#%d: %s", rid, string(dump))
}
return NewResponse(
request,
rsp,
nil,
), nil
}
// A Response represents a HTTP response.
type Response struct {
request *Request
*http.Response
body []byte
}
// NewResponse Creates a new Response
func NewResponse(
request *Request,
response *http.Response,
body []byte,
) *Response {
return &Response{
request: request,
Response: response,
body: body,
}
}
// ReadBody read the response data, then return it.
func (r *Response) ReadBody() ([]byte, error) {
if r.body != nil {
return r.body, nil
}
r.body = make([]byte, 0)
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
r.body = b
return r.body, nil
}
// ReadJsonBody read the response data as JSON into v.
func (r *Response) ReadJsonBody(v interface{}) error {
b, err := r.ReadBody()
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
// The predefined API codes
const (
ApiSuccess = "200000"
)
// An ApiResponse represents a API response wrapped Response.
type ApiResponse struct {
response *Response
Code string `json:"code"`
RawData json.RawMessage `json:"data"` // delay parsing
Message string `json:"msg"`
}
// HttpSuccessful judges the success of http.
func (ar *ApiResponse) HttpSuccessful() bool {
return ar.response.StatusCode == http.StatusOK
}
// ApiSuccessful judges the success of API.
func (ar *ApiResponse) ApiSuccessful() bool {
return ar.Code == ApiSuccess
}
// ReadData read the api response `data` as JSON into v.
func (ar *ApiResponse) ReadData(v interface{}) error {
if !ar.HttpSuccessful() {
rsb, _ := ar.response.ReadBody()
m := fmt.Sprintf("[HTTP]Failure: status code is NOT 200, %s %s with body=%s, respond code=%d body=%s",
ar.response.request.Method,
ar.response.request.RequestURI(),
string(ar.response.request.Body),
ar.response.StatusCode,
string(rsb),
)
return errors.New(m)
}
if !ar.ApiSuccessful() {
m := fmt.Sprintf("[API]Failure: api code is NOT %s, %s %s with body=%s, respond code=%s message=\"%s\" data=%s",
ApiSuccess,
ar.response.request.Method,
ar.response.request.RequestURI(),
string(ar.response.request.Body),
ar.Code,
ar.Message,
string(ar.RawData),
)
return errors.New(m)
}
// when input parameter v is nil, read nothing and return nil
if v == nil {
return nil
}
if len(ar.RawData) == 0 {
m := fmt.Sprintf("[API]Failure: try to read empty data, %s %s with body=%s, respond code=%s message=\"%s\" data=%s",
ar.response.request.Method,
ar.response.request.RequestURI(),
string(ar.response.request.Body),
ar.Code,
ar.Message,
string(ar.RawData),
)
return errors.New(m)
}
return json.Unmarshal(ar.RawData, v)
}
// ReadPaginationData read the data `items` as JSON into v, and returns *PaginationModel.
func (ar *ApiResponse) ReadPaginationData(v interface{}) (*PaginationModel, error) {
p := &PaginationModel{}
if err := ar.ReadData(p); err != nil {
return nil, err
}
if err := p.ReadItems(v); err != nil {
return p, err
}
return p, nil
}