Skip to content

Commit 820ffe8

Browse files
feat(client): add support for endpoint-specific base URLs in python
This also changes how `WithEnvironmentProduction()` and `WithBaseURL()` interact. Previously, `WithBaseURL()` would be overridden by `WithEnvironmentProduction()` if `WithEnvironmentProduction()` was later in the list of options; now, `WithBaseURL()` always takes precedence.
1 parent d608b57 commit 820ffe8

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

internal/requestconfig/requestconfig.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ type RequestConfig struct {
208208
Context context.Context
209209
Request *http.Request
210210
BaseURL *url.URL
211+
// DefaultBaseURL will be used if BaseURL is not explicitly overridden using
212+
// WithBaseURL.
213+
DefaultBaseURL *url.URL
211214
CustomHTTPDoer HTTPDoer
212215
HTTPClient *http.Client
213216
Middlewares []middleware
@@ -374,7 +377,11 @@ func retryDelay(res *http.Response, retryCount int) time.Duration {
374377

375378
func (cfg *RequestConfig) Execute() (err error) {
376379
if cfg.BaseURL == nil {
377-
return fmt.Errorf("requestconfig: base url is not set")
380+
if cfg.DefaultBaseURL != nil {
381+
cfg.BaseURL = cfg.DefaultBaseURL
382+
} else {
383+
return fmt.Errorf("requestconfig: base url is not set")
384+
}
378385
}
379386

380387
cfg.Request.URL, err = cfg.BaseURL.Parse(strings.TrimLeft(cfg.Request.URL.String(), "/"))
@@ -608,3 +615,17 @@ func PreRequestOptions(opts ...RequestOption) (RequestConfig, error) {
608615
}
609616
return cfg, nil
610617
}
618+
619+
// WithDefaultBaseURL returns a RequestOption that sets the client's default Base URL.
620+
// This is always overridden by setting a base URL with WithBaseURL.
621+
// WithBaseURL should be used instead of WithDefaultBaseURL except in internal code.
622+
func WithDefaultBaseURL(baseURL string) RequestOption {
623+
u, err := url.Parse(baseURL)
624+
return RequestOptionFunc(func(r *RequestConfig) error {
625+
if err != nil {
626+
return err
627+
}
628+
r.DefaultBaseURL = u
629+
return nil
630+
})
631+
}

option/requestoption.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func WithRequestTimeout(dur time.Duration) RequestOption {
262262
// environment to be the "production" environment. An environment specifies which base URL
263263
// to use by default.
264264
func WithEnvironmentProduction() RequestOption {
265-
return WithBaseURL("https://api.openlayer.com/v1/")
265+
return requestconfig.WithDefaultBaseURL("https://api.openlayer.com/v1/")
266266
}
267267

268268
// WithAPIKey returns a RequestOption that sets the client setting "api_key".

0 commit comments

Comments
 (0)