1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-17 04:13:02 +00:00
arikawa/utils/httputil/client.go

208 lines
4.6 KiB
Go
Raw Normal View History

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-01-02 05:39:52 +00:00
2020-04-19 21:53:53 +00:00
"github.com/diamondburned/arikawa/utils/httputil/httpdriver"
"github.com/diamondburned/arikawa/utils/json"
2020-04-19 21:53:53 +00:00
"github.com/pkg/errors"
2020-01-02 05:39:52 +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.
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-02 05:39:52 +00:00
json.Driver
2020-01-06 03:48:39 +00:00
SchemaEncoder
2020-04-19 21:53:53 +00:00
// DefaultOptions, if not nil, will be copied and prefixed on each Request.
DefaultOptions []RequestOption
// 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.
OnResponse func(httpdriver.Request, httpdriver.Response) error
// Default to the global Retries variable (5).
Retries uint
2020-01-02 05:39:52 +00:00
}
2020-04-19 21:53:53 +00:00
// ResponseNoop is used for (*Client).OnResponse.
func ResponseNoop(httpdriver.Request, httpdriver.Response) error {
return nil
}
2020-01-15 04:43:34 +00:00
2020-04-19 21:53:53 +00:00
func NewClient() *Client {
return &Client{
Client: httpdriver.NewClient(),
Driver: json.Default,
2020-01-06 03:48:39 +00:00
SchemaEncoder: &DefaultSchema{},
Retries: Retries,
2020-04-19 21:53:53 +00:00
OnResponse: ResponseNoop,
}
}
func (c *Client) applyOptions(r httpdriver.Request, extra []RequestOption) error {
for _, opt := range c.DefaultOptions {
if err := opt(r); err != nil {
return err
}
2020-01-02 05:39:52 +00:00
}
2020-04-19 21:53:53 +00:00
for _, opt := range extra {
if err := opt(r); err != nil {
return err
}
}
return nil
2020-01-02 05:39:52 +00:00
}
2020-01-19 03:12:08 +00:00
func (c *Client) MeanwhileMultipart(
2020-04-19 21:53:53 +00:00
writer func(*multipart.Writer) error,
method, url string, opts ...RequestOption) (httpdriver.Response, error) {
2020-01-02 05:39:52 +00:00
// We want to cancel the request if our bodyWriter fails
ctx, cancel := context.WithCancel(context.Background())
2020-01-19 02:27:30 +00:00
defer cancel()
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
var bgErr error
go func() {
2020-04-19 21:53:53 +00:00
if err := writer(body); err != nil {
2020-01-02 05:39:52 +00:00
bgErr = err
cancel()
}
2020-01-19 02:27:30 +00:00
// Close the writer so the body gets flushed to the HTTP reader.
w.Close()
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-04-19 21:53:53 +00:00
resp, err := c.RequestCtx(ctx, method, url, opts...)
2020-01-02 05:39:52 +00:00
if err != nil && bgErr != nil {
return nil, bgErr
}
return resp, err
}
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()
}
func (c *Client) RequestCtxJSON(
ctx context.Context,
to interface{}, method, url string, opts ...RequestOption) error {
opts = PrependOptions(opts, JSONRequest)
r, err := c.RequestCtx(ctx, method, url, opts...)
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
}
if err := c.DecodeStream(body, to); err != nil {
return JSONError{err}
}
return nil
2020-01-02 05:39:52 +00:00
}
2020-04-19 21:53:53 +00:00
func (c *Client) RequestCtx(
ctx context.Context,
method, url string, opts ...RequestOption) (httpdriver.Response, error) {
2020-01-02 05:39:52 +00:00
2020-04-19 21:53:53 +00:00
req, err := c.Client.NewRequest(ctx, method, url)
2020-01-02 05:39:52 +00:00
if err != nil {
return nil, RequestError{err}
}
2020-04-19 21:53:53 +00:00
if err := c.applyOptions(req, opts); err != nil {
return nil, errors.Wrap(err, "Failed to apply options")
2020-01-02 05:39:52 +00:00
}
2020-04-19 21:53:53 +00:00
var r httpdriver.Response
var status int
2020-04-19 21:53:53 +00:00
for i := uint(0); c.Retries < 1 || i < c.Retries; i++ {
r, err = c.Client.Do(req)
if err != nil {
continue
}
2020-04-19 21:53:53 +00:00
if status = r.GetStatus(); status < 200 || status > 299 {
continue
}
break
}
2020-04-19 21:53:53 +00:00
// Call OnResponse() even if the request failed.
if err := c.OnResponse(req, r); err != nil {
return nil, err
}
// If all retries failed:
2020-01-02 05:39:52 +00:00
if err != nil {
return nil, RequestError{err}
}
// 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.
c.Unmarshal(httpErr.Body, &httpErr)
2020-01-02 05:39:52 +00:00
return nil, httpErr
}
return r, nil
}
2020-04-19 21:53:53 +00:00
func (c *Client) Request(method, url string, opts ...RequestOption) (httpdriver.Response, error) {
2020-01-02 05:39:52 +00:00
return c.RequestCtx(context.Background(), method, url, opts...)
}
2020-04-19 21:53:53 +00:00
func (c *Client) RequestJSON(to interface{}, method, url string, opts ...RequestOption) error {
2020-01-02 05:39:52 +00:00
return c.RequestCtxJSON(context.Background(), to, method, url, opts...)
}