-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
75 lines (63 loc) · 1.78 KB
/
options.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
package rclient
import (
"net/http"
"net/url"
)
// A ClientOption configures a *RestClient.
type ClientOption func(client *RestClient)
// Builder sets the RequestBuilder field of a RestClient.
func Builder(builder RequestBuilder) ClientOption {
return func(r *RestClient) {
r.RequestBuilder = builder
}
}
// Doer sets the RequestDoer field of a RestClient.
func Doer(doer RequestDoer) ClientOption {
return func(r *RestClient) {
r.RequestDoer = doer
}
}
// Reader sets the ResponseReader field of a RestClient.
func Reader(reader ResponseReader) ClientOption {
return func(r *RestClient) {
r.ResponseReader = reader
}
}
// RequestOptions sets the RequestOptions field of a RestClient.
func RequestOptions(options ...RequestOption) ClientOption {
return func(r *RestClient) {
r.RequestOptions = append(r.RequestOptions, options...)
}
}
// A RequestOption configures a *http.Request.
type RequestOption func(req *http.Request) error
// BasicAuth adds the specified username and password as basic auth to a request.
func BasicAuth(user, pass string) RequestOption {
return func(req *http.Request) error {
req.SetBasicAuth(user, pass)
return nil
}
}
// Header adds the specified name and value as a header to a request.
func Header(name, val string) RequestOption {
return func(req *http.Request) error {
req.Header.Add(name, val)
return nil
}
}
// Headers adds the specified names and values as headers to a request
func Headers(headers map[string]string) RequestOption {
return func(req *http.Request) error {
for name, val := range headers {
req.Header.Add(name, val)
}
return nil
}
}
// Query adds the specified query to a request.
func Query(query url.Values) RequestOption {
return func(req *http.Request) error {
req.URL.RawQuery = query.Encode()
return nil
}
}