|
21 | 21 | // options pattern in our [README].
|
22 | 22 | //
|
23 | 23 | // [README]: https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4#readme-requestoptions
|
24 |
| -type RequestOption = func(*requestconfig.RequestConfig) error |
| 24 | +type RequestOption = requestconfig.RequestOption |
25 | 25 |
|
26 | 26 | // WithAPIVersion returns a RequestOption that defines the API version the client is attempting
|
27 | 27 | // to use. While any value can be set here, invalid values are ignored and will recieve the
|
28 | 28 | // default value of today.
|
29 | 29 | func WithAPIVersion(value string) RequestOption {
|
30 | 30 | return func(r *requestconfig.RequestConfig) error {
|
31 | 31 | r.Request.Header.Set("api-version", value)
|
32 | 32 | return nil
|
33 | 33 | }
|
|
39 | 39 | if err != nil {
|
40 | 40 | log.Fatalf("failed to parse BaseURL: %s\n", err)
|
41 | 41 | }
|
42 |
| - return func(r *requestconfig.RequestConfig) error { |
| 42 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
43 | 43 | if u.Path != "" && !strings.HasSuffix(u.Path, "/") {
|
44 | 44 | u.Path += "/"
|
45 | 45 | }
|
46 | 46 | r.BaseURL = u
|
47 | 47 | return nil
|
48 |
| - } |
| 48 | + }) |
49 | 49 | }
|
50 | 50 |
|
51 | 51 | // WithHTTPClient returns a RequestOption that changes the underlying [http.Client] used to make this
|
52 | 52 | // request, which by default is [http.DefaultClient].
|
53 | 53 | func WithHTTPClient(client *http.Client) RequestOption {
|
54 |
| - return func(r *requestconfig.RequestConfig) error { |
| 54 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
55 | 55 | r.HTTPClient = client
|
56 | 56 | return nil
|
57 |
| - } |
| 57 | + }) |
58 | 58 | }
|
59 | 59 |
|
60 | 60 | // MiddlewareNext is a function which is called by a middleware to pass an HTTP request
|
|
69 | 69 | // WithMiddleware returns a RequestOption that applies the given middleware
|
70 | 70 | // to the requests made. Each middleware will execute in the order they were given.
|
71 | 71 | func WithMiddleware(middlewares ...Middleware) RequestOption {
|
72 |
| - return func(r *requestconfig.RequestConfig) error { |
| 72 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
73 | 73 | r.Middlewares = append(r.Middlewares, middlewares...)
|
74 | 74 | return nil
|
75 |
| - } |
| 75 | + }) |
76 | 76 | }
|
77 | 77 |
|
78 | 78 | // WithMaxRetries returns a RequestOption that sets the maximum number of retries that the client
|
|
84 | 84 | if retries < 0 {
|
85 | 85 | panic("option: cannot have fewer than 0 retries")
|
86 | 86 | }
|
87 |
| - return func(r *requestconfig.RequestConfig) error { |
| 87 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
88 | 88 | r.MaxRetries = retries
|
89 | 89 | return nil
|
90 |
| - } |
| 90 | + }) |
91 | 91 | }
|
92 | 92 |
|
93 | 93 | // WithHeader returns a RequestOption that sets the header value to the associated key. It overwrites
|
94 | 94 | // any value if there was one already present.
|
95 | 95 | func WithHeader(key, value string) RequestOption {
|
96 |
| - return func(r *requestconfig.RequestConfig) error { |
| 96 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
97 | 97 | r.Request.Header.Set(key, value)
|
98 | 98 | return nil
|
99 |
| - } |
| 99 | + }) |
100 | 100 | }
|
101 | 101 |
|
102 | 102 | // WithHeaderAdd returns a RequestOption that adds the header value to the associated key. It appends
|
103 | 103 | // onto any existing values.
|
104 | 104 | func WithHeaderAdd(key, value string) RequestOption {
|
105 |
| - return func(r *requestconfig.RequestConfig) error { |
| 105 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
106 | 106 | r.Request.Header.Add(key, value)
|
107 | 107 | return nil
|
108 |
| - } |
| 108 | + }) |
109 | 109 | }
|
110 | 110 |
|
111 | 111 | // WithHeaderDel returns a RequestOption that deletes the header value(s) associated with the given key.
|
112 | 112 | func WithHeaderDel(key string) RequestOption {
|
113 |
| - return func(r *requestconfig.RequestConfig) error { |
| 113 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
114 | 114 | r.Request.Header.Del(key)
|
115 | 115 | return nil
|
116 |
| - } |
| 116 | + }) |
117 | 117 | }
|
118 | 118 |
|
119 | 119 | // WithQuery returns a RequestOption that sets the query value to the associated key. It overwrites
|
120 | 120 | // any value if there was one already present.
|
121 | 121 | func WithQuery(key, value string) RequestOption {
|
122 |
| - return func(r *requestconfig.RequestConfig) error { |
| 122 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
123 | 123 | query := r.Request.URL.Query()
|
124 | 124 | query.Set(key, value)
|
125 | 125 | r.Request.URL.RawQuery = query.Encode()
|
126 | 126 | return nil
|
127 |
| - } |
| 127 | + }) |
128 | 128 | }
|
129 | 129 |
|
130 | 130 | // WithQueryAdd returns a RequestOption that adds the query value to the associated key. It appends
|
131 | 131 | // onto any existing values.
|
132 | 132 | func WithQueryAdd(key, value string) RequestOption {
|
133 |
| - return func(r *requestconfig.RequestConfig) error { |
| 133 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
134 | 134 | query := r.Request.URL.Query()
|
135 | 135 | query.Add(key, value)
|
136 | 136 | r.Request.URL.RawQuery = query.Encode()
|
137 | 137 | return nil
|
138 |
| - } |
| 138 | + }) |
139 | 139 | }
|
140 | 140 |
|
141 | 141 | // WithQueryDel returns a RequestOption that deletes the query value(s) associated with the key.
|
142 | 142 | func WithQueryDel(key string) RequestOption {
|
143 |
| - return func(r *requestconfig.RequestConfig) error { |
| 143 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
144 | 144 | query := r.Request.URL.Query()
|
145 | 145 | query.Del(key)
|
146 | 146 | r.Request.URL.RawQuery = query.Encode()
|
147 | 147 | return nil
|
148 |
| - } |
| 148 | + }) |
149 | 149 | }
|
150 | 150 |
|
151 | 151 | // WithJSONSet returns a RequestOption that sets the body's JSON value associated with the key.
|
152 | 152 | // The key accepts a string as defined by the [sjson format].
|
153 | 153 | //
|
154 | 154 | // [sjson format]: https://github.com/tidwall/sjson
|
155 | 155 | func WithJSONSet(key string, value interface{}) RequestOption {
|
156 |
| - return func(r *requestconfig.RequestConfig) (err error) { |
| 156 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) (err error) { |
157 | 157 | if buffer, ok := r.Body.(*bytes.Buffer); ok {
|
158 | 158 | b := buffer.Bytes()
|
159 | 159 | b, err = sjson.SetBytes(b, key, value)
|
|
165 | 165 | }
|
166 | 166 |
|
167 | 167 | return fmt.Errorf("cannot use WithJSONSet on a body that is not serialized as *bytes.Buffer")
|
168 |
| - } |
| 168 | + }) |
169 | 169 | }
|
170 | 170 |
|
171 | 171 | // WithJSONDel returns a RequestOption that deletes the body's JSON value associated with the key.
|
172 | 172 | // The key accepts a string as defined by the [sjson format].
|
173 | 173 | //
|
174 | 174 | // [sjson format]: https://github.com/tidwall/sjson
|
175 | 175 | func WithJSONDel(key string) RequestOption {
|
176 |
| - return func(r *requestconfig.RequestConfig) (err error) { |
| 176 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) (err error) { |
177 | 177 | if buffer, ok := r.Body.(*bytes.Buffer); ok {
|
178 | 178 | b := buffer.Bytes()
|
179 | 179 | b, err = sjson.DeleteBytes(b, key)
|
|
185 | 185 | }
|
186 | 186 |
|
187 | 187 | return fmt.Errorf("cannot use WithJSONDel on a body that is not serialized as *bytes.Buffer")
|
188 |
| - } |
| 188 | + }) |
189 | 189 | }
|
190 | 190 |
|
191 | 191 | // WithResponseBodyInto returns a RequestOption that overwrites the deserialization target with
|
192 | 192 | // the given destination. If provided, we don't deserialize into the default struct.
|
193 | 193 | func WithResponseBodyInto(dst any) RequestOption {
|
194 |
| - return func(r *requestconfig.RequestConfig) error { |
| 194 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
195 | 195 | r.ResponseBodyInto = dst
|
196 | 196 | return nil
|
197 |
| - } |
| 197 | + }) |
198 | 198 | }
|
199 | 199 |
|
200 | 200 | // WithResponseInto returns a RequestOption that copies the [*http.Response] into the given address.
|
201 | 201 | func WithResponseInto(dst **http.Response) RequestOption {
|
202 |
| - return func(r *requestconfig.RequestConfig) error { |
| 202 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
203 | 203 | r.ResponseInto = dst
|
204 | 204 | return nil
|
205 |
| - } |
| 205 | + }) |
206 | 206 | }
|
207 | 207 |
|
208 | 208 | // WithRequestBody returns a RequestOption that provides a custom serialized body with the given
|
209 | 209 | // content type.
|
210 | 210 | //
|
211 | 211 | // body accepts an io.Reader or raw []bytes.
|
212 | 212 | func WithRequestBody(contentType string, body any) RequestOption {
|
213 |
| - return func(r *requestconfig.RequestConfig) error { |
| 213 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
214 | 214 | if reader, ok := body.(io.Reader); ok {
|
215 | 215 | r.Body = reader
|
216 | 216 | return r.Apply(WithHeader("Content-Type", contentType))
|
|
222 | 222 | }
|
223 | 223 |
|
224 | 224 | return fmt.Errorf("body must be a byte slice or implement io.Reader")
|
225 |
| - } |
| 225 | + }) |
226 | 226 | }
|
227 | 227 |
|
228 | 228 | // WithRequestTimeout returns a RequestOption that sets the timeout for
|
229 | 229 | // each request attempt. This should be smaller than the timeout defined in
|
230 | 230 | // the context, which spans all retries.
|
231 | 231 | func WithRequestTimeout(dur time.Duration) RequestOption {
|
232 |
| - return func(r *requestconfig.RequestConfig) error { |
| 232 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
233 | 233 | r.RequestTimeout = dur
|
234 | 234 | return nil
|
235 |
| - } |
| 235 | + }) |
236 | 236 | }
|
237 | 237 |
|
238 | 238 | // WithEnvironmentProduction returns a RequestOption that sets the current
|
|
244 | 244 |
|
245 | 245 | // WithAPIToken returns a RequestOption that sets the client setting "api_token".
|
246 | 246 | func WithAPIToken(value string) RequestOption {
|
247 |
| - return func(r *requestconfig.RequestConfig) error { |
| 247 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
248 | 248 | r.APIToken = value
|
249 | 249 | return r.Apply(WithHeader("authorization", fmt.Sprintf("Bearer %s", r.APIToken)))
|
250 |
| - } |
| 250 | + }) |
251 | 251 | }
|
252 | 252 |
|
253 | 253 | // WithAPIKey returns a RequestOption that sets the client setting "api_key".
|
254 | 254 | func WithAPIKey(value string) RequestOption {
|
255 |
| - return func(r *requestconfig.RequestConfig) error { |
| 255 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
256 | 256 | r.APIKey = value
|
257 | 257 | return r.Apply(WithHeader("X-Auth-Key", r.APIKey))
|
258 |
| - } |
| 258 | + }) |
259 | 259 | }
|
260 | 260 |
|
261 | 261 | // WithAPIEmail returns a RequestOption that sets the client setting "api_email".
|
262 | 262 | func WithAPIEmail(value string) RequestOption {
|
263 |
| - return func(r *requestconfig.RequestConfig) error { |
| 263 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
264 | 264 | r.APIEmail = value
|
265 | 265 | return r.Apply(WithHeader("X-Auth-Email", r.APIEmail))
|
266 |
| - } |
| 266 | + }) |
267 | 267 | }
|
268 | 268 |
|
269 | 269 | // WithUserServiceKey returns a RequestOption that sets the client setting "user_service_key".
|
270 | 270 | func WithUserServiceKey(value string) RequestOption {
|
271 |
| - return func(r *requestconfig.RequestConfig) error { |
| 271 | + return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error { |
272 | 272 | r.UserServiceKey = value
|
273 | 273 | return r.Apply(WithHeader("X-Auth-User-Service-Key", r.UserServiceKey))
|
274 |
| - } |
| 274 | + }) |
275 | 275 | }
|
0 commit comments