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 (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-01-02 19:55:45 +00:00
|
|
|
"github.com/diamondburned/arikawa/json"
|
2020-01-02 05:39:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
http.Client
|
|
|
|
json.Driver
|
2020-01-06 03:48:39 +00:00
|
|
|
SchemaEncoder
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
var DefaultClient = NewClient()
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func NewClient() Client {
|
|
|
|
return Client{
|
|
|
|
Client: http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
},
|
2020-01-06 03:48:39 +00:00
|
|
|
Driver: json.Default{},
|
|
|
|
SchemaEncoder: &DefaultSchema{},
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) MeanwhileBody(bodyWriter func(io.Writer) error,
|
|
|
|
method, url string, opts ...RequestOption) (*http.Response, error) {
|
|
|
|
|
|
|
|
// We want to cancel the request if our bodyWriter fails
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
r, w := io.Pipe()
|
|
|
|
|
|
|
|
var bgErr error
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := bodyWriter(w); err != nil {
|
|
|
|
bgErr = err
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
resp, err := c.RequestCtx(ctx, method, url,
|
|
|
|
append([]RequestOption{WithBody(r)}, opts...)...)
|
|
|
|
|
|
|
|
if err != nil && bgErr != nil {
|
|
|
|
if resp.Body != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, bgErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FastRequest(
|
|
|
|
method, url string, opts ...RequestOption) error {
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
r, err := c.Request(method, url, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RequestCtx(ctx context.Context,
|
|
|
|
method, url string, opts ...RequestOption) (*http.Response, error) {
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, RequestError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := c.Client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, RequestError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.StatusCode < 200 || r.StatusCode > 299 {
|
|
|
|
httpErr := &HTTPError{
|
|
|
|
Status: r.StatusCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
httpErr.Body = b
|
|
|
|
|
|
|
|
c.Unmarshal(b, &httpErr)
|
|
|
|
return nil, httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RequestCtxJSON(ctx context.Context,
|
|
|
|
to interface{}, method, url string, opts ...RequestOption) error {
|
|
|
|
|
|
|
|
r, err := c.RequestCtx(ctx, method, url,
|
|
|
|
append([]RequestOption{JSONRequest}, opts...)...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if err := c.DecodeStream(r.Body, to); err != nil {
|
|
|
|
return JSONError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Request(
|
|
|
|
method, url string, opts ...RequestOption) (*http.Response, error) {
|
|
|
|
|
|
|
|
return c.RequestCtx(context.Background(), method, url, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RequestJSON(
|
|
|
|
to interface{}, method, url string, opts ...RequestOption) error {
|
|
|
|
|
|
|
|
return c.RequestCtxJSON(context.Background(), to, method, url, opts...)
|
|
|
|
}
|