-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
125 lines (107 loc) · 3.21 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
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
package surf
import (
"context"
"net/http"
"net/url"
"time"
)
// WithRequestConfig is a function signature for configuring request options.
type WithRequestConfig func(c *RequestConfig)
type WithRequestConfigChain []WithRequestConfig
// WithBody sets the request body in the request configuration.
func WithBody(body interface{}) WithRequestConfig {
return func(c *RequestConfig) {
c.Body = body
}
}
// WithBaseURL sets the BaseURL parameters in the request configuration.
func WithBaseURL(url string) WithRequestConfig {
return func(c *RequestConfig) {
c.BaseURL = url
}
}
// WithHeader sets the request header in the request configuration.
func WithHeader(header http.Header) WithRequestConfig {
return func(c *RequestConfig) {
c.Header = header
}
}
// WithQuery sets the query parameters in the request configuration.
func WithQuery(values url.Values) WithRequestConfig {
return func(c *RequestConfig) {
c.Query = values
}
}
// WithParams sets the parameters in the request configuration.
func WithParams(params map[string]string) WithRequestConfig {
return func(c *RequestConfig) {
c.Params = params
}
}
// WithCookies sets the cookies in the request configuration.
func WithCookies(cookies []*http.Cookie) WithRequestConfig {
return func(c *RequestConfig) {
c.Cookies = cookies
}
}
// WithContext sets the context in the request configuration.
func WithContext(ctx context.Context) WithRequestConfig {
return func(c *RequestConfig) {
c.Context = ctx
}
}
// WithTimeoutContext sets the context and timeout in the request configuration.
func WithTimeoutContext(ctx context.Context, timeout time.Duration) WithRequestConfig {
return func(c *RequestConfig) {
c.Context = ctx
c.Timeout = timeout
}
}
// WithSetQuery adds a query parameter in the request configuration.
func WithSetQuery(key, value string) WithRequestConfig {
return func(c *RequestConfig) {
c.SetQuery(key, value)
}
}
// WithSetParam adds a parameter in the request configuration.
func WithSetParam(key, value string) WithRequestConfig {
return func(c *RequestConfig) {
c.SetParam(key, value)
}
}
// WithSetHeader adds a header in the request configuration.
func WithSetHeader(headers http.Header) WithRequestConfig {
return func(c *RequestConfig) {
for k, l := range headers {
for _, v := range l {
c.SetHeader(k, v)
}
}
}
}
// WithSetCookie adds a cookie in the request configuration.
func WithSetCookie(cookie *http.Cookie) WithRequestConfig {
return func(c *RequestConfig) {
c.SetCookie(cookie)
}
}
// WithRequestInterceptor append RequestInterceptor in the request configuration.
func WithRequestInterceptor(handler RequestInterceptor) WithRequestConfig {
return func(c *RequestConfig) {
c.AppendRequestInterceptors(handler)
}
}
// WithResponseInterceptor append ResponseInterceptor in the request configuration.
func WithResponseInterceptor(handler ResponseInterceptor) WithRequestConfig {
return func(c *RequestConfig) {
c.AppendResponseInterceptors(handler)
}
}
// combineRequestConfig combines multiple request configurations into a single configuration.
func combineRequestConfig(args ...WithRequestConfig) RequestConfig {
config := RequestConfig{}
for _, arg := range args {
arg(&config)
}
return config
}