-
Notifications
You must be signed in to change notification settings - Fork 32
/
hooks.go
86 lines (75 loc) · 1.98 KB
/
hooks.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package gosip
import (
"net/http"
"time"
)
// HookHandlers struct to configure events handlers
type HookHandlers struct {
OnError func(event *HookEvent) // when error appeared
OnRetry func(event *HookEvent) // before retry request
OnRequest func(event *HookEvent) // before request is sent
OnResponse func(event *HookEvent) // after response is received
}
// HookEvent hook event parameters struct
type HookEvent struct {
Request *http.Request
StartedAt time.Time
StatusCode int
Error error
}
// onError on error hook handler
func (c *SPClient) onError(req *http.Request, startAt time.Time, statusCode int, err error) {
if req.Header.Get("X-Gosip-NoHooks") == "true" {
return
}
if c.Hooks != nil && c.Hooks.OnError != nil {
c.Hooks.OnError(&HookEvent{
Request: req,
StartedAt: startAt,
StatusCode: statusCode,
Error: err,
})
}
}
// onRetry on retry hook handler
func (c *SPClient) onRetry(req *http.Request, startAt time.Time, statusCode int, err error) {
if req.Header.Get("X-Gosip-NoHooks") == "true" {
return
}
if c.Hooks != nil && c.Hooks.OnRetry != nil {
c.Hooks.OnRetry(&HookEvent{
Request: req,
StartedAt: startAt,
StatusCode: statusCode,
Error: err,
})
}
}
// onResponse on response hook handler
func (c *SPClient) onResponse(req *http.Request, startAt time.Time, statusCode int, err error) {
if req.Header.Get("X-Gosip-NoHooks") == "true" {
return
}
if c.Hooks != nil && c.Hooks.OnResponse != nil {
c.Hooks.OnResponse(&HookEvent{
Request: req,
StartedAt: startAt,
StatusCode: statusCode,
Error: err,
})
}
}
// onRequest on response hook handler
func (c *SPClient) onRequest(req *http.Request, startAt time.Time, statusCode int, err error) {
if req.Header.Get("X-Gosip-NoHooks") == "true" {
return
}
if c.Hooks != nil && c.Hooks.OnRequest != nil {
c.Hooks.OnRequest(&HookEvent{
Request: req,
StartedAt: startAt,
StatusCode: statusCode,
Error: err,
})
}
}