Skip to content

Commit 4258c83

Browse files
committed
chore(oauth): refactor OAuth handler to support functional configuration options
- Refactor NewOAuthHandler to accept functional options for configuration - Add support for injecting a custom HTTP client via WithOAuthHTTPClient option fix #440 Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 8891432 commit 4258c83

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

client/transport/oauth.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,34 @@ type OAuthHandler struct {
146146
expectedState string // Expected state value for CSRF protection
147147
}
148148

149-
// NewOAuthHandler creates a new OAuth handler
150-
func NewOAuthHandler(config OAuthConfig) *OAuthHandler {
149+
type OAuthHandlerOption func(*OAuthHandler)
150+
151+
// WithOAuthHTTPClient allows setting a custom http.Client for the OAuthHandler.
152+
func WithOAuthHTTPClient(client *http.Client) OAuthHandlerOption {
153+
return func(h *OAuthHandler) {
154+
if client != nil {
155+
h.httpClient = client
156+
}
157+
}
158+
}
159+
160+
// NewOAuthHandler creates a new OAuth handler.
161+
// Optionally accepts functional options such as WithOAuthHTTPClient.
162+
func NewOAuthHandler(config OAuthConfig, opts ...OAuthHandlerOption) *OAuthHandler {
151163
if config.TokenStore == nil {
152164
config.TokenStore = NewMemoryTokenStore()
153165
}
154166

155-
return &OAuthHandler{
167+
handler := &OAuthHandler{
156168
config: config,
157169
httpClient: &http.Client{Timeout: 30 * time.Second},
158170
}
171+
172+
for _, opt := range opts {
173+
opt(handler)
174+
}
175+
176+
return handler
159177
}
160178

161179
// GetAuthorizationHeader returns the Authorization header value for a request

0 commit comments

Comments
 (0)