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 <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Nov 21, 2023
1 parent 2349f01 commit 8d10a15
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 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 @@ -160,8 +164,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 {

Check warning on line 169 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L169

Added line #L169 was not covered by tests
newClient := *c
newClient.respHandler = handler
return &newClient
Expand Down Expand Up @@ -231,7 +236,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)

Check warning on line 239 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L239

Added line #L239 was not covered by tests
}

defer func() {
Expand Down

0 comments on commit 8d10a15

Please sign in to comment.