@@ -35,22 +35,30 @@ type Promise struct {
35
35
mu sync.Mutex
36
36
}
37
37
38
+ type RequestInterceptors []func (* http.Request ) error
39
+ type ResponseInterceptors []func (* http.Response ) error
40
+ type InterceptorOptions struct {
41
+ requestInterceptors RequestInterceptors
42
+ responseInterceptors ResponseInterceptors
43
+ }
44
+
38
45
type RequestOptions struct {
39
- method string
40
- url string
41
- baseURL string
42
- params map [string ]string
43
- body interface {}
44
- headers map [string ]string
45
- timeout int
46
- auth * auth
47
- responseType string
48
- responseEncoding string
49
- maxRedirects int
50
- maxContentLength int
51
- maxBodyLength int
52
- decompress bool
53
- validateStatus func (int ) bool
46
+ method string
47
+ url string
48
+ baseURL string
49
+ params map [string ]string
50
+ body interface {}
51
+ headers map [string ]string
52
+ timeout int
53
+ auth * auth
54
+ responseType string
55
+ responseEncoding string
56
+ maxRedirects int
57
+ maxContentLength int
58
+ maxBodyLength int
59
+ decompress bool
60
+ validateStatus func (int ) bool
61
+ interceptorOptions InterceptorOptions
54
62
}
55
63
56
64
type auth struct {
@@ -336,6 +344,13 @@ func (c *Client) Request(options *RequestOptions) (*Response, error) {
336
344
return nil , err
337
345
}
338
346
347
+ for _ , interceptor := range options .interceptorOptions .requestInterceptors {
348
+ err = interceptor (req )
349
+ if err != nil {
350
+ return nil , fmt .Errorf ("request interceptor failed: %w" , err )
351
+ }
352
+ }
353
+
339
354
if options .headers == nil {
340
355
options .headers = make (map [string ]string )
341
356
}
@@ -394,6 +409,13 @@ func (c *Client) Request(options *RequestOptions) (*Response, error) {
394
409
return nil , fmt .Errorf ("Request failed with status code: %v" , resp .StatusCode )
395
410
}
396
411
412
+ for _ , interceptor := range options .interceptorOptions .responseInterceptors {
413
+ err = interceptor (resp )
414
+ if err != nil {
415
+ return nil , fmt .Errorf ("response interceptor failed: %w" , err )
416
+ }
417
+ }
418
+
397
419
return & Response {
398
420
StatusCode : resp .StatusCode ,
399
421
Headers : resp .Header ,
@@ -444,6 +466,12 @@ func mergeOptions(dst, src *RequestOptions) {
444
466
if src .validateStatus != nil {
445
467
dst .validateStatus = src .validateStatus
446
468
}
469
+ if src .interceptorOptions .requestInterceptors != nil {
470
+ dst .interceptorOptions .requestInterceptors = src .interceptorOptions .requestInterceptors
471
+ }
472
+ if src .interceptorOptions .responseInterceptors != nil {
473
+ dst .interceptorOptions .responseInterceptors = src .interceptorOptions .responseInterceptors
474
+ }
447
475
dst .decompress = src .decompress
448
476
}
449
477
0 commit comments