Skip to content

Commit

Permalink
Pass res interface into respHandler
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Nov 21, 2023
1 parent 2349f01 commit f181920
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ type Client interface {
GetMinResolvedTSByStoresIDs(context.Context, []uint64) (uint64, map[uint64]uint64, error)

/* Client-related methods */
WithRespHandler(func(resp *http.Response) error) Client
// WithRespHandler sets and returns a new client with the given HTTP response handler.
// This allows the caller to customize how the response is handled, including error handling logic.
// Additionally, it is important for the caller to handle the content of the response body properly
// in order to ensure that it can be read and marshaled correctly into `res`.
WithRespHandler(func(resp *http.Response, res interface{}) error) Client
Close()
}

Expand All @@ -80,7 +84,7 @@ type client struct {
tlsConf *tls.Config
cli *http.Client

respHandler func(resp *http.Response) error
respHandler func(resp *http.Response, res interface{}) error

requestCounter *prometheus.CounterVec
executionDuration *prometheus.HistogramVec
Expand Down Expand Up @@ -161,7 +165,9 @@ func (c *client) Close() {

// WithRespHandler sets and returns a new client with the given HTTP response handler.
// This allows the caller to customize how the response is handled, including error handling logic.
func (c *client) WithRespHandler(handler func(resp *http.Response) error) Client {
func (c *client) WithRespHandler(
handler func(resp *http.Response, res interface{}) error,
) Client {
newClient := *c
newClient.respHandler = handler
return &newClient
Expand Down Expand Up @@ -231,7 +237,7 @@ func (c *client) request(

// Give away the response handling to the caller if the handler is set.
if c.respHandler != nil {
return c.respHandler(resp)
return c.respHandler(resp, res)
}

defer func() {
Expand Down

0 comments on commit f181920

Please sign in to comment.