-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
98 lines (83 loc) · 2.5 KB
/
middleware.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
87
88
89
90
91
92
93
94
95
96
97
98
package kapusta
import (
"fmt"
"net/http"
"net/url"
"time"
)
// HeaderMiddleware returns a DecoratorFunc that adds the given HTTP header to every request done by a Client.
func HeaderMiddleware(name, value string) MiddlewareFunc {
return HeadersMiddleware(map[string]string{name: value})
}
// HeadersMiddleware returns a DecoratorFunc that adds the given HTTP headers to every request done by a Client.
func HeadersMiddleware(values map[string]string) MiddlewareFunc {
return func(c Client) Client {
return ClientFunc(func(r *http.Request) (*http.Response, error) {
for key, value := range values {
r.Header.Add(key, value)
}
return c.Do(r)
})
}
}
// RecoverMiddleware returns a DecoratorFunc that recovers panic and convert it to error
func RecoverMiddleware() MiddlewareFunc {
return func(c Client) Client {
return ClientFunc(func(r *http.Request) (res *http.Response, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered panic: %v", r)
}
}()
return c.Do(r)
})
}
}
// BaseURLMiddleware returns a DecoratorFunc that replaces scheme and host in request from specified baseURL
func BaseURLMiddleware(baseURL string) MiddlewareFunc {
parsed, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
return func(c Client) Client {
return ClientFunc(func(r *http.Request) (*http.Response, error) {
r.URL.Scheme = parsed.Scheme
r.URL.Host = parsed.Host
return c.Do(r)
})
}
}
// BadRequestFunc is used to determine was response successful or not.
// For example you can mark response as successful if http status is 200 or 400.
type GoodRequestFn func(r *http.Response, err error) bool
// RequestStatusIn helper for treat requests with specified statuses valid.
func RequestStatusIn(status ...int) GoodRequestFn {
return func(r *http.Response, err error) bool {
if err != nil {
return false
}
for _, s := range status {
if r.StatusCode == s {
return true
}
}
return false
}
}
// BackoffMiddleware implements simple
func BackoffMiddleware(retries int, delay time.Duration, goodRequestFn GoodRequestFn) MiddlewareFunc {
return func(c Client) Client {
nextDelay := time.Duration(0)
return ClientFunc(func(r *http.Request) (*http.Response, error) {
for i := 1; i <= retries; i++ {
res, err := c.Do(r)
if goodRequestFn(res, err) {
return res, err
}
nextDelay += delay * time.Duration(i)
time.Sleep(nextDelay)
}
return nil, fmt.Errorf("request failed after %v retries.", retries)
})
}
}