Skip to content

Commit

Permalink
sr: add support for using bearer tokens in client
Browse files Browse the repository at this point in the history
  • Loading branch information
weeco committed Aug 20, 2024
1 parent 56f1b06 commit c502a65
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 c502a65

Please sign in to comment.