-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientoption.go
62 lines (50 loc) · 1.25 KB
/
clientoption.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
package httpclient
import (
"net/http"
"github.com/phamvinhdat/httpclient/gosender"
)
type clientOption struct {
sender Sender
header http.Header
hookFns []HookFn
}
type ClientOption interface {
apply(*clientOption)
}
type clientOptFunc func(*clientOption)
func (f clientOptFunc) apply(c *clientOption) {
f(c)
}
func getClientOption(opts ...ClientOption) clientOption {
opt := clientOption{
sender: gosender.New(),
header: http.Header{},
}
for _, f := range opts {
f.apply(&opt)
}
return opt
}
func WithSender(sender Sender) ClientOption {
return clientOptFunc(func(c *clientOption) {
c.sender = sender
})
}
func WithClientHookFn(hookFn HookFn) ClientOption {
return clientOptFunc(func(c *clientOption) {
c.hookFns = append(c.hookFns, hookFn)
})
}
// WithClientHeader sets the header entries associated with key to the single
// element value. It replaces any existing values associated with key. If
// isAdding[0] == true (default is false) then It appends to any existing values
// associated with key
func WithClientHeader(key, value string, isAdding ...bool) ClientOption {
return clientOptFunc(func(c *clientOption) {
fn := c.header.Set
if isAdding != nil && isAdding[0] == true {
fn = c.header.Add
}
fn(key, value)
})
}