Skip to content

Commit

Permalink
Add PreReq option to the sr API
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Todor <[email protected]>
  • Loading branch information
mihaitodor committed Dec 3, 2024
1 parent 293b7c4 commit 72bad60
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/sr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Client struct {
pass string
}
bearerToken string
preReq PreReqFunc
}

// NewClient returns a new schema registry client.
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions pkg/sr/clientopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 72bad60

Please sign in to comment.