-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlist_client.go
90 lines (71 loc) · 2.18 KB
/
dlist_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
79
80
81
82
83
84
85
86
87
88
89
90
package dlist
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
)
type Config struct {
Version string `required:"true"`
BotID string
APIToken string `required:"true"`
RequestTimeout time.Duration `default:"5s"`
}
type DlistClient struct {
BaseUrl string
BotID string
APIToken string
http *http.Client
}
func New(cfg *Config) *DlistClient {
return &DlistClient{
BaseUrl: fmt.Sprintf("https://api.discordlist.gg/%s", cfg.Version),
BotID: cfg.BotID,
APIToken: cfg.APIToken,
http: &http.Client{
Timeout: cfg.RequestTimeout,
},
}
}
func (d *DlistClient) request(ctx context.Context, method, url string, body []byte, headers http.Header) ([]byte, error) {
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", d.BaseUrl, url), bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", d.APIToken))
for h, val := range headers {
for _, item := range val {
req.Header.Add(h, item)
}
}
res, err := d.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("received unexpected status code: %v", res.StatusCode)
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed reading body: %v", err)
}
return resBody, nil
}
func (d *DlistClient) get(ctx context.Context, url string, headers http.Header) ([]byte, error) {
return d.request(ctx, http.MethodGet, url, nil, http.Header{})
}
func (d *DlistClient) post(ctx context.Context, url string, body []byte, headers http.Header) ([]byte, error) {
return d.request(ctx, http.MethodPost, url, body, headers)
}
func (d *DlistClient) put(ctx context.Context, url string, body []byte, headers http.Header) ([]byte, error) {
return d.request(ctx, http.MethodPut, url, body, headers)
}
func (d *DlistClient) PostGuilds(count int) ([]byte, error) {
return d.put(context.Background(), fmt.Sprintf("/bots/%s/guilds?count=%d", d.BotID, count), nil, http.Header{})
}
func (d *DlistClient) GetBot(ID string) ([]byte, error) {
return d.get(context.Background(), fmt.Sprintf("/bots/%s", ID), http.Header{})
}