From c502a655c8db654aecf0410ecf4c9b451f4c09dd Mon Sep 17 00:00:00 2001 From: Martin Schneppenheim <23424570+weeco@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:06:05 +0200 Subject: [PATCH] sr: add support for using bearer tokens in client --- pkg/sr/client.go | 4 ++++ pkg/sr/clientopt.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/pkg/sr/client.go b/pkg/sr/client.go index 39e52306..c2e9b2ce 100644 --- a/pkg/sr/client.go +++ b/pkg/sr/client.go @@ -60,6 +60,7 @@ type Client struct { user string pass string } + bearerToken string } // NewClient returns a new schema registry client. @@ -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) diff --git a/pkg/sr/clientopt.go b/pkg/sr/clientopt.go index b8d04090..f45a94e6 100644 --- a/pkg/sr/clientopt.go +++ b/pkg/sr/clientopt.go @@ -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) {