/
constructor.go
70 lines (53 loc) · 1.61 KB
/
constructor.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
package xhttpclient
import (
"net/http"
)
type RoundTripperFunc func(*http.Request) (*http.Response, error)
func (rtf RoundTripperFunc) RoundTrip(request *http.Request) (*http.Response, error) {
return rtf(request)
}
// Constructor is an Alice-style constructor for RoundTrippers
type Constructor func(http.RoundTripper) http.RoundTripper
// Chain is a sequence of Constructors, just like github.com/justinas/alice.Chain
type Chain struct {
c []Constructor
}
func NewChain(c ...Constructor) Chain {
return Chain{c: c}
}
func (ch Chain) Append(more ...Constructor) Chain {
merged := Chain{
c: make([]Constructor, 0, len(ch.c)+len(more)),
}
merged.c = append(merged.c, ch.c...)
merged.c = append(merged.c, more...)
return merged
}
func (ch Chain) Extend(more Chain) Chain {
return ch.Append(more.c...)
}
func (ch Chain) Then(rt http.RoundTripper) http.RoundTripper {
if rt == nil {
rt = new(http.Transport)
}
for i := range ch.c {
rt = ch.c[len(ch.c)-1-i](rt)
}
return rt
}
func (ch Chain) ThenFunc(rtf RoundTripperFunc) http.RoundTripper {
if rtf == nil {
return ch.Then(nil)
}
return ch.Then(rtf)
}
// A ChainFactory can be used to tailor a Chain of decorators for a specific client. This factory
// type is primarily useful when an application has multiple HTTP client objects. For cases where
// there is only (1) HTTP client, a Chain component works better.
type ChainFactory interface {
NewClientChain(string, Options) (Chain, error)
}
type ChainFactoryFunc func(string, Options) (Chain, error)
func (cff ChainFactoryFunc) NewClientChain(name string, o Options) (Chain, error) {
return cff(name, o)
}