-
Notifications
You must be signed in to change notification settings - Fork 4
/
requests.go
231 lines (191 loc) · 4.49 KB
/
requests.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
package requests
//import "fmt"
import "time"
import "strings"
import "net/url"
import "net/http"
import "io/ioutil"
import "encoding/json"
type M map[string]interface{}
type Request struct {
Url string
Args M
}
type Response struct {
Content string
}
func (r *Request) initArgs() {
if r.Args == nil {
r.Args = M{}
}
}
func (r *Request) setTimeout(timeout int) error {
r.Args["timeout"] = int(timeout)
return nil
}
func Timeout(timeout int) func(*Request) error {
return func(r *Request) error {
return r.setTimeout(timeout)
}
}
func (r *Request) setProxies(proxy string) error {
r.Args["proxy"] = proxy
return nil
}
func Proxies(proxy string) func(*Request) error {
return func(r *Request) error {
return r.setProxies(proxy)
}
}
func (r *Request) setCookies(cookies map[string]string) error {
r.Args["cookies"] = cookies
return nil
}
func Cookies(cookies map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setCookies(cookies)
}
}
func (r *Request) setHeaders(headers map[string]string) error {
r.Args["headers"] = headers
return nil
}
func Headers(headers map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setHeaders(headers)
}
}
func (r *Request) setParams(params map[string]string) error {
r.Args["params"] = params
return nil
}
func Params(params map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setParams(params)
}
}
func (r *Request) setForm(form map[string]string) error {
r.Args["form"] = form
return nil
}
func Form(form map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setForm(form)
}
}
func (r *Request) setData(data map[string]string) error {
r.Args["data"] = data
return nil
}
func Data(data map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setData(data)
}
}
func (r *Request) setBin(bin map[string]string) error {
r.Args["bin"] = bin
return nil
}
func Bin(bin map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setBin(bin)
}
}
func (r *Request) setJson(json map[string]string) error {
r.Args["json"] = json
return nil
}
func Json(json map[string]string) func(*Request) error {
return func(r *Request) error {
return r.setJson(json)
}
}
func (r *Request) setOptions(options M) error {
for k, v := range options {
r.Args[k] = v
}
return nil
}
func Options(options M) func(*Request) error {
return func(r *Request) error {
return r.setOptions(options)
}
}
func (r *Request) Options(uri string, options ...func(*Request) error) (*Response, error) {
return r.MakeRequest("Options", uri)
}
func (r *Request) MakeRequest(method string, uri string, options ...func(*Request) error) (*Response, error) {
r.initArgs()
for _, option := range options {
err := option(r)
if err != nil {
panic(err)
}
}
payload := ""
if data, ok := r.Args["form"].(map[string]string); ok {
var req http.Request
req.ParseForm()
for key, val := range data {
req.Form.Add(key, val)
}
payload = strings.TrimSpace(req.Form.Encode())
}
if data, ok := r.Args["data"].(map[string]string); ok {
body, err := json.Marshal(data)
if err == nil {
payload = string(body)
}
}
req, err := http.NewRequest(method, uri, strings.NewReader(payload))
req.Close = true
if params, ok := r.Args["params"].(map[string]string); ok {
q := req.URL.Query()
for key, val := range params {
q.Add(key, val)
}
req.URL.RawQuery = q.Encode()
}
if headers, ok := r.Args["headers"].(map[string]string); ok {
for key, val := range headers {
req.Header.Set(key, val)
}
}
if cookies, ok := r.Args["cookies"].(map[string]string); ok {
for key, val := range cookies {
cookie := &http.Cookie{Name: key, Value: val, HttpOnly: false}
req.AddCookie(cookie)
}
}
transport := &http.Transport{}
if proxy, ok := r.Args["proxy"].(string); ok {
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err == nil {
transport.Proxy = http.ProxyURL(proxyUrl)
}
}
}
client := &http.Client{
Transport: transport,
}
timeoutSeconds := r.Args["timeout"].(int)
timeout := time.Duration(0) * time.Second
if timeoutSeconds > 0 {
timeout = time.Duration(timeoutSeconds) * time.Second
}
client.Timeout = timeout
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
response := &Response{}
if err != nil {
panic(err)
} else {
response.Content = string(body)
}
return response, nil
}