forked from HereMobilityDevelopers/mediary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (62 loc) · 1.71 KB
/
config.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
package mediary
import (
"net/http"
)
// Handler is just an alias to http.RoundTriper.RoundTrip function
type Handler func(*http.Request) (*http.Response, error)
// Interceptor is a user defined function that can alter a request before it's sent
// and/or alter a response before it's returned to the caller
type Interceptor func(*http.Request, Handler) (*http.Response, error)
type options struct {
predefinedClient *http.Client
interceptors []Interceptor
}
// Builder is a builder interface to help guide and create a mediary instance
type Builder interface {
AddInterceptors(...Interceptor) Builder
WithPreconfiguredClient(*http.Client) Builder
Build() *http.Client
}
type builderImpl struct {
apply func(*options)
next *builderImpl
}
// Init initializes mediary instance configuration
func Init() Builder {
return new(builderImpl)
}
func (impl *builderImpl) AddInterceptors(interceptors ...Interceptor) Builder {
return &builderImpl{
apply: func(opt *options) {
opt.interceptors = append(opt.interceptors, interceptors...)
},
next: impl,
}
}
func (impl *builderImpl) WithPreconfiguredClient(client *http.Client) Builder {
return &builderImpl{
apply: func(opt *options) {
opt.predefinedClient = client
},
next: impl,
}
}
func (impl *builderImpl) Build() *http.Client {
var client = &http.Client{}
if impl != nil {
opts := new(options)
iterator := impl
for iterator.next != nil {
iterator.apply(opts)
iterator = iterator.next
}
if opts.predefinedClient != nil {
client = opts.predefinedClient
}
if client.Transport == nil {
client.Transport = http.DefaultTransport
}
client.Transport = prepareCustomRoundTripper(client.Transport, opts.interceptors...)
}
return client
}