-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoption.go
179 lines (151 loc) · 3.69 KB
/
option.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package valuefirst
import (
"github.com/fairyhunter13/reflecthelper/v5"
"github.com/flip-id/valuefirst/manager"
"net/http"
"strings"
"time"
"github.com/gojek/heimdall/v7"
"github.com/gojek/heimdall/v7/hystrix"
)
const (
// DefaultTimeout sets the default timeout of the HTTP client.
DefaultTimeout = 30 * time.Second
// BaseURL sets the base URL of the API.
BaseURL = "https://api.myvfirst.com/psms"
)
// FnOption is a function that sets the option.
type FnOption func(o *Option)
// WithBaseURL sets the base URL of the API.
func WithBaseURL(s string) FnOption {
return func(o *Option) {
o.BaseURL = s
}
}
// WithBasicAuth sets the basic auth credentials.
func WithBasicAuth(user string, password string) FnOption {
return func(o *Option) {
o.BasicAuth = OptionBasicAuth{
User: user,
Password: password,
}
}
}
// WithTimeout sets the timeout of the HTTP client.
func WithTimeout(t time.Duration) FnOption {
return func(o *Option) {
o.Timeout = t
}
}
// WithClient sets the HTTP client.
func WithClient(c heimdall.Doer) FnOption {
return func(o *Option) {
o.Client = c
}
}
// WithHystrixOptions sets the hystrix options.
func WithHystrixOptions(opts ...hystrix.Option) FnOption {
return func(o *Option) {
o.HystrixOptions = append(o.HystrixOptions, opts...)
}
}
// WithCustomIPs sets the custom IPs used for hitting the API.
func WithCustomIPs(ips ...string) FnOption {
return func(o *Option) {
o.CustomIPs = ips
}
}
// WithTokenManager sets the token manager.
func WithTokenManager(tm manager.TokenManager) FnOption {
return func(o *Option) {
o.TokenManager = tm
}
}
// WithTokenManagerOptions sets the token manager options.
func WithTokenManagerOptions(opts ...manager.FnOption) FnOption {
return func(o *Option) {
o.TokenManagerOptions = opts
}
}
// Option is a config for Value.
type Option struct {
BaseURL string
BasicAuth OptionBasicAuth
Timeout time.Duration
Client heimdall.Doer
HystrixOptions []hystrix.Option
CustomIPs []string
TokenManager manager.TokenManager
TokenManagerOptions []manager.FnOption
// private variables
client *hystrix.Client
vfClient *client
}
// OptionBasicAuth is a config for basic authorization.
type OptionBasicAuth struct {
User string
Password string
}
// Clone clones the Option.
// Clone only makes a shallow copy of the Option struct.
func (o *Option) Clone() *Option {
opt := *o
return &opt
}
// Assign assigns the Option using the functional options.
func (o *Option) Assign(opts ...FnOption) *Option {
for _, opt := range opts {
opt(o)
}
return o
}
func (o *Option) setValueFirstClient(c *client) *Option {
o.vfClient = c
return o
}
// Default sets the config default value.
func (o *Option) Default() *Option {
if o.BaseURL == "" {
o.BaseURL = BaseURL
}
o.BaseURL = strings.TrimRight(o.BaseURL, "/")
if o.Timeout < DefaultTimeout {
o.Timeout = DefaultTimeout
}
if o.Client == nil {
o.Client = http.DefaultClient
}
o.client = hystrix.NewClient(
append(
[]hystrix.Option{
hystrix.WithHTTPTimeout(o.Timeout),
hystrix.WithHystrixTimeout(o.Timeout),
hystrix.WithHTTPClient(o.Client),
},
o.HystrixOptions...,
)...,
)
if o.vfClient == nil {
o.vfClient = &client{o}
}
if reflecthelper.IsNil(o.TokenManager) {
o.TokenManager = manager.New(
append(
[]manager.FnOption{manager.WithClient(o.vfClient)},
o.TokenManagerOptions...,
)...,
)
}
return o
}
// Validate validates the config variables to ensure smooth integration.
func (o *Option) Validate() (err error) {
if o.BasicAuth.User == "" {
err = ErrEmptyUsername
return
}
if o.BasicAuth.Password == "" {
err = ErrEmptyPassword
}
return
}