-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
103 lines (96 loc) · 3.35 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
package gsclient
import (
"github.com/sirupsen/logrus"
"net/http"
"os"
"runtime"
"time"
)
const (
defaultCheckRequestTimeoutSecs = 120
defaultMaxNumberOfRetries = 5
defaultDelayIntervalMilliSecs = 500
version = "2.1.0"
defaultAPIURL = "https://api.gridscale.io"
resourceActiveStatus = "active"
requestDoneStatus = "done"
requestFailStatus = "failed"
bodyType = "application/json"
)
//Config config for client
type Config struct {
apiURL string
userUUID string
apiToken string
userAgent string
sync bool
httpClient *http.Client
requestCheckTimeoutSecs time.Duration
delayInterval time.Duration
maxNumberOfRetries int
logger logrus.Logger
}
//NewConfiguration creates a new config
//
//- Parameters:
// + apiURL string: base URL of API.
// + uuid string: UUID of user.
// + token string: API token.
// + debugMode bool: true => run client in debug mode.
// + sync bool: true => client is in synchronous mode. The client will block until Create/Update/Delete processes
// are completely finished. It is safer to set this parameter to `true`.
// + requestCheckTimeoutSecs int: Timeout (in second) for checking requests (for synchronous feature)
// + delayIntervalMilliSecs int: delay (in MilliSecond) between requests when checking request (or retry 5xx, 424 error code)
// + maxNumberOfRetries int: number of retries when server returns 5xx, 424 error code.
func NewConfiguration(apiURL string, uuid string, token string, debugMode, sync bool, requestCheckTimeoutSecs,
delayIntervalMilliSecs, maxNumberOfRetries int) *Config {
logLevel := logrus.InfoLevel
if debugMode {
logLevel = logrus.DebugLevel
}
logger := logrus.Logger{
Out: os.Stderr,
Level: logLevel,
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
DisableColors: false,
},
}
cfg := &Config{
apiURL: apiURL,
userUUID: uuid,
apiToken: token,
userAgent: "gsclient-go/" + version + " (" + runtime.GOOS + ")",
sync: sync,
httpClient: http.DefaultClient,
logger: logger,
requestCheckTimeoutSecs: time.Duration(requestCheckTimeoutSecs) * time.Second,
delayInterval: time.Duration(delayIntervalMilliSecs) * time.Millisecond,
maxNumberOfRetries: maxNumberOfRetries,
}
return cfg
}
//DefaultConfiguration creates a default configuration
func DefaultConfiguration(uuid string, token string) *Config {
logger := logrus.Logger{
Out: os.Stderr,
Level: logrus.InfoLevel,
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
DisableColors: false,
},
}
cfg := &Config{
apiURL: defaultAPIURL,
userUUID: uuid,
apiToken: token,
userAgent: "gsclient-go/" + version + " (" + runtime.GOOS + ")",
sync: true,
httpClient: http.DefaultClient,
logger: logger,
requestCheckTimeoutSecs: time.Duration(defaultCheckRequestTimeoutSecs) * time.Second,
delayInterval: time.Duration(defaultDelayIntervalMilliSecs) * time.Millisecond,
maxNumberOfRetries: defaultMaxNumberOfRetries,
}
return cfg
}