-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
78 lines (61 loc) · 1.37 KB
/
client.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
package discgo
import (
"net/http"
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type ApiConfig struct {
APIBaseURL string
ApplicationID string
PublicKey string
ClientID string
ClientSecret string
BotToken string
}
type ApiClient struct {
discordConfig *ApiConfig
client HTTPClient
logger Logger
}
type ApiClientOption func(*ApiClient)
func WithApiConfig(cfg *ApiConfig) ApiClientOption {
return func(d *ApiClient) {
d.discordConfig = cfg
}
}
func WithHttpClient(c HTTPClient) ApiClientOption {
return func(d *ApiClient) {
d.client = c
}
}
func WithClientLogger(l Logger) ApiClientOption {
return func(dc *ApiClient) {
// TODO make this a logger interface
dc.logger = l
}
}
func NewApiClient(options ...ApiClientOption) *ApiClient {
d := ApiClient{}
for _, opt := range options {
opt(&d)
}
if d.client == nil {
d.client = http.DefaultClient
}
if d.discordConfig.APIBaseURL == "" {
d.discordConfig.APIBaseURL = "https://discord.com/api"
}
if d.logger == nil {
d.logger = &noopLogger{}
}
return &d
}
// Do adds headers before calling DiscordClient.http.Do
func (d *ApiClient) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bot "+d.discordConfig.BotToken)
return d.client.Do(req)
}
func (d *ApiClient) Close() {
// nothing to do here right now
}