Skip to content

Commit

Permalink
Merge pull request #808 from weeco/bearer-token-support-sr
Browse files Browse the repository at this point in the history
sr: add support for using bearer tokens in client
  • Loading branch information
twmb authored Aug 21, 2024
2 parents 56f1b06 + c502a65 commit b77dd13
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/sr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Client struct {
user string
pass string
}
bearerToken string
}

// NewClient returns a new schema registry client.
Expand Down Expand Up @@ -127,6 +128,9 @@ start:
if cl.basicAuth != nil {
req.SetBasicAuth(cl.basicAuth.user, cl.basicAuth.pass)
}
if cl.bearerToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cl.bearerToken))
}
cl.applyParams(ctx, req)

resp, err := cl.httpcl.Do(req)
Expand Down
8 changes: 8 additions & 0 deletions pkg/sr/clientopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func BasicAuth(user, pass string) ClientOpt {
}}
}

// BearerToken sets an Authorization header to use for every request.
// The format will be: "Authorization: Bearer $token".
func BearerToken(token string) ClientOpt {
return clientOpt{func(cl *Client) {
cl.bearerToken = token
}}
}

// DefaultParams sets default parameters to apply to every request.
func DefaultParams(ps ...Param) ClientOpt {
return clientOpt{func(cl *Client) {
Expand Down

0 comments on commit b77dd13

Please sign in to comment.