-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
153 lines (129 loc) · 3.16 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
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
package files_sdk
import (
"fmt"
"log"
"net/http"
"os"
"strings"
libLog "github.com/Files-com/files-sdk-go/v2/lib/logpath"
"github.com/hashicorp/go-retryablehttp"
)
var VERSION = "2.0.267"
const (
UserAgent = "Files.com Go SDK"
DefaultSite = "app"
APIPath = "/api/rest/v1"
)
var GlobalConfig Config
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
Get(string) (*http.Response, error)
}
type Logger interface {
Printf(string, ...interface{})
}
type Config struct {
APIKey string `header:"X-FilesAPI-Key"`
SessionId string `header:"X-FilesAPI-Auth"`
Endpoint string
Subdomain string
standardClient HttpClient
rawClient *retryablehttp.Client
AdditionalHeaders map[string]string
logger Logger
Debug bool
UserAgent string
Environment
FeatureFlags map[string]bool
}
func (c *Config) SetHttpClient(client *http.Client) {
c.GetRawClient().HTTPClient = client
}
func (c *Config) GetHttpClient() HttpClient {
if c.standardClient == nil {
c.standardClient = c.GetRawClient().StandardClient()
}
return c.standardClient
}
func (c *Config) GetRawClient() *retryablehttp.Client {
if c.rawClient == nil {
c.rawClient = retryablehttp.NewClient()
c.rawClient.ErrorHandler = retryablehttp.PassthroughErrorHandler
c.rawClient.Logger = c.Logger()
c.rawClient.RetryMax = 3
}
return c.rawClient
}
type NullLogger struct{}
func (n NullLogger) Printf(_ string, _ ...interface{}) {
}
func (c *Config) InDebug() bool {
return c.Debug || (os.Getenv("FILES_SDK_DEBUG") != "")
}
func (c *Config) Logger() retryablehttp.Logger {
if c.logger != nil {
return c.logger
}
if c.InDebug() {
c.SetLogger(log.New(os.Stderr, "", log.LstdFlags))
} else {
c.SetLogger(NullLogger{})
}
return c.logger
}
func (c *Config) LogPath(path string, args map[string]interface{}) {
c.Logger().Printf(libLog.New(path, args))
}
func (c *Config) SetLogger(l Logger) {
c.logger = l
}
func (c *Config) RootPath() string {
if c.Subdomain == "" {
c.Subdomain = DefaultSite
}
if c.Endpoint == "" {
c.Endpoint = strings.Replace(c.Environment.Endpoint(), "{SUBDOMAIN}", c.Subdomain, 1)
}
return c.Endpoint + APIPath
}
func (c *Config) GetAPIKey() string {
if c.APIKey != "" {
return c.APIKey
}
if os.Getenv("FILES_API_KEY") != "" {
return os.Getenv("FILES_API_KEY")
}
return ""
}
func (c *Config) SetHeaders(headers *http.Header) {
if c.UserAgent == "" {
c.UserAgent = fmt.Sprintf("%v %v", UserAgent, strings.TrimSpace(VERSION))
}
headers.Set("User-Agent", c.UserAgent)
if c.GetAPIKey() != "" {
headers.Set("X-FilesAPI-Key", c.GetAPIKey())
} else if c.SessionId != "" {
headers.Set("X-FilesAPI-Auth", c.SessionId)
}
for key, value := range c.AdditionalHeaders {
headers.Set(key, value)
}
}
func (c *Config) FeatureFlag(flag string) bool {
var flags map[string]bool
if c.FeatureFlags == nil {
flags = FeatureFlags()
} else {
flags = c.FeatureFlags
}
value, ok := flags[flag]
if !ok {
panic(fmt.Sprintf("feature flag `%v` is not a valid flag", flag))
}
return value
}
func FeatureFlags() map[string]bool {
return map[string]bool{
"incremental-updates": false,
}
}