-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
124 lines (99 loc) · 3.04 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
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
120
121
122
123
124
package internal
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
)
// Client represents the REST client.
type Client struct {
mu sync.Mutex
Token string
HTTP *http.Client
// We will manually add the API version
APIVersion string
// Used to safely create URLs and is filled if empty
URLHost string
URLScheme string
UserAgent string
}
// NewClient makes a new client.
func NewClient(baseURL url.URL, token string) *Client {
return &Client{
mu: sync.Mutex{},
Token: token,
HTTP: http.DefaultClient,
APIVersion: "9",
URLHost: baseURL.Host,
URLScheme: baseURL.Scheme,
UserAgent: "Sandwich/" + VERSION + " (github.com/WelcomerTeam/Sandwich-Daemon)",
}
}
// Fetch returns the response. Passing any headers will be sent to the request however
// Authorization will be overwrote.
func (c *Client) Fetch(ctx context.Context, method string, url string,
body io.Reader, headers map[string]string,
) ([]byte, int, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, 0, fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
res, err := c.HandleRequest(req, false)
if err != nil {
return nil, 0, err
}
defer res.Body.Close()
resultBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, res.StatusCode, fmt.Errorf("failed to read request body: %w", err)
}
return resultBody, res.StatusCode, nil
}
// FetchJSON attempts to convert the response into a JSON structure. Passing any headers
// will be sent to the request however Authorization will be overwrote.
func (c *Client) FetchJSON(ctx context.Context, method string, url string, body io.Reader,
headers map[string]string, structure interface{},
) (int, error) {
responseBody, status, err := c.Fetch(ctx, method, url, body, headers)
if err != nil {
return status, err
}
err = json.Unmarshal(responseBody, &structure)
if err != nil {
return -1, fmt.Errorf("failed to unmarshal body: %w", err)
}
return status, nil
}
// HandleRequest makes a request to the Discord API.
func (c *Client) HandleRequest(req *http.Request, retry bool) (*http.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
// Add the /api and version prefix when we did not include one
if !strings.HasPrefix(req.URL.Path, "/api") {
req.URL.Path = "/api/v" + c.APIVersion + req.URL.Path
}
req.URL.Host = replaceIfEmpty(req.URL.Host, c.URLHost)
req.URL.Scheme = replaceIfEmpty(req.URL.Scheme, c.URLScheme)
req.Header.Set("User-Agent", replaceIfEmpty(req.Header.Get("User-Agent"), c.UserAgent))
if c.Token != "" {
req.Header.Set("Authorization", replaceIfEmpty(req.Header.Get("Authorization"), "Bot "+c.Token))
}
res, err := c.HTTP.Do(req)
if err != nil {
return res, fmt.Errorf("failed to do HTTP request: %v", err)
}
if res.StatusCode == http.StatusTooManyRequests {
return res, fmt.Errorf("failed to do HTTP request: received 429")
}
if res.StatusCode == http.StatusUnauthorized {
return res, ErrInvalidToken
}
return res, nil
}