-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
31 lines (26 loc) · 1.03 KB
/
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
package requests
import (
"context"
"net/http"
)
var (
globalHooks []Hook
// lastGlobalHooks will be done after normal hook
lastGlobalHooks []Hook
)
type Hook interface {
// PrepareRequest handle this request before do requesting
PrepareRequest(context.Context, *http.Request) error
// OnRequestError is for when PrepareRequest return error, it will handle this error, if this function also return error, all the work will stop
OnRequestError(context.Context, *http.Request, error) error
// ProcessResponse handle this request and response after do requesting
ProcessResponse(context.Context, *http.Request, *http.Response) error
// OnResponseError is for when ProcessResponse return error, it will handle this error, if this function also return error, all the work will stop
OnResponseError(context.Context, *http.Request, *http.Response, error) error
}
func UseGlobalHook(hooks ...Hook) {
globalHooks = append(globalHooks, hooks...)
}
func UseLastGlobalHook(hooks ...Hook) {
lastGlobalHooks = append(lastGlobalHooks, hooks...)
}