Skip to content

Commit 76f7eab

Browse files
authored
Add MIddleware to SDK config (#394)
* add Middleware to config * Improve middleware type documentation * Improve middleware documentation * Update documentation for WithMiddleware * fix linting * improve docs for WithMiddleware
1 parent f9bdea8 commit 76f7eab

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

core/config/config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ type APIKey struct {
6969
Prefix string
7070
}
7171

72+
// Middleware is a function that wraps an http.RoundTripper to provide additional functionality
73+
// such as logging, authentication, etc.
74+
type Middleware func(http.RoundTripper) http.RoundTripper
75+
7276
// Configuration stores the configuration of the API client
7377
type Configuration struct {
7478
Host string `json:"host,omitempty"`
@@ -90,6 +94,7 @@ type Configuration struct {
9094
Servers ServerConfigurations
9195
OperationServers map[string]ServerConfigurations
9296
HTTPClient *http.Client
97+
Middleware []Middleware
9398

9499
// If != nil, a goroutine will be launched that will refresh the service account's access token when it's close to being expired.
95100
// The goroutine is killed whenever this context is canceled.
@@ -284,6 +289,18 @@ func WithJar(jar http.CookieJar) ConfigurationOption {
284289
}
285290
}
286291

292+
// WithMiddleware returns a ConfigurationOption that adds a Middleware to the client.
293+
// The Middleware is prepended to the list of Middlewares so that the last added Middleware is the first to be executed.
294+
// Warning: Providing this option may overide the authentication performed by the SDK if the middlewares provided break the chain.
295+
// If changes are made to the authentication header and the chain is preserved, they will be overwritten. If you wish to overwrite authentication, use WithCustomAuth.
296+
func WithMiddleware(m Middleware) ConfigurationOption {
297+
// Prepend m to the list of middlewares
298+
return func(config *Configuration) error {
299+
config.Middleware = append([]Middleware{m}, config.Middleware...)
300+
return nil
301+
}
302+
}
303+
287304
// WithBackgroundTokenRefresh returns a ConfigurationOption that enables access token refreshing in backgound.
288305
//
289306
// If enabled, a goroutine will be launched that will refresh the service account's access token when it's close to being expired.
@@ -532,3 +549,19 @@ func containsCaseSensitive(haystack []string, needle string) bool {
532549
}
533550
return false
534551
}
552+
553+
// ChainMiddleware chains multiple middlewares to create a single http.RoundTripper
554+
// The middlewares are applied in reverse order, so the last middleware provided in the arguments is the first to be executed
555+
// If the root http.RoundTripper is nil, http.DefaultTransport is used
556+
func ChainMiddleware(rt http.RoundTripper, middlewares ...Middleware) http.RoundTripper {
557+
if rt == nil {
558+
rt = http.DefaultTransport
559+
}
560+
561+
// Apply middlewares in reverse order
562+
for i := len(middlewares) - 1; i >= 0; i-- {
563+
rt = middlewares[i](rt)
564+
}
565+
566+
return rt
567+
}

0 commit comments

Comments
 (0)