@@ -35,22 +35,30 @@ type Promise struct {
3535 mu sync.Mutex
3636}
3737
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+
3845type 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
5462}
5563
5664type auth struct {
@@ -336,6 +344,13 @@ func (c *Client) Request(options *RequestOptions) (*Response, error) {
336344 return nil , err
337345 }
338346
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+
339354 if options .headers == nil {
340355 options .headers = make (map [string ]string )
341356 }
@@ -394,6 +409,13 @@ func (c *Client) Request(options *RequestOptions) (*Response, error) {
394409 return nil , fmt .Errorf ("Request failed with status code: %v" , resp .StatusCode )
395410 }
396411
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+
397419 return & Response {
398420 StatusCode : resp .StatusCode ,
399421 Headers : resp .Header ,
@@ -444,6 +466,12 @@ func mergeOptions(dst, src *RequestOptions) {
444466 if src .validateStatus != nil {
445467 dst .validateStatus = src .validateStatus
446468 }
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+ }
447475 dst .decompress = src .decompress
448476}
449477
0 commit comments