-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalloption.go
113 lines (97 loc) · 2.65 KB
/
calloption.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package rapi
import (
"net/http"
"net/textproto"
)
// CallOption configures how we set up the http call.
type CallOption interface {
apply(*callOptions)
}
type funcCallOption struct {
f func(*callOptions)
}
func (o *funcCallOption) apply(options *callOptions) {
o.f(options)
}
func newFuncCallOption(f func(options *callOptions)) *funcCallOption {
return &funcCallOption{
f: f,
}
}
type joinCallOption struct {
opts []CallOption
}
func newJoinCallOption(opts ...CallOption) *joinCallOption {
return &joinCallOption{
opts: opts,
}
}
func (o *joinCallOption) apply(options *callOptions) {
for _, opt := range o.opts {
opt.apply(options)
}
}
type callOptions struct {
RequestHeader http.Header
MaxResponseBodySize int64
ErrOut error
ForceBody bool
}
func newCallOptions() (o *callOptions) {
return &callOptions{
RequestHeader: http.Header{},
}
}
func (o *callOptions) Clone() *callOptions {
if o == nil {
return nil
}
result := &callOptions{
RequestHeader: o.RequestHeader.Clone(),
MaxResponseBodySize: o.MaxResponseBodySize,
ErrOut: o.ErrOut,
ForceBody: o.ForceBody,
}
return result
}
// WithRequestHeader returns a CallOption that sets the given http headers to the request headers.
func WithRequestHeader(headers ...http.Header) CallOption {
return newFuncCallOption(func(options *callOptions) {
for _, hdr := range headers {
for k, v := range hdr.Clone() {
k = textproto.CanonicalMIMEHeaderKey(k)
options.RequestHeader[k] = v
}
}
})
}
// WithAdditionalRequestHeader returns a CallOption that adds the given http headers to the request headers.
func WithAdditionalRequestHeader(headers ...http.Header) CallOption {
return newFuncCallOption(func(options *callOptions) {
for _, hdr := range headers {
for k, v := range hdr {
for _, v2 := range v {
options.RequestHeader.Add(k, v2)
}
}
}
})
}
// WithMaxResponseBodySize returns a CallOption that limits maximum response body size.
func WithMaxResponseBodySize(maxResponseBodySize int64) CallOption {
return newFuncCallOption(func(options *callOptions) {
options.MaxResponseBodySize = maxResponseBodySize
})
}
// WithErrOut returns a CallOption that defines the error output to return as Caller.Call error.
func WithErrOut(errOut error) CallOption {
return newFuncCallOption(func(options *callOptions) {
options.ErrOut = errOut
})
}
// WithForceBody returns a CallOption that forces sending input in the request body for all methods including HEAD and GET.
func WithForceBody(forceBody bool) CallOption {
return newFuncCallOption(func(options *callOptions) {
options.ForceBody = forceBody
})
}