-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
37 lines (32 loc) · 1011 Bytes
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package requests
import (
"net/http"
)
type Hook interface {
// call BeforeRequest before send http request,
// if resp != nil or err != nil
// use this resp and err, no longer send http request.
BeforeRequest(req *http.Request) (resp *http.Response, err error)
// call AfterRequest after got response
// if newResp != nil or newErr != nil
// use the new NewResp instead of origin response.
AfterRequest(req *http.Request, resp *http.Response, err error) (newResp *http.Response, newErr error)
}
func applyBeforeReqHooks(req *http.Request, hooks []Hook) (resp *http.Response, err error) {
for _, hook := range hooks {
resp, err = hook.BeforeRequest(req)
if resp != nil || err != nil {
return
}
}
return
}
func applyAfterReqHooks(req *http.Request, resp *http.Response, err error, hooks []Hook) (newResp *http.Response, newErr error) {
for _, hook := range hooks {
newResp, newErr = hook.AfterRequest(req, resp, err)
if newResp != nil || newErr != nil {
return
}
}
return
}