mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-09 16:35:12 +00:00
159 lines
3 KiB
Go
159 lines
3 KiB
Go
// Package httputil provides abstractions around the common needs of HTTP. It
|
|
// also allows swapping in and out the HTTP client.
|
|
package httputil
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/diamondburned/arikawa/internal/json"
|
|
)
|
|
|
|
type Client struct {
|
|
http.Client
|
|
json.Driver
|
|
SchemaEncoder
|
|
}
|
|
|
|
var DefaultClient = NewClient()
|
|
|
|
func NewClient() Client {
|
|
return Client{
|
|
Client: http.Client{
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
Driver: json.Default{},
|
|
SchemaEncoder: &DefaultSchema{},
|
|
}
|
|
}
|
|
|
|
func (c *Client) MeanwhileMultipart(
|
|
multipartWriter func(*multipart.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())
|
|
defer cancel()
|
|
|
|
r, w := io.Pipe()
|
|
body := multipart.NewWriter(w)
|
|
|
|
var bgErr error
|
|
|
|
go func() {
|
|
if err := multipartWriter(body); err != nil {
|
|
bgErr = err
|
|
cancel()
|
|
}
|
|
|
|
// Close the writer so the body gets flushed to the HTTP reader.
|
|
w.Close()
|
|
}()
|
|
|
|
resp, err := c.RequestCtx(ctx, method, url,
|
|
append([]RequestOption{
|
|
WithBody(r),
|
|
WithContentType(body.FormDataContentType()),
|
|
}, 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 {
|
|
|
|
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 {
|
|
log.Println("Do error", url, err)
|
|
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()
|
|
|
|
// No content, working as intended (tm)
|
|
if r.StatusCode == http.StatusNoContent {
|
|
return nil
|
|
}
|
|
|
|
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...)
|
|
}
|