66
66
bufPool = & sync.Pool {New : func () interface {} { return & bytes.Buffer {} }}
67
67
)
68
68
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
+
69
86
// Client struct is used to create Resty client with client level settings,
70
87
// these settings are applicable to all the request raised from the client.
71
88
//
@@ -104,12 +121,12 @@ type Client struct {
104
121
log Logger
105
122
httpClient * http.Client
106
123
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
113
130
}
114
131
115
132
// User type is to hold an username and password information
@@ -347,7 +364,7 @@ func (c *Client) NewRequest() *Request {
347
364
//
348
365
// return nil // if its success otherwise return error
349
366
// })
350
- func (c * Client ) OnBeforeRequest (m func ( * Client , * Request ) error ) * Client {
367
+ func (c * Client ) OnBeforeRequest (m RequestMiddleware ) * Client {
351
368
c .udBeforeRequest = append (c .udBeforeRequest , m )
352
369
return c
353
370
}
@@ -361,7 +378,7 @@ func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
361
378
//
362
379
// return nil // if its success otherwise return error
363
380
// })
364
- func (c * Client ) OnAfterResponse (m func ( * Client , * Response ) error ) * Client {
381
+ func (c * Client ) OnAfterResponse (m ResponseMiddleware ) * Client {
365
382
c .afterResponse = append (c .afterResponse , m )
366
383
return c
367
384
}
@@ -370,7 +387,7 @@ func (c *Client) OnAfterResponse(m func(*Client, *Response) error) *Client {
370
387
// It is called right before the request is fired.
371
388
//
372
389
// 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 {
374
391
if c .preReqHook != nil {
375
392
c .log .Warnf ("Overwriting an existing pre-request hook: %s" , functionName (h ))
376
393
}
@@ -396,7 +413,7 @@ func (c *Client) SetDebugBodyLimit(sl int64) *Client {
396
413
397
414
// OnRequestLog method used to set request log callback into Resty. Registered callback gets
398
415
// 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 {
400
417
if c .requestLog != nil {
401
418
c .log .Warnf ("Overwriting an existing on-request-log callback from=%s to=%s" ,
402
419
functionName (c .requestLog ), functionName (rl ))
@@ -407,7 +424,7 @@ func (c *Client) OnRequestLog(rl func(*RequestLog) error) *Client {
407
424
408
425
// OnResponseLog method used to set response log callback into Resty. Registered callback gets
409
426
// 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 {
411
428
if c .responseLog != nil {
412
429
c .log .Warnf ("Overwriting an existing on-response-log callback from=%s to=%s" ,
413
430
functionName (c .responseLog ), functionName (rl ))
@@ -939,7 +956,7 @@ func createClient(hc *http.Client) *Client {
939
956
c .SetLogger (createLogger ())
940
957
941
958
// default before request middlewares
942
- c .beforeRequest = []func ( * Client , * Request ) error {
959
+ c .beforeRequest = []RequestMiddleware {
943
960
parseRequestURL ,
944
961
parseRequestHeader ,
945
962
parseRequestBody ,
@@ -948,10 +965,10 @@ func createClient(hc *http.Client) *Client {
948
965
}
949
966
950
967
// user defined request middlewares
951
- c .udBeforeRequest = []func ( * Client , * Request ) error {}
968
+ c .udBeforeRequest = []RequestMiddleware {}
952
969
953
970
// default after response middlewares
954
- c .afterResponse = []func ( * Client , * Response ) error {
971
+ c .afterResponse = []ResponseMiddleware {
955
972
responseLogger ,
956
973
parseResponseBody ,
957
974
saveResponseIntoFile ,
0 commit comments