-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_option.go
151 lines (125 loc) · 3.02 KB
/
client_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
// Code generated by AfterShip SDK Generator. DO NOT EDIT.
package tracking
import (
"os"
"strconv"
"strings"
)
const sdkPrefix = "AFTERSHIP_TRACKING_SDK_"
func init() {
key := getEnv("API_KEY")
if len(key) > 0 {
defaultClientOptions.apiKey = strings.ToLower(key)
}
authType := getEnv("AUTHENTICATION_TYPE")
if len(authType) > 0 {
defaultClientOptions.authenticationType = strings.ToUpper(authType)
}
secret := getEnv("API_SECRET")
if len(secret) > 0 {
defaultClientOptions.apiSecret = strings.ToLower(secret)
}
domain := getEnv("DOMAIN")
if len(domain) > 0 {
defaultClientOptions.domain = domain
}
maxRetry := getEnv("MAX_RETRY")
if len(maxRetry) > 0 {
num, err := strconv.Atoi(maxRetry)
if err == nil {
defaultClientOptions.maxRetry = num
}
}
timeout := getEnv("TIMEOUT")
if len(timeout) > 0 {
num, err := strconv.Atoi(timeout)
if err == nil {
defaultClientOptions.timeoutMs = int64(num)
}
}
userAgent := getEnv("USER_AGENT")
if len(userAgent) > 0 {
defaultClientOptions.userAgent = userAgent
}
proxy := getEnv("PROXY")
if len(proxy) > 0 {
defaultClientOptions.proxy = proxy
}
}
func getEnv(key string) string {
return os.Getenv(sdkPrefix + key)
}
type clientOptions struct {
domain string
maxRetry int
timeoutMs int64
userAgent string
proxy string
apiKey string
apiSecret string
authenticationType string
}
var defaultClientOptions = clientOptions{
domain: "https://api.aftership.com",
maxRetry: 2,
timeoutMs: 10000,
userAgent: "tracking-sdk-go/v6 (https://www.aftership.com) Go-http-client/1.1",
proxy: "",
apiKey: "",
apiSecret: "",
authenticationType: "API_KEY",
}
type ClientOption interface {
apply(options *clientOptions)
}
type funcClientOption struct {
f func(options *clientOptions)
}
func (op *funcClientOption) apply(o *clientOptions) {
op.f(o)
}
func newFuncOption(f func(options *clientOptions)) *funcClientOption {
return &funcClientOption{
f: f,
}
}
func WithMaxRetry(maxRetry int) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.maxRetry = maxRetry
})
}
func WithTimeout(timeout int64) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.timeoutMs = timeout
})
}
func WithUserAgent(userAgent string) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.userAgent = userAgent
})
}
func WithProxy(proxy string) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.proxy = proxy
})
}
func WithApiKey(key string) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.apiKey = key
})
}
func WithSecret(secret string) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.apiSecret = secret
})
}
func WithAuthKind(kind string) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.authenticationType = kind
})
}
func WithHost(host string) ClientOption {
return newFuncOption(func(o *clientOptions) {
o.domain = host
})
}