-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrequest.go
299 lines (242 loc) · 6.49 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
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
package gohttp
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// Request is a request type
type Request struct {
transport *http.Transport
client *http.Client
cookie http.CookieJar
timeout time.Duration
formVals *bytes.Buffer
multipartBuffer bytes.Buffer
queryVals string
headers map[string]string
writer *multipart.Writer
contentType string
basicUser, basicPasswd string
}
// MultipartParam is a multipart param type
type MultipartParam struct {
FieldName string
FileName string
FileBody io.Reader
}
// NewRequest returns a new request
func NewRequest(opts ...Option) *Request {
r := &Request{}
for _, o := range opts {
o.apply(r)
}
return r
}
// createClient create request client
func (req *Request) createClient() *http.Client {
tr := req.transport
if tr == nil {
tr = http.DefaultTransport.(*http.Transport)
}
if req.client == nil {
req.client = &http.Client{
Transport: tr,
Timeout: req.timeout,
Jar: req.cookie,
}
}
return req.client
}
// JSON set json data with request
func (req *Request) JSON(jsonBody map[string]interface{}) *Request {
data, err := json.Marshal(jsonBody)
if err != nil {
panic(err)
}
req.formVals = bytes.NewBuffer(data)
req.contentType = "application/json"
return req
}
// FormData set Post request form parameters
func (req *Request) FormData(formValues map[string]string) *Request {
vals := url.Values{}
for key, val := range formValues {
vals.Add(key, val)
}
req.formVals = bytes.NewBuffer([]byte(vals.Encode()))
req.contentType = "application/x-www-form-urlencoded"
return req
}
// Body set Post request as body
func (req *Request) Body(formValues []byte) *Request {
req.formVals = bytes.NewBuffer(formValues)
req.contentType = "application/octet-stream"
return req
}
// Text is send text data with post request
func (req *Request) Text(formValues string) *Request {
req.formVals = bytes.NewBuffer([]byte(formValues))
req.contentType = "text/plain"
return req
}
// Query set request query param
func (req *Request) Query(formValues map[string]string) *Request {
vals := url.Values{}
for key, val := range formValues {
vals.Add(key, val)
}
req.queryVals = vals.Encode()
req.contentType = "application/x-www-form-urlencoded"
return req
}
// Headers set header information
func (req *Request) Headers(headerVals map[string]string) *Request {
req.headers = headerVals
return req
}
// BasicAuth make basic authentication
func (req *Request) BasicAuth(username, password string) *Request {
req.basicUser = username
req.basicPasswd = password
return req
}
// Get is a get http request
func (req *Request) Get(url string) (*Response, error) {
return req.makeRequest(http.MethodGet, url, req.formVals)
}
// Post is a post http request
func (req *Request) Post(url string) (*Response, error) {
return req.makeRequest(http.MethodPost, url, req.formVals)
}
// Put is a put http request
func (req *Request) Put(url string) (*Response, error) {
return req.makeRequest(http.MethodPut, url, req.formVals)
}
// Patch is a patch http request
func (req *Request) Patch(url string) (*Response, error) {
return req.makeRequest(http.MethodPatch, url, req.formVals)
}
// Delete is a delete http request
func (req *Request) Delete(url string) (*Response, error) {
return req.makeRequest(http.MethodDelete, url, req.formVals)
}
// Head is a head http request
func (req *Request) Head(url string) (*Response, error) {
return req.makeRequest(http.MethodHead, url, req.formVals)
}
// Options is a options http request
func (req *Request) Options(url string) (*Response, error) {
return req.makeRequest(http.MethodOptions, url, req.formVals)
}
// MultipartFormData add form data in multipart request
func (req *Request) MultipartFormData(formData map[string]string) *Request {
if req.writer == nil {
req.writer = multipart.NewWriter(&req.multipartBuffer)
}
for key, val := range formData {
req.writer.WriteField(key, val)
}
return req
}
// Upload upload a single file
func (req *Request) Upload(name, file string) *Request {
if req.writer == nil {
req.writer = multipart.NewWriter(&req.multipartBuffer)
}
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
// Add file
fw, err := req.writer.CreateFormFile(name, file)
if err != nil {
panic(err)
}
if _, err = io.Copy(fw, f); err != nil {
panic(err)
}
req.contentType = req.writer.FormDataContentType()
req.formVals = &req.multipartBuffer
return req
}
// UploadFromReader upload a single file
func (req *Request) UploadFromReader(param MultipartParam) *Request {
if req.writer == nil {
req.writer = multipart.NewWriter(&req.multipartBuffer)
}
// Add file
fw, err := req.writer.CreateFormFile(param.FieldName, param.FileName)
if err != nil {
panic(err)
}
if _, err = io.Copy(fw, param.FileBody); err != nil {
panic(err)
}
req.contentType = req.writer.FormDataContentType()
req.formVals = &req.multipartBuffer
return req
}
// Uploads upload multiple files
func (req *Request) Uploads(files map[string]string) *Request {
for name, file := range files {
_ = req.Upload(name, file)
}
return req
}
// UploadsFromReader upload multiple files
func (req *Request) UploadsFromReader(params []MultipartParam) *Request {
for _, param := range params {
_ = req.UploadFromReader(param)
}
return req
}
// makeRequest makes a http request
func (req *Request) makeRequest(verb, url string, payloads *bytes.Buffer) (*Response, error) {
response := Response{}
verb = strings.ToUpper(verb)
var request *http.Request
var err error
client := req.createClient()
if req.writer != nil {
req.writer.Close()
}
if req.queryVals != "" {
url += "?" + req.queryVals
}
if payloads == nil {
payloads = bytes.NewBuffer([]byte(``))
}
if verb == "GET" {
request, err = http.NewRequest(verb, url, nil)
} else {
request, err = http.NewRequest(verb, url, payloads)
}
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", req.contentType)
if req.basicUser != "" && req.basicPasswd != "" {
request.SetBasicAuth(req.basicUser, req.basicPasswd)
}
// set headers from Headers method
for key, val := range req.headers {
request.Header.Set(key, val)
}
if val,ok := req.headers["Host"]; ok {
request.Host = val
}
//request.Close = true
resp, err := client.Do(request)
if err != nil {
return nil, err
}
response.resp = resp
return &response, nil
}