From 72bad60e0110accf2c185d6a6fe2248e1bbff566 Mon Sep 17 00:00:00 2001 From: Mihai Todor Date: Tue, 3 Dec 2024 13:07:47 +0000 Subject: [PATCH] Add PreReq option to the sr API Signed-off-by: Mihai Todor --- pkg/sr/client.go | 7 +++++++ pkg/sr/clientopt.go | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/sr/client.go b/pkg/sr/client.go index 1bdc0e21..47cf882a 100644 --- a/pkg/sr/client.go +++ b/pkg/sr/client.go @@ -64,6 +64,7 @@ type Client struct { pass string } bearerToken string + preReq PreReqFunc } // NewClient returns a new schema registry client. @@ -144,6 +145,12 @@ start: } cl.applyParams(ctx, req) + if cl.preReq != nil { + if err := cl.preReq(req); err != nil { + return fmt.Errorf("pre-request hook failed for %s %q: %w", method, reqURL, err) + } + } + resp, err := cl.httpcl.Do(req) if err != nil { if len(urls) == 0 { diff --git a/pkg/sr/clientopt.go b/pkg/sr/clientopt.go index f45a94e6..fe1d23c9 100644 --- a/pkg/sr/clientopt.go +++ b/pkg/sr/clientopt.go @@ -85,6 +85,16 @@ func BearerToken(token string) ClientOpt { }} } +// PreReqFunc is a hook func which mutates a request. +type PreReqFunc func(req *http.Request) error + +// PreReq sets a hook func to call before every request is sent. +func PreReq(preReq PreReqFunc) ClientOpt { + return clientOpt{func(cl *Client) { + cl.preReq = preReq + }} +} + // DefaultParams sets default parameters to apply to every request. func DefaultParams(ps ...Param) ClientOpt { return clientOpt{func(cl *Client) {