-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
291 lines (256 loc) · 5.56 KB
/
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
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
package unirest
import (
"bytes"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"reflect"
"strings"
"unsafe"
)
type HTTPClient struct {
query url.Values
form url.Values
files []*fileField
url string
path string
body []byte
method string
header http.Header
basicAuth [2]string
makeCopy bool
}
type fileField struct {
key string
filename string
content []byte
}
func New() *HTTPClient {
return &HTTPClient{
query: url.Values{},
form: url.Values{},
method: "GET",
header: http.Header{},
files: make([]*fileField, 0),
makeCopy: true,
}
}
func (c *HTTPClient) AutoClone(b bool) *HTTPClient {
c = c.Clone()
c.makeCopy = b
return c
}
func (c *HTTPClient) SetURL(url string) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.url = strings.TrimRight(url, "/")
return c
}
func (c *HTTPClient) AppendPath(path string) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
if path == "" {
return c
}
if path[0] != '/' {
path = "/" + path
}
path = strings.TrimRight(path, "/")
c.path += path
return c
}
func (c *HTTPClient) AddQuery(key, value string) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.query.Add(key, value)
return c
}
func (c *HTTPClient) AddHeader(key, value string) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.header.Add(key, value)
return c
}
func (c *HTTPClient) AddFormField(key, value string) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.form.Add(key, value)
c.method = "POST"
return c
}
func (c *HTTPClient) AddFile(key, filename string, content []byte) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.files = append(c.files, &fileField{
key: key,
filename: filename,
content: content,
})
c.method = "POST"
return c
}
func (c *HTTPClient) SetBasicAuth(username, password string) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.basicAuth[0] = username
c.basicAuth[1] = password
return c
}
func (c *HTTPClient) SetJSONBody(json []byte) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.body = json
c.header.Set("Content-Type", "application/json")
c.method = "POST"
return c
}
func (c *HTTPClient) SetRawBody(body []byte) *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.body = body
c.method = "POST"
c.header.Del("Content-Type")
return c
}
func (c *HTTPClient) Get() *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.method = "GET"
return c
}
func (c *HTTPClient) Post() *HTTPClient {
if c.makeCopy {
c = c.Clone()
}
c.method = "POST"
return c
}
func (c *HTTPClient) Send() *Response {
req, err := c.ParseRequest()
if err != nil {
return &Response{Err: err}
}
var httpclient http.Client
resp, err := httpclient.Do(req)
if err != nil {
return &Response{Err: err}
}
return &Response{Response: resp}
}
func (c *HTTPClient) ParseRequest() (*http.Request, error) {
req := &http.Request{
Method: c.method,
Header: c.header,
}
req.Header.Set("User-Agent", "Unirest-Go/1.0")
if c.basicAuth[0] != "" {
req.SetBasicAuth(c.basicAuth[0], c.basicAuth[1])
}
u, err := url.Parse(c.url + c.path)
if err != nil {
return nil, err
}
req.URL = u
if len(c.query) != 0 {
req.URL.RawQuery = c.query.Encode()
}
if c.body != nil && (len(c.files) != 0 || len(c.form) != 0) {
return nil, errors.New("unirest-go: can send this request with multiple content type")
}
var reader *bytes.Reader
if c.body != nil {
reader = bytes.NewReader(c.body)
} else if len(c.files) != 0 {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, file := range c.files {
fw, err := writer.CreateFormFile(file.key, file.filename)
if err != nil {
return nil, err
}
_, err = fw.Write(file.content)
if err != nil {
return nil, err
}
}
for key, values := range c.form {
for _, value := range values {
err := writer.WriteField(key, value)
if err != nil {
return nil, err
}
}
}
err := writer.Close()
if err != nil {
return nil, err
}
reader = bytes.NewReader(body.Bytes())
req.Header.Set("Content-Type", writer.FormDataContentType())
} else if len(c.form) != 0 {
reader = bytes.NewReader(s2b(c.form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
if reader != nil {
readCloser := ioutil.NopCloser(reader)
req.Body = readCloser
req.ContentLength = int64(reader.Len())
snapshot := *reader
req.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return ioutil.NopCloser(&r), nil
}
}
return req, nil
}
func (c *HTTPClient) Clone() *HTTPClient {
clone := *c
clone.query = copyMap(c.query)
clone.header = copyMap(c.header)
clone.form = copyMap(c.form)
return &clone
}
func copyMap(m map[string][]string) map[string][]string {
clone := map[string][]string{}
for key, values := range m {
clone[key] = make([]string, len(values))
copy(clone[key], values)
}
return clone
}
// b2s converts byte slice to a string without memory allocation.
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func b2s(b []byte) string {
/* #nosec G103 */
return *(*string)(unsafe.Pointer(&b))
}
// s2b converts string to a byte slice without memory allocation.
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func s2b(s string) (b []byte) {
/* #nosec G103 */
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
/* #nosec G103 */
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
}