-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest.go
251 lines (219 loc) · 5.51 KB
/
request.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
package cast
import (
"net/http"
"time"
)
type profiling struct {
requestStart time.Time
requestDone time.Time
requestCost time.Duration
dnsStart time.Time
dnsDone time.Time
dnsCost time.Duration
connectStart time.Time
connectDone time.Time
connectCost time.Duration
tlsHandshakeStart time.Time
tlsHandshakeDone time.Time
tlsHandshakeCost time.Duration
sendingStart time.Time
sendingDone time.Time
sendingCost time.Duration
waitingStart time.Time
waitingDone time.Time
waitingCost time.Duration
receivingSart time.Time
receivingDone time.Time
receivingCost time.Duration
}
// Request is the http.Request wrapper with attributes.
type Request struct {
path string
method string
header http.Header
queryParam interface{}
pathParam map[string]interface{}
body requestBody
timeout time.Duration
remoteAddress string
prof profiling
rawRequest *http.Request
circuitName string
}
// NewRequest returns an instance of of Request.
func NewRequest() *Request {
return &Request{
method: http.MethodGet,
header: make(http.Header),
pathParam: make(map[string]interface{}),
prof: profiling{
requestStart: time.Now().In(time.UTC),
},
}
}
// WithPath set the relative or absolute path for the http request
// if the base url don't be provided.
func (r *Request) WithPath(path string) *Request {
r.path = path
return r
}
// Options sets the following http request method to "OPTIONS".
func (r *Request) Options() *Request {
r.method = http.MethodOptions
return r
}
// Get sets the following http request method to "GET".
func (r *Request) Get() *Request {
r.method = http.MethodGet
return r
}
// Head sets the following http request method to "HEAD".
func (r *Request) Head() *Request {
r.method = http.MethodHead
return r
}
// Post sets the following http request method to "POST".
func (r *Request) Post() *Request {
r.method = http.MethodPost
return r
}
// Put sets the following http request method to "PUT".
func (r *Request) Put() *Request {
r.method = http.MethodPut
return r
}
// Delete sets the following http request method to "DELETE".
func (r *Request) Delete() *Request {
r.method = http.MethodDelete
return r
}
// Trace sets the following http request method to "TRACE".
func (r *Request) Trace() *Request {
r.method = http.MethodTrace
return r
}
// Connect sets the following http request method to "CONNECT".
func (r *Request) Connect() *Request {
r.method = http.MethodConnect
return r
}
// Patch sets the following http request method to "PATCH".
func (r *Request) Patch() *Request {
r.method = http.MethodPatch
return r
}
// Method sets the following http request method.
func (r *Request) Method(method string) *Request {
r.method = method
return r
}
// WithQueryParam sets query parameters.
func (r *Request) WithQueryParam(queryParam interface{}) *Request {
r.queryParam = queryParam
return r
}
// WithPathParam sets path parameters.
func (r *Request) WithPathParam(pathParam map[string]interface{}) *Request {
r.pathParam = pathParam
return r
}
// WithJSONBody creates body with JSON.
func (r *Request) WithJSONBody(body interface{}) *Request {
r.body = &requestJSONBody{
payload: body,
}
return r
}
// WithXMLBody creates body with XML.
func (r *Request) WithXMLBody(body interface{}) *Request {
r.body = &requestXMLBody{
payload: body,
}
return r
}
// WithPlainBody creates body with plain text.
func (r *Request) WithPlainBody(body string) *Request {
r.body = &requestPlainBody{
payload: body,
}
return r
}
// WithFormURLEncodedBody creates body with url encoded string.
func (r *Request) WithFormURLEncodedBody(body interface{}) *Request {
r.body = &requestFormURLEncodedBody{
payload: body,
}
return r
}
// WithMultipartFormDataBody creates body with form data
func (r *Request) WithMultipartFormDataBody(formData ...*FormData) *Request {
r.body = &requestMultipartFormDataBody{
formData: formData,
}
return r
}
// WithCustomBody creates custom body.
func (r *Request) WithCustomBody(contentType string, body []byte) *Request {
r.body = &requestCustomBody{
payload: body,
contentType: contentType,
}
return r
}
// WithTimeout sets the request timeout.
func (r *Request) WithTimeout(timeout time.Duration) *Request {
r.timeout = timeout
return r
}
// WithHeader replaces the request header.
func (r *Request) WithHeader(header http.Header) *Request {
r.header = header
return r
}
// SetHeader sets the key, value pair list.
func (r *Request) SetHeader(vv ...string) *Request {
if len(vv)%2 != 0 {
return r
}
for i := 0; i < len(vv); i += 2 {
r.header.Set(vv[i], vv[i+1])
}
return r
}
// AddHeader adds the key, value pair list.
func (r *Request) AddHeader(vv ...string) {
if len(vv)%2 != 0 {
return
}
for i := 0; i < len(vv); i += 2 {
r.header.Add(vv[i], vv[i+1])
}
}
// ReqBody returns the underlying body.
func (r *Request) ReqBody() ([]byte, error) {
if r.body == nil {
return []byte{}, nil
}
body, err := r.body.Body()
if err != nil {
return nil, err
}
return body, nil
}
// HeaderExist whether specified header exists.
func (r *Request) HeaderExist(h string) bool {
if r == nil {
return false
}
_, ok := r.header[h]
return ok
}
// WithCircuit is equipped with circuit.
func (r *Request) WithCircuit(name string) *Request {
r.circuitName = name
return r
}
// RawRequest returns the http request.
func (r *Request) RawRequest() *http.Request {
return r.rawRequest
}