File tree 2 files changed +36
-3
lines changed
2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -192,6 +192,12 @@ func UseDefaultParam[T any](dst *param.Field[T], src *T) {
192
192
}
193
193
}
194
194
195
+ // This interface is primarily used to describe an [*http.Client], but also
196
+ // supports custom HTTP implementations.
197
+ type HTTPDoer interface {
198
+ Do (req * http.Request ) (* http.Response , error )
199
+ }
200
+
195
201
// RequestConfig represents all the state related to one request.
196
202
//
197
203
// Editing the variables inside RequestConfig directly is unstable api. Prefer
@@ -202,6 +208,7 @@ type RequestConfig struct {
202
208
Context context.Context
203
209
Request * http.Request
204
210
BaseURL * url.URL
211
+ CustomHTTPDoer HTTPDoer
205
212
HTTPClient * http.Client
206
213
Middlewares []middleware
207
214
APIKey string
@@ -399,6 +406,9 @@ func (cfg *RequestConfig) Execute() (err error) {
399
406
}
400
407
401
408
handler := cfg .HTTPClient .Do
409
+ if cfg .CustomHTTPDoer != nil {
410
+ handler = cfg .CustomHTTPDoer .Do
411
+ }
402
412
for i := len (cfg .Middlewares ) - 1 ; i >= 0 ; i -= 1 {
403
413
handler = applyMiddleware (cfg .Middlewares [i ], handler )
404
414
}
Original file line number Diff line number Diff line change @@ -40,11 +40,34 @@ func WithBaseURL(base string) RequestOption {
40
40
})
41
41
}
42
42
43
- // WithHTTPClient returns a RequestOption that changes the underlying [http.Client] used to make this
43
+ // HTTPClient is primarily used to describe an [*http.Client], but also
44
+ // supports custom implementations.
45
+ //
46
+ // For bespoke implementations, prefer using an [*http.Client] with a
47
+ // custom transport. See [http.RoundTripper] for further information.
48
+ type HTTPClient interface {
49
+ Do (* http.Request ) (* http.Response , error )
50
+ }
51
+
52
+ // WithHTTPClient returns a RequestOption that changes the underlying http client used to make this
44
53
// request, which by default is [http.DefaultClient].
45
- func WithHTTPClient (client * http.Client ) RequestOption {
54
+ //
55
+ // For custom uses cases, it is recommended to provide an [*http.Client] with a custom
56
+ // [http.RoundTripper] as its transport, rather than directly implementing [HTTPClient].
57
+ func WithHTTPClient (client HTTPClient ) RequestOption {
46
58
return requestconfig .RequestOptionFunc (func (r * requestconfig.RequestConfig ) error {
47
- r .HTTPClient = client
59
+ if client == nil {
60
+ return fmt .Errorf ("requestoption: custom http client cannot be nil" )
61
+ }
62
+
63
+ if c , ok := client .(* http.Client ); ok {
64
+ // Prefer the native client if possible.
65
+ r .HTTPClient = c
66
+ r .CustomHTTPDoer = nil
67
+ } else {
68
+ r .CustomHTTPDoer = client
69
+ }
70
+
48
71
return nil
49
72
})
50
73
}
You can’t perform that action at this time.
0 commit comments