From f18192004615400e20f1fd3b94606d5113e4ded4 Mon Sep 17 00:00:00 2001 From: JmPotato Date: Tue, 21 Nov 2023 15:39:47 +0800 Subject: [PATCH] Pass res interface into respHandler Signed-off-by: JmPotato --- client/http/client.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/client/http/client.go b/client/http/client.go index 5e438a770760..319ec7a8d9b9 100644 --- a/client/http/client.go +++ b/client/http/client.go @@ -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() } @@ -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 @@ -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 @@ -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() {