-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
118 lines (95 loc) · 2.11 KB
/
config.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
package yext
import (
"net/http"
"os"
"time"
"github.com/jonboulle/clockwork"
)
const (
SandboxHost string = "api-sandbox.yext.com"
ProductionHost string = "api.yext.com"
AccountId string = "me"
Version string = "20180226"
)
type Config struct {
HTTPClient *http.Client
BaseUrl string
ApiKey string
AccountId string
Version string
RetryCount *int
RateLimitRetry bool
Clock clockwork.Clock
Logger Logger
}
func NewConfig() *Config {
return &Config{
HTTPClient: http.DefaultClient,
AccountId: AccountId,
Version: Version,
Clock: clockwork.NewRealClock(),
}
}
func NewDefaultConfig() *Config {
return NewConfig().WithProductionHost().WithRetries(3)
}
func (c *Config) WithHTTPClient(client *http.Client) *Config {
c.HTTPClient = client
return c
}
func (c *Config) WithBaseUrl(baseUrl string) *Config {
c.BaseUrl = baseUrl
return c
}
func (c *Config) WithHost(host string) *Config {
return c.WithBaseUrl("https://" + host + "/v2")
}
func (c *Config) WithSandboxHost() *Config {
return c.WithHost(SandboxHost)
}
func (c *Config) WithProductionHost() *Config {
return c.WithHost(ProductionHost)
}
func (c *Config) WithApiKey(apikey string) *Config {
c.ApiKey = apikey
return c
}
func (c *Config) WithAccountId(id string) *Config {
c.AccountId = id
return c
}
func (c *Config) WithVersion(v string) *Config {
c.Version = v
return c
}
func (c *Config) WithTodaysVersion() *Config {
c.Version = time.Now().Format("20060102")
return c
}
func (c *Config) WithEnvCredentials() *Config {
c = c.WithApiKey(os.ExpandEnv("$YEXT_API_KEY"))
if os.ExpandEnv("$YEXT_API_ACCOUNTID") != "" {
c = c.WithAccountId(os.ExpandEnv("$YEXT_API_ACCOUNTID"))
}
return c
}
func (c *Config) WithRetries(r int) *Config {
c.RetryCount = Int(r)
return c
}
func (c *Config) WithLogger(l Logger) *Config {
c.Logger = l
return c
}
func (c *Config) WithStdLogger() *Config {
c.Logger = NewStdLogger()
return c
}
func (c *Config) WithRateLimitRetry() *Config {
c.RateLimitRetry = true
return c
}
func (c *Config) WithMockClock() *Config {
c.Clock = clockwork.NewFakeClock()
return c
}