Skip to content

Commit 986808a

Browse files
authored
add types for middleware (#341)
1 parent 8a29e30 commit 986808a

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

Diff for: client.go

+31-14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ var (
6666
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
6767
)
6868

69+
type (
70+
// RequestMiddleware type is for request middleware, called before a request is sent
71+
RequestMiddleware func(*Client, *Request) error
72+
73+
// ResponseMiddleware type is for response middleware, called after a response has been received
74+
ResponseMiddleware func(*Client, *Response) error
75+
76+
// PreRequestHook type is for the request hook, called right before the request is sent
77+
PreRequestHook func(*Client, *http.Request) error
78+
79+
// RequestLogCallback type is for request logs, called before the request is logged
80+
RequestLogCallback func(*RequestLog) error
81+
82+
// ResponseLogCallback type is for response logs, called before the response is logged
83+
ResponseLogCallback func(*ResponseLog) error
84+
)
85+
6986
// Client struct is used to create Resty client with client level settings,
7087
// these settings are applicable to all the request raised from the client.
7188
//
@@ -104,12 +121,12 @@ type Client struct {
104121
log Logger
105122
httpClient *http.Client
106123
proxyURL *url.URL
107-
beforeRequest []func(*Client, *Request) error
108-
udBeforeRequest []func(*Client, *Request) error
109-
preReqHook func(*Client, *http.Request) error
110-
afterResponse []func(*Client, *Response) error
111-
requestLog func(*RequestLog) error
112-
responseLog func(*ResponseLog) error
124+
beforeRequest []RequestMiddleware
125+
udBeforeRequest []RequestMiddleware
126+
preReqHook PreRequestHook
127+
afterResponse []ResponseMiddleware
128+
requestLog RequestLogCallback
129+
responseLog ResponseLogCallback
113130
}
114131

115132
// User type is to hold an username and password information
@@ -347,7 +364,7 @@ func (c *Client) NewRequest() *Request {
347364
//
348365
// return nil // if its success otherwise return error
349366
// })
350-
func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
367+
func (c *Client) OnBeforeRequest(m RequestMiddleware) *Client {
351368
c.udBeforeRequest = append(c.udBeforeRequest, m)
352369
return c
353370
}
@@ -361,7 +378,7 @@ func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
361378
//
362379
// return nil // if its success otherwise return error
363380
// })
364-
func (c *Client) OnAfterResponse(m func(*Client, *Response) error) *Client {
381+
func (c *Client) OnAfterResponse(m ResponseMiddleware) *Client {
365382
c.afterResponse = append(c.afterResponse, m)
366383
return c
367384
}
@@ -370,7 +387,7 @@ func (c *Client) OnAfterResponse(m func(*Client, *Response) error) *Client {
370387
// It is called right before the request is fired.
371388
//
372389
// Note: Only one pre-request hook can be registered. Use `client.OnBeforeRequest` for mutilple.
373-
func (c *Client) SetPreRequestHook(h func(*Client, *http.Request) error) *Client {
390+
func (c *Client) SetPreRequestHook(h PreRequestHook) *Client {
374391
if c.preReqHook != nil {
375392
c.log.Warnf("Overwriting an existing pre-request hook: %s", functionName(h))
376393
}
@@ -396,7 +413,7 @@ func (c *Client) SetDebugBodyLimit(sl int64) *Client {
396413

397414
// OnRequestLog method used to set request log callback into Resty. Registered callback gets
398415
// called before the resty actually logs the information.
399-
func (c *Client) OnRequestLog(rl func(*RequestLog) error) *Client {
416+
func (c *Client) OnRequestLog(rl RequestLogCallback) *Client {
400417
if c.requestLog != nil {
401418
c.log.Warnf("Overwriting an existing on-request-log callback from=%s to=%s",
402419
functionName(c.requestLog), functionName(rl))
@@ -407,7 +424,7 @@ func (c *Client) OnRequestLog(rl func(*RequestLog) error) *Client {
407424

408425
// OnResponseLog method used to set response log callback into Resty. Registered callback gets
409426
// called before the resty actually logs the information.
410-
func (c *Client) OnResponseLog(rl func(*ResponseLog) error) *Client {
427+
func (c *Client) OnResponseLog(rl ResponseLogCallback) *Client {
411428
if c.responseLog != nil {
412429
c.log.Warnf("Overwriting an existing on-response-log callback from=%s to=%s",
413430
functionName(c.responseLog), functionName(rl))
@@ -939,7 +956,7 @@ func createClient(hc *http.Client) *Client {
939956
c.SetLogger(createLogger())
940957

941958
// default before request middlewares
942-
c.beforeRequest = []func(*Client, *Request) error{
959+
c.beforeRequest = []RequestMiddleware{
943960
parseRequestURL,
944961
parseRequestHeader,
945962
parseRequestBody,
@@ -948,10 +965,10 @@ func createClient(hc *http.Client) *Client {
948965
}
949966

950967
// user defined request middlewares
951-
c.udBeforeRequest = []func(*Client, *Request) error{}
968+
c.udBeforeRequest = []RequestMiddleware{}
952969

953970
// default after response middlewares
954-
c.afterResponse = []func(*Client, *Response) error{
971+
c.afterResponse = []ResponseMiddleware{
955972
responseLogger,
956973
parseResponseBody,
957974
saveResponseIntoFile,

0 commit comments

Comments
 (0)