-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
48 lines (37 loc) · 901 Bytes
/
http.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
package revolt
import (
"bytes"
"fmt"
"io"
"net/http"
)
// Send http request
func (c Client) Request(method, path string, data []byte) ([]byte, error) {
reqBody := bytes.NewBuffer(data)
// Prepare request
req, err := http.NewRequest(method, RevoltAPI+path, reqBody)
if err != nil {
return []byte{}, err
}
req.Header.Set("content-type", "application/json")
// Set auth headers
if c.SelfBot == nil {
req.Header.Set("x-bot-token", c.Token)
} else if c.SelfBot.SessionToken != "" {
req.Header.Set("x-session-token", c.SelfBot.SessionToken)
}
// Send request
resp, err := c.HTTP.Do(req)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return []byte{}, fmt.Errorf("%s: %s", resp.Status, body)
}
return body, nil
}