-
Notifications
You must be signed in to change notification settings - Fork 23
/
environment.go
68 lines (60 loc) · 1.45 KB
/
environment.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
package chargebee
import (
"errors"
"fmt"
"net/http"
"time"
)
type Environment struct {
Key string
SiteName string
ChargebeeDomain string
Protocol string
}
var (
TotalHTTPTimeout = 80 * time.Second
ExportWaitInSecs = 3 * time.Second
TimeMachineWaitInSecs = 3 * time.Second
DefaultEnv Environment
httpClient *http.Client
)
const (
APIVersion = "v2"
Charset = "UTF-8"
)
func Configure(key string, siteName string) {
if key == "" || siteName == "" {
panic(errors.New("Key/siteName cannot be empty"))
}
DefaultEnv = Environment{Key: key, SiteName: siteName}
}
func WithHTTPClient(c *http.Client) {
if c.Timeout == 0 {
c.Timeout = TotalHTTPTimeout
}
httpClient = c
}
func (env *Environment) apiBaseUrl() string {
if env.Protocol == "" {
env.Protocol = "https"
}
if env.ChargebeeDomain != "" {
return fmt.Sprintf("%v://%v.%v/api/%v", env.Protocol, env.SiteName, env.ChargebeeDomain, APIVersion)
}
return fmt.Sprintf("https://%v.chargebee.com/api/%v", env.SiteName, APIVersion)
}
func DefaultConfig() Environment {
if DefaultEnv == (Environment{}) {
panic(errors.New("The default environment has not been configured"))
}
return DefaultEnv
}
func NewDefaultHTTPClient() *http.Client {
return &http.Client{Timeout: TotalHTTPTimeout}
}
func UpdateTotalHTTPTimeout(timeout time.Duration) {
TotalHTTPTimeout = timeout
if httpClient != nil {
httpClient.Timeout = TotalHTTPTimeout
}
}