-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
158 lines (121 loc) · 2.99 KB
/
rest.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package guildedgo
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
func (c *Client) PostRequest(endpoint string, body interface{}) ([]byte, error) {
jsonBody, _ := json.Marshal(&body)
resp, err := DoRequest("POST", endpoint, jsonBody, c.Token)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) PostRequestV2(endpoint string, body any, v any) error {
jsonBody, _ := json.Marshal(&body)
resp, err := DoRequest("POST", endpoint, jsonBody, c.Token)
if err != nil {
return err
}
err = json.Unmarshal(resp, &v)
if err != nil {
return err
}
return nil
}
func (c *Client) PatchRequest(endpoint string, body any, v any) error {
jsonBody, _ := json.Marshal(&body)
resp, err := DoRequest("PATCH", endpoint, jsonBody, c.Token)
if err != nil {
return err
}
err = json.Unmarshal(resp, &v)
if err != nil {
return err
}
return nil
}
func (c *Client) GetRequest(endpoint string) ([]byte, error) {
resp, err := DoRequest("GET", endpoint, nil, c.Token)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetRequestV2(endpoint string, v any) error {
resp, err := DoRequest("GET", endpoint, nil, c.Token)
if err != nil {
return err
}
err = json.Unmarshal(resp, &v)
if err != nil {
return err
}
return nil
}
func (c *Client) PutRequest(endpoint string, body interface{}) ([]byte, error) {
jsonBody, _ := json.Marshal(&body)
resp, err := DoRequest("PUT", endpoint, jsonBody, c.Token)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) PutRequestV2(endpoint string, body interface{}, v any) error {
jsonBody, _ := json.Marshal(&body)
resp, err := DoRequest("PUT", endpoint, jsonBody, c.Token)
if err != nil {
return err
}
err = json.Unmarshal(resp, &v)
if err != nil {
return err
}
return nil
}
func (c *Client) DeleteRequest(endpoint string) ([]byte, error) {
resp, err := DoRequest("DELETE", endpoint, nil, c.Token)
if err != nil {
return nil, err
}
return resp, nil
}
type responseError struct {
Code string `json:"code"`
Message string `json:"message"`
}
func DoRequest(method string, endpoint string, body []byte, token string) ([]byte, error) {
request, err := http.NewRequest(method, endpoint, bytes.NewBuffer(body))
request.Header.Set("Authorization", "Bearer "+token)
request.Header.Set("Content-Type", "application/json")
// Handle status codes - remember rate limiting
resp, err := do(request)
if err != nil {
return nil, err
}
return resp, nil
}
func do(req *http.Request) ([]byte, error) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusBadGateway, http.StatusForbidden, http.StatusBadRequest, http.StatusNotFound:
var resError responseError
err := json.Unmarshal(body, &resError)
if err != nil {
return nil, err
}
return nil, errors.New(resError.Message)
}
return body, nil
}