1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-16 03:44:26 +00:00
arikawa/utils/httputil/options.go

95 lines
1.9 KiB
Go
Raw Normal View History

2020-01-02 05:39:52 +00:00
package httputil
import (
"io"
"net/http"
2020-05-09 21:59:39 +00:00
"net/url"
2020-01-02 05:39:52 +00:00
2021-06-02 02:53:19 +00:00
"github.com/diamondburned/arikawa/v3/utils/httputil/httpdriver"
"github.com/diamondburned/arikawa/v3/utils/json"
2020-01-02 05:39:52 +00:00
)
2020-04-19 21:53:53 +00:00
type RequestOption func(httpdriver.Request) error
2020-05-03 21:02:03 +00:00
type ResponseFunc func(httpdriver.Request, httpdriver.Response) error
2020-01-02 05:39:52 +00:00
2020-04-19 21:53:53 +00:00
func PrependOptions(opts []RequestOption, prepend ...RequestOption) []RequestOption {
if len(opts) == 0 {
return prepend
}
return append(prepend, opts...)
}
func JSONRequest(r httpdriver.Request) error {
r.AddHeader(http.Header{
"Content-Type": {"application/json"},
})
2020-01-02 05:39:52 +00:00
return nil
}
2020-04-19 21:53:53 +00:00
func MultipartRequest(r httpdriver.Request) error {
r.AddHeader(http.Header{
"Content-Type": {"multipart/form-data"},
})
2020-01-19 02:27:30 +00:00
return nil
}
2020-04-19 16:17:04 +00:00
func WithHeaders(headers http.Header) RequestOption {
2020-04-19 21:53:53 +00:00
return func(r httpdriver.Request) error {
r.AddHeader(headers)
2020-04-19 16:17:04 +00:00
return nil
}
}
2020-01-19 03:12:08 +00:00
func WithContentType(ctype string) RequestOption {
2020-04-19 21:53:53 +00:00
return func(r httpdriver.Request) error {
r.AddHeader(http.Header{
"Content-Type": {ctype},
})
2020-01-19 03:12:08 +00:00
return nil
}
}
2020-01-06 03:48:39 +00:00
func WithSchema(schema SchemaEncoder, v interface{}) RequestOption {
2020-04-19 21:53:53 +00:00
return func(r httpdriver.Request) error {
2020-05-09 21:59:39 +00:00
var params url.Values
2020-05-09 23:25:24 +00:00
if p, ok := v.(url.Values); ok {
params = p
2020-05-09 21:59:39 +00:00
} else {
2020-05-09 23:25:24 +00:00
p, err := schema.Encode(v)
2020-05-09 21:59:39 +00:00
if err != nil {
return err
}
2020-05-09 23:25:24 +00:00
params = p
2020-01-06 03:48:39 +00:00
}
2020-04-19 21:53:53 +00:00
r.AddQuery(params)
2020-01-02 05:39:52 +00:00
return nil
}
}
2020-01-06 03:48:39 +00:00
func WithBody(body io.ReadCloser) RequestOption {
2020-04-19 21:53:53 +00:00
return func(r httpdriver.Request) error {
r.WithBody(body)
2020-01-06 03:48:39 +00:00
return nil
}
}
2020-01-02 05:39:52 +00:00
2020-04-19 21:53:53 +00:00
// WithJSONBody inserts a JSON body into the request. This ignores JSON errors.
func WithJSONBody(v interface{}) RequestOption {
2020-01-02 05:39:52 +00:00
if v == nil {
return func(httpdriver.Request) error { return nil }
2020-01-02 05:39:52 +00:00
}
2020-04-19 21:53:53 +00:00
return func(r httpdriver.Request) error {
2021-01-13 23:20:26 +00:00
rp, wp := io.Pipe()
go func() { wp.CloseWithError(json.EncodeStream(wp, v)) }()
2020-04-19 21:53:53 +00:00
r.AddHeader(http.Header{
"Content-Type": {"application/json"},
})
r.WithBody(rp)
2020-01-02 05:39:52 +00:00
return nil
}
}