-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
161 lines (134 loc) · 2.84 KB
/
client.go
File metadata and controls
161 lines (134 loc) · 2.84 KB
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
159
160
161
package rest
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/rs/zerolog"
)
type HTTPClient interface {
Request(req *request) (*DiscordResponse, error)
}
type Config struct {
Authorization string
UserAgent string
Ratelimiter Ratelimiter
Cache Cache
Proxy func(*http.Request) (*url.URL, error)
Logger *zerolog.Logger
}
type Client struct {
httpClient HTTPClient
rateLimiter Ratelimiter
cache Cache
}
type request struct {
ctx context.Context
method string
path string
contentType string
body []byte
reason string
omitAuth bool
headers http.Header
out interface{}
}
func NewRequest() *request {
return &request{}
}
func (r *request) WithContext(ctx context.Context) *request {
r.ctx = ctx
return r
}
func (r *request) OmitAuth() *request {
r.omitAuth = true
return r
}
func (r *request) Bind(out interface{}) *request {
r.out = out
return r
}
func (r *request) Method(method string) *request {
r.method = method
return r
}
func (r *request) Path(path string) *request {
r.path = path
return r
}
func (r *request) Body(body []byte) *request {
r.body = body
return r
}
func (r *request) ContentType(contentType string) *request {
r.contentType = contentType
return r
}
func (r *request) Reason(reason string) *request {
r.reason = reason
return r
}
func (r *request) SendRaw(c *Client) (*DiscordResponse, error) {
if r.method == "GET" && c.cache != nil {
data, err := c.cache.Get(r.path)
if err == nil {
return data, nil
}
}
var resp *DiscordResponse
var err error
if c.rateLimiter != nil {
resp, err = c.rateLimiter.Request(c.httpClient, r)
} else {
resp, err = c.httpClient.Request(r)
}
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return resp, &ErrorREST{
Message: fmt.Sprintf("Non 2xx status code: %d (%s)", resp.StatusCode, string(resp.Body)),
Status: resp.StatusCode,
Body: resp.Body,
}
}
if r.method == "GET" && c.cache != nil {
// if this fails, there's not much of a recovery that can be done
_ = c.cache.Put(r.path, resp)
}
return resp, err
}
func (r *request) Send(c *Client) error {
r.headers = make(http.Header)
if r.reason != "" && r.headers.Get(XAuditLogReasonHeader) == "" {
r.headers.Set(XAuditLogReasonHeader, r.reason)
}
resp, err := r.SendRaw(c)
if err != nil {
return err
}
if r.out != nil {
return resp.JSON(r.out)
} else {
return nil
}
}
func New(config *Config) *Client {
var client Doer
if config.Proxy != nil {
client = NewProxyClient(config.Proxy)
} else {
client = &http.Client{
Timeout: time.Second * 5,
}
}
return &Client{
rateLimiter: config.Ratelimiter,
httpClient: &DefaultHTTPClient{
doer: client,
userAgent: config.UserAgent,
authorization: config.Authorization,
},
}
}