Skip to content

Commit

Permalink
feat: expose NewClient options to credentials wrappers (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlnevyhosteny authored Mar 13, 2024
1 parent 8a0358b commit 86b1aff
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions pkg/uhttp/authcredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func NewBearerAuth(token string) *BearerAuth {
}
}

func (b *BearerAuth) GetClient(ctx context.Context) (*http.Client, error) {
httpClient, err := NewClient(ctx, WithLogger(true, nil))
func (b *BearerAuth) GetClient(ctx context.Context, options ...Option) (*http.Client, error) {
httpClient, err := getHttpClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("creating HTTP client failed: %w", err)
return nil, err
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
ts := oauth2.StaticTokenSource(
Expand All @@ -58,10 +58,10 @@ func NewBasicAuth(username, password string) *BasicAuth {
}
}

func (b *BasicAuth) GetClient(ctx context.Context) (*http.Client, error) {
httpClient, err := NewClient(ctx, WithLogger(true, nil))
func (b *BasicAuth) GetClient(ctx context.Context, options ...Option) (*http.Client, error) {
httpClient, err := getHttpClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("creating HTTP client failed: %w", err)
return nil, err
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
auth := b.Username + ":" + b.Password
Expand Down Expand Up @@ -89,10 +89,10 @@ func NewOAuth2ClientCredentials(clientId, clientSecret string, tokenURL *url.URL
}
}

func (o *OAuth2ClientCredentials) GetClient(ctx context.Context) (*http.Client, error) {
httpClient, err := NewClient(ctx, WithLogger(true, nil))
func (o *OAuth2ClientCredentials) GetClient(ctx context.Context, options ...Option) (*http.Client, error) {
httpClient, err := getHttpClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("creating HTTP client failed: %w", err)
return nil, err
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
ts := o.cfg.TokenSource(ctx)
Expand All @@ -117,10 +117,10 @@ func NewOAuth2JWT(credentials []byte, scopes []string, createfn CreateJWTConfig)
}
}

func (o *OAuth2JWT) GetClient(ctx context.Context) (*http.Client, error) {
httpClient, err := NewClient(ctx, WithLogger(true, nil))
func (o *OAuth2JWT) GetClient(ctx context.Context, options ...Option) (*http.Client, error) {
httpClient, err := getHttpClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("creating HTTP client failed: %w", err)
return nil, err
}

jwt, err := o.CreateJWTConfig(o.Credentials, o.Scopes...)
Expand All @@ -134,3 +134,14 @@ func (o *OAuth2JWT) GetClient(ctx context.Context) (*http.Client, error) {

return httpClient, nil
}

func getHttpClient(ctx context.Context, options ...Option) (*http.Client, error) {
options = append(options, WithLogger(true, nil))

httpClient, err := NewClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("creating HTTP client failed: %w", err)
}

return httpClient, nil
}

0 comments on commit 86b1aff

Please sign in to comment.