-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathoptions.go
More file actions
222 lines (193 loc) · 6.21 KB
/
options.go
File metadata and controls
222 lines (193 loc) · 6.21 KB
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
package grequests
import (
"context"
"io"
"net"
"net/http"
"net/url"
"time"
)
// Option is the base type we use to apply request options
type Option interface {
Apply(*RequestOptions)
}
type optionFunc func(*RequestOptions)
func (o optionFunc) Apply(r *RequestOptions) {
o(r)
}
// FromRequestOptions is a function that you can use to upgrade your
// requests
func FromRequestOptions(r *RequestOptions) Option {
return optionFunc(func(ro *RequestOptions) {
*ro = *r
})
}
// UserAgent sets the value of a requests user agent
func UserAgent(value string) Option {
return optionFunc(func(ro *RequestOptions) {
ro.UserAgent = value
})
}
// Files is where you can include files to upload. The use of this data
// structure is limited to POST requests
func Files(f []FileUpload) Option {
return optionFunc(func(ro *RequestOptions) {
ro.Files = f
})
}
// JSON can be used when you wish to send JSON within the request body
func JSON(json interface{}) Option {
return optionFunc(func(ro *RequestOptions) {
ro.JSON = json
})
}
// XML can be used if you wish to send XML within the request body
func XML(xml interface{}) Option {
return optionFunc(func(ro *RequestOptions) {
ro.XML = xml
})
}
// DisableTLSCertValidation is a flag that specifies if we should validate the
// server's TLS certificate. It should be noted that Go's TLS verify mechanism
// doesn't validate if a certificate has been revoked
func DisableTLSCertValidation() Option {
return optionFunc(func(ro *RequestOptions) {
ro.InsecureSkipVerify = true
})
}
// DisableCompression will disable gzip compression on requests
func DisableCompression() Option {
return optionFunc(func(ro *RequestOptions) {
ro.DisableCompression = true
})
}
// Host allows you to set an arbitrary host
func Host(host string) Option {
return optionFunc(func(ro *RequestOptions) {
ro.Host = host
})
}
// BasicAuth allows you to specify a user name and password that you wish to
// use when requesting the URL. It will use basic HTTP authentication
// formatting the username and password in base64.
func BasicAuth(username, password string) Option {
return optionFunc(func(ro *RequestOptions) {
ro.Auth = []string{username, password}
})
}
// IsAJAX is a flag that can be set to make the request appear
// to be generated by browser Javascript
func IsAJAX() Option {
return optionFunc(func(ro *RequestOptions) {
ro.IsAjax = true
})
}
// Cookies is an array of `http.Cookie` that allows you to attach
// cookies to your request
func Cookies(cookies []*http.Cookie) Option {
return optionFunc(func(ro *RequestOptions) {
ro.Cookies = cookies
})
}
// UseCookieJar will create a custom HTTP client that will
// process and store HTTP cookies when they are sent down
func UseCookieJar() Option {
return optionFunc(func(ro *RequestOptions) {
ro.UseCookieJar = true
})
}
// Proxies is a map in the following format
// *protocol* => proxy address e.g http => http://127.0.0.1:8080
func Proxies(proxies map[string]*url.URL) Option {
return optionFunc(func(ro *RequestOptions) {
ro.Proxies = proxies
})
}
// TLSHandshakeTimeout specifies the maximum amount of time waiting to
// wait for a TLS handshake. Zero means no timeout.
func TLSHandshakeTimeout(timeout time.Duration) Option {
return optionFunc(func(ro *RequestOptions) {
ro.TLSHandshakeTimeout = timeout
})
}
// DialTimeout is the maximum amount of time a dial will wait for
// a connect to complete.
func DialTimeout(timeout time.Duration) Option {
return optionFunc(func(ro *RequestOptions) {
ro.DialTimeout = timeout
})
}
// DialKeepAlive specifies the keep-alive period for an active
// network connection. If zero, keep-alive are not enabled.
func DialKeepAlive(timeout time.Duration) Option {
return optionFunc(func(ro *RequestOptions) {
ro.DialKeepAlive = timeout
})
}
// RequestTimeout is the maximum amount of time a whole request(include dial /
// request / redirect) will wait.
func RequestTimeout(timeout time.Duration) Option {
return optionFunc(func(ro *RequestOptions) {
ro.RequestTimeout = timeout
})
}
// HTTPClient can be provided if you wish to supply a custom HTTP client
// this is useful if you want to use an OAUTH client with your request.
func HTTPClient(client *http.Client) Option {
return optionFunc(func(ro *RequestOptions) {
ro.HTTPClient = client
})
}
// SensitiveHTTPHeaders is a map of sensitive HTTP headers that a user
// doesn't want passed on a redirect.
func SensitiveHTTPHeaders(headers ...string) Option {
return optionFunc(func(ro *RequestOptions) {
m := map[string]struct{}{}
for _, h := range headers {
m[http.CanonicalHeaderKey(h)] = struct{}{}
}
ro.SensitiveHTTPHeaders = m
})
}
// RedirectLimit is the acceptable amount of redirects that we should expect
// before returning an error be default this is set to 30. You can change this
// globally by modifying the `RedirectLimit` variable.
func RedirectLimit(limit int) Option {
return optionFunc(func(ro *RequestOptions) {
ro.RedirectLimit = limit
})
}
// RequestBody allows you to put anything matching an `io.Reader` into the request
// this option will take precedence over any other request option specified
func RequestBody(requestBody io.Reader) Option {
return optionFunc(func(ro *RequestOptions) {
ro.RequestBody = requestBody
})
}
// CookieJar allows you to specify a special cookiejar to use with your request.
// this option will take precedence over the `UseCookieJar` option above.
func CookieJar(jar http.CookieJar) Option {
return optionFunc(func(ro *RequestOptions) {
ro.CookieJar = jar
})
}
// Context can be used to maintain state between requests https://golang.org/pkg/context/#Context
func Context(ctx context.Context) Option {
return optionFunc(func(ro *RequestOptions) {
ro.Context = ctx
})
}
// BeforeRequest is a hook that can be used to modify the request object
// before the request has been fired. This is useful for adding authentication
// and other functionality not provided in this library
func BeforeRequest(f func(req *http.Request) error) Option {
return optionFunc(func(ro *RequestOptions) {
ro.BeforeRequest = f
})
}
// LocalAddr allows you to send the request on any local interface
func LocalAddr(addr *net.TCPAddr) Option {
return optionFunc(func(ro *RequestOptions) {
ro.LocalAddr = addr
})
}