2020-01-15 04:56:50 +00:00
|
|
|
// Package httputil provides abstractions around the common needs of HTTP. It
|
|
|
|
// also allows swapping in and out the HTTP client.
|
2020-01-02 05:39:52 +00:00
|
|
|
package httputil
|
|
|
|
|
|
|
|
import (
|
2020-04-19 21:53:53 +00:00
|
|
|
"bytes"
|
2020-01-02 05:39:52 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2020-01-19 03:12:08 +00:00
|
|
|
"mime/multipart"
|
2020-11-25 20:08:42 +00:00
|
|
|
"time"
|
2020-05-07 16:49:07 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/utils/httputil/httpdriver"
|
|
|
|
"github.com/diamondburned/arikawa/v2/utils/json"
|
2020-01-02 05:39:52 +00:00
|
|
|
)
|
|
|
|
|
2020-05-07 16:49:07 +00:00
|
|
|
// StatusTooManyRequests is the HTTP status code discord sends on rate-limiting.
|
|
|
|
const StatusTooManyRequests = 429
|
|
|
|
|
2020-01-20 19:28:55 +00:00
|
|
|
// Retries is the default attempts to retry if the API returns an error before
|
2020-04-19 21:53:53 +00:00
|
|
|
// giving up. If the value is smaller than 1, then requests will retry forever.
|
2020-01-20 19:28:55 +00:00
|
|
|
var Retries uint = 5
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
type Client struct {
|
2020-04-19 21:53:53 +00:00
|
|
|
httpdriver.Client
|
2020-01-06 03:48:39 +00:00
|
|
|
SchemaEncoder
|
2020-01-20 19:28:55 +00:00
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
// OnRequest, if not nil, will be copied and prefixed on each Request.
|
|
|
|
OnRequest []RequestOption
|
2020-04-19 21:53:53 +00:00
|
|
|
|
|
|
|
// OnResponse is called after every Do() call. Response might be nil if Do()
|
|
|
|
// errors out. The error returned will override Do's if it's not nil.
|
2020-05-03 21:02:03 +00:00
|
|
|
OnResponse []ResponseFunc
|
2020-04-19 21:53:53 +00:00
|
|
|
|
2020-11-25 20:08:42 +00:00
|
|
|
// Timeout is the maximum amount of time the client will wait for a request
|
|
|
|
// to finish. If this is 0 or smaller the Client won't time out. Otherwise,
|
|
|
|
// the timeout will be used as deadline for context of every request.
|
|
|
|
Timeout time.Duration
|
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
// Default to the global Retries variable (5).
|
2020-01-20 19:28:55 +00:00
|
|
|
Retries uint
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
context context.Context
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
func NewClient() *Client {
|
|
|
|
return &Client{
|
|
|
|
Client: httpdriver.NewClient(),
|
2020-01-06 03:48:39 +00:00
|
|
|
SchemaEncoder: &DefaultSchema{},
|
2020-01-20 19:28:55 +00:00
|
|
|
Retries: Retries,
|
2020-05-03 21:02:03 +00:00
|
|
|
context: context.Background(),
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
// Copy returns a shallow copy of the client.
|
|
|
|
func (c *Client) Copy() *Client {
|
|
|
|
cl := new(Client)
|
|
|
|
*cl = *c
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContext returns a client copy of the client with the given context.
|
|
|
|
func (c *Client) WithContext(ctx context.Context) *Client {
|
|
|
|
c = c.Copy()
|
|
|
|
c.context = ctx
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context is a shared context for all future calls. It's Background by
|
|
|
|
// default.
|
|
|
|
func (c *Client) Context() context.Context {
|
|
|
|
return c.context
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:09:37 +00:00
|
|
|
// applyOptions tries to apply all options. It does not halt if a single option
|
|
|
|
// fails, and the error returned is the latest error.
|
|
|
|
func (c *Client) applyOptions(r httpdriver.Request, extra []RequestOption) (e error) {
|
2020-05-03 21:02:03 +00:00
|
|
|
for _, opt := range c.OnRequest {
|
2020-04-19 21:53:53 +00:00
|
|
|
if err := opt(r); err != nil {
|
2020-08-04 21:09:37 +00:00
|
|
|
e = err
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
2020-08-04 21:09:37 +00:00
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
for _, opt := range extra {
|
|
|
|
if err := opt(r); err != nil {
|
2020-08-04 21:09:37 +00:00
|
|
|
e = err
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:09:37 +00:00
|
|
|
return
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 21:11:11 +00:00
|
|
|
// MultipartWriter is the interface for a data structure that can write into a
|
|
|
|
// multipart writer.
|
|
|
|
type MultipartWriter interface {
|
|
|
|
WriteMultipart(body *multipart.Writer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeanwhileMultipart concurrently encodes and writes the given multipart writer
|
|
|
|
// at the same time. The writer will be called in another goroutine, but the
|
|
|
|
// writer will be closed when MeanwhileMultipart returns.
|
2020-01-19 03:12:08 +00:00
|
|
|
func (c *Client) MeanwhileMultipart(
|
2020-12-16 21:11:11 +00:00
|
|
|
writer MultipartWriter,
|
2020-04-19 21:53:53 +00:00
|
|
|
method, url string, opts ...RequestOption) (httpdriver.Response, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
r, w := io.Pipe()
|
2020-01-19 03:12:08 +00:00
|
|
|
body := multipart.NewWriter(w)
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-12-16 21:11:11 +00:00
|
|
|
// Ensure the writer is closed by the time this function exits, so
|
|
|
|
// WriteMultipart will exit.
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := writer.WriteMultipart(body)
|
|
|
|
if err != nil {
|
|
|
|
err = body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
w.CloseWithError(err)
|
|
|
|
}()
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
// Prepend the multipart writer and the correct Content-Type header options.
|
|
|
|
opts = PrependOptions(
|
|
|
|
opts,
|
|
|
|
WithBody(r),
|
|
|
|
WithContentType(body.FormDataContentType()),
|
|
|
|
)
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
// Request with the current client and our own context:
|
2020-11-14 23:30:18 +00:00
|
|
|
return c.Request(method, url, opts...)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
func (c *Client) FastRequest(method, url string, opts ...RequestOption) error {
|
2020-01-02 05:39:52 +00:00
|
|
|
r, err := c.Request(method, url, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
return r.GetBody().Close()
|
|
|
|
}
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
func (c *Client) RequestJSON(to interface{}, method, url string, opts ...RequestOption) error {
|
2020-04-19 21:53:53 +00:00
|
|
|
opts = PrependOptions(opts, JSONRequest)
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
r, err := c.Request(method, url, opts...)
|
2020-04-19 21:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var body, status = r.GetBody(), r.GetStatus()
|
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
// No content, working as intended (tm)
|
|
|
|
if status == httpdriver.NoContent {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-16 21:11:11 +00:00
|
|
|
// to is nil for some reason. Ignore.
|
|
|
|
if to == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-19 21:53:53 +00:00
|
|
|
|
2020-05-08 03:43:46 +00:00
|
|
|
if err := json.DecodeStream(body, to); err != nil {
|
2020-04-19 21:53:53 +00:00
|
|
|
return JSONError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
func (c *Client) Request(method, url string, opts ...RequestOption) (httpdriver.Response, error) {
|
2020-12-12 01:58:52 +00:00
|
|
|
// Error for the actual Do method.
|
2020-05-05 22:36:14 +00:00
|
|
|
var doErr error
|
2020-12-12 01:58:52 +00:00
|
|
|
// Error that represents the latest error in the chain.
|
|
|
|
var onRespErr error
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
var r httpdriver.Response
|
|
|
|
var status int
|
2020-01-20 19:28:55 +00:00
|
|
|
|
2020-11-25 20:08:42 +00:00
|
|
|
ctx := c.context
|
|
|
|
|
|
|
|
if c.Timeout > 0 {
|
|
|
|
var cancel func()
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, c.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:09:37 +00:00
|
|
|
// The c.Retries < 1 check ensures that we retry forever if that field is
|
|
|
|
// less than 1.
|
2020-04-19 21:53:53 +00:00
|
|
|
for i := uint(0); c.Retries < 1 || i < c.Retries; i++ {
|
2020-11-25 20:08:42 +00:00
|
|
|
q, err := c.Client.NewRequest(ctx, method, url)
|
2020-05-05 22:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, RequestError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.applyOptions(q, opts); err != nil {
|
2020-08-04 21:09:37 +00:00
|
|
|
// We failed to apply an option, so we should call all OnResponse
|
|
|
|
// handler to clean everything up.
|
|
|
|
for _, fn := range c.OnResponse {
|
|
|
|
fn(q, nil)
|
|
|
|
}
|
|
|
|
// Exit after cleaning everything up.
|
2020-11-14 23:30:18 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to apply http request options")
|
2020-05-05 22:32:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:36:14 +00:00
|
|
|
r, doErr = c.Client.Do(q)
|
2020-05-05 22:32:28 +00:00
|
|
|
|
|
|
|
// Call OnResponse() even if the request failed.
|
|
|
|
for _, fn := range c.OnResponse {
|
2020-08-04 21:09:37 +00:00
|
|
|
// Be sure to call ALL OnResponse handlers.
|
2020-05-05 22:32:28 +00:00
|
|
|
if err := fn(q, r); err != nil {
|
2020-08-04 21:09:37 +00:00
|
|
|
onRespErr = err
|
2020-05-05 22:32:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 01:58:52 +00:00
|
|
|
if onRespErr != nil || doErr != nil {
|
2020-01-20 19:28:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-07 16:49:07 +00:00
|
|
|
if status = r.GetStatus(); status == StatusTooManyRequests || status >= 500 {
|
2020-01-20 19:28:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-12-12 01:58:52 +00:00
|
|
|
if onRespErr != nil {
|
|
|
|
return nil, errors.Wrap(onRespErr, "OnResponse handler failed")
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:28:55 +00:00
|
|
|
// If all retries failed:
|
2020-05-05 22:36:14 +00:00
|
|
|
if doErr != nil {
|
|
|
|
return nil, RequestError{doErr}
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 19:28:55 +00:00
|
|
|
// Response received, but with a failure status code:
|
2020-04-19 21:53:53 +00:00
|
|
|
if status < 200 || status > 299 {
|
|
|
|
// Try and parse the body.
|
|
|
|
var body = r.GetBody()
|
|
|
|
defer body.Close()
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
// This rarely happens, so we can (probably) make an exception for it.
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
buf.ReadFrom(body)
|
|
|
|
|
|
|
|
httpErr := &HTTPError{
|
|
|
|
Status: status,
|
|
|
|
Body: buf.Bytes(),
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 21:53:53 +00:00
|
|
|
// Optionally unmarshal the error.
|
2020-05-08 03:43:46 +00:00
|
|
|
json.Unmarshal(httpErr.Body, &httpErr)
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
return nil, httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|