/
options.go
119 lines (101 loc) · 2.7 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
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
package freshchat
import (
"github.com/fairyhunter13/reflecthelper/v5"
"github.com/gojek/heimdall/v7"
"github.com/gojek/heimdall/v7/hystrix"
"net/http"
"strings"
"time"
)
const (
// DefaultBaseURLAustralia is the default base URL for the Freshchat API in Australia.
DefaultBaseURLAustralia = "https://api.au.freshchat.com"
// DefaultTimeout is the default timeout for the client.
DefaultTimeout = 30 * time.Second
)
// Option is an option for the Freshchat API
type Option struct {
BaseURL string
NameSpace string
APIToken string
FromPhoneNumber string
Timeout time.Duration
Client heimdall.Doer
HystrixOptions []hystrix.Option
client *hystrix.Client
}
// FnOption is a functional option for the Freshchat API
type FnOption func(o *Option)
// Clone clones the option and returns a new one.
func (o *Option) Clone() *Option {
opt := *o
return &opt
}
// Assign assigns the functional options to the option.
func (o *Option) Assign(opts ...FnOption) *Option {
for _, opt := range opts {
opt(o)
}
return o
}
// Default returns option with default values.
func (o *Option) Default() *Option {
if o.BaseURL == "" {
o.BaseURL = DefaultBaseURLAustralia
}
o.BaseURL = strings.TrimRight(o.BaseURL, "/")
if o.Timeout < DefaultTimeout {
o.Timeout = DefaultTimeout
}
if reflecthelper.IsNil(o.Client) {
o.Client = http.DefaultClient
}
o.client = hystrix.NewClient(
hystrix.WithHystrixTimeout(o.Timeout),
hystrix.WithHTTPTimeout(o.Timeout),
hystrix.WithHTTPClient(o.Client),
)
return o
}
// WithBaseURL sets the base URL for the Freshchat API.
func WithBaseURL(baseURL string) FnOption {
return func(o *Option) {
o.BaseURL = baseURL
}
}
// WithNameSpace sets the name space for the Freshchat API.
func WithNameSpace(nameSpace string) FnOption {
return func(o *Option) {
o.NameSpace = nameSpace
}
}
// WithAPIToken sets the API token for the Freshchat API.
func WithAPIToken(apiToken string) FnOption {
return func(o *Option) {
o.APIToken = apiToken
}
}
// WithFromPhoneNumber sets the from phone number for the Freshchat API.
func WithFromPhoneNumber(fromPhoneNumber string) FnOption {
return func(o *Option) {
o.FromPhoneNumber = fromPhoneNumber
}
}
// WithTimeout sets the timeout for the Freshchat API.
func WithTimeout(timeout time.Duration) FnOption {
return func(o *Option) {
o.Timeout = timeout
}
}
// WithClient sets the client for the Freshchat API.
func WithClient(client heimdall.Doer) FnOption {
return func(o *Option) {
o.Client = client
}
}
// WithHystrixOptions sets the hystrix options for the Freshchat API.
func WithHystrixOptions(options []hystrix.Option) FnOption {
return func(o *Option) {
o.HystrixOptions = options
}
}