forked from Ecalose/hbookerlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
81 lines (72 loc) · 1.74 KB
/
options.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
package hbookerLib
import (
"regexp"
)
type Options interface {
Apply(client *Client)
}
type OptionFunc func(client *Client)
func (f OptionFunc) Apply(client *Client) {
f(client)
}
func WithLoginToken(loginToken string) Options {
return OptionFunc(func(client *Client) {
client.Authenticate.SetLoginToken(loginToken)
})
}
func WithAccount(account string) Options {
return OptionFunc(func(client *Client) {
client.Authenticate.SetAccount(account)
})
}
func WithVersion(version string) Options {
return OptionFunc(func(client *Client) {
client.Authenticate.SetAppVersion(version)
})
}
func WithIos() Options {
return OptionFunc(func(client *Client) {
client.ios = true
})
}
func WithRetryCount(retryCount int) Options {
return OptionFunc(func(client *Client) {
if retryCount > 0 {
client.retryCount = retryCount
}
})
}
func WithDebug() Options {
return OptionFunc(func(client *Client) {
client.debug = !client.debug
})
}
func WithOutputDebug() Options {
return OptionFunc(func(client *Client) {
client.outputDebug = !client.outputDebug
})
}
func WithProxyURL(proxyURL string) Options {
return OptionFunc(func(client *Client) {
if regexp.MustCompile(`^http[s]?://[a-zA-Z0-9.-]+(:\d+)?$`).MatchString(proxyURL) {
client.proxyURL = proxyURL
}
})
}
func WithAPIBaseURL(apiBaseURL string) Options {
return OptionFunc(func(client *Client) {
if regexp.MustCompile(`^https?://[a-zA-Z0-9.-]+(:\d+)?(/.*)?$`).MatchString(apiBaseURL) {
client.baseURL = apiBaseURL
}
})
}
func WithApiKey(apiKey string) Options {
return OptionFunc(func(client *Client) {
client.apiKey = apiKey
})
}
func WithDeviceToken(deviceToken string) Options {
return OptionFunc(func(client *Client) {
client.Authenticate.SetDeviceToken(deviceToken)
})
}