2015-11-27 13:46:13 +01:00
|
|
|
// Package rest implements a simple REST wrapper
|
2016-02-23 22:16:13 +01:00
|
|
|
//
|
|
|
|
// All methods are safe for concurrent calling.
|
2015-11-27 13:46:13 +01:00
|
|
|
package rest
|
2015-10-04 23:08:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-09-04 21:00:37 +02:00
|
|
|
"context"
|
2015-10-04 23:08:31 +02:00
|
|
|
"encoding/json"
|
2017-07-25 18:38:18 +02:00
|
|
|
"encoding/xml"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-10-04 23:08:31 +02:00
|
|
|
"io"
|
2017-07-05 18:48:43 +02:00
|
|
|
"mime/multipart"
|
2015-10-04 23:08:31 +02:00
|
|
|
"net/http"
|
2017-07-06 13:12:01 +02:00
|
|
|
"net/url"
|
2016-02-23 22:16:13 +01:00
|
|
|
"sync"
|
2015-10-04 23:08:31 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-08-26 13:18:36 +02:00
|
|
|
"github.com/rclone/rclone/lib/readers"
|
2015-10-04 23:08:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client contains the info to sustain the API
|
|
|
|
type Client struct {
|
2016-02-23 22:16:13 +01:00
|
|
|
mu sync.RWMutex
|
2015-11-27 13:46:13 +01:00
|
|
|
c *http.Client
|
|
|
|
rootURL string
|
|
|
|
errorHandler func(resp *http.Response) error
|
2015-11-27 19:25:52 +01:00
|
|
|
headers map[string]string
|
2017-07-25 20:16:26 +02:00
|
|
|
signer SignerFn
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient takes an oauth http.Client and makes a new api instance
|
2015-11-27 19:25:52 +01:00
|
|
|
func NewClient(c *http.Client) *Client {
|
|
|
|
api := &Client{
|
2015-11-27 13:46:13 +01:00
|
|
|
c: c,
|
|
|
|
errorHandler: defaultErrorHandler,
|
2015-11-27 19:25:52 +01:00
|
|
|
headers: make(map[string]string),
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
2015-11-27 19:25:52 +01:00
|
|
|
return api
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 12:44:43 +01:00
|
|
|
// ReadBody reads resp.Body into result, closing the body
|
|
|
|
func ReadBody(resp *http.Response) (result []byte, err error) {
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
2022-08-20 16:38:02 +02:00
|
|
|
return io.ReadAll(resp.Body)
|
2017-03-12 12:44:43 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:25:52 +01:00
|
|
|
// defaultErrorHandler doesn't attempt to parse the http body, just
|
2019-08-28 12:21:38 +02:00
|
|
|
// returns it in the error message closing resp.Body
|
2015-11-27 13:46:13 +01:00
|
|
|
func defaultErrorHandler(resp *http.Response) (err error) {
|
2017-03-12 12:44:43 +01:00
|
|
|
body, err := ReadBody(resp)
|
2015-11-27 19:25:52 +01:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("error reading error out of body: %w", err)
|
2015-11-27 19:25:52 +01:00
|
|
|
}
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("HTTP error %v (%v) returned body: %q", resp.StatusCode, resp.Status, body)
|
2015-11-27 13:46:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetErrorHandler sets the handler to decode an error response when
|
|
|
|
// the HTTP status code is not 2xx. The handler should close resp.Body.
|
2015-11-27 19:25:52 +01:00
|
|
|
func (api *Client) SetErrorHandler(fn func(resp *http.Response) error) *Client {
|
2016-02-23 22:16:13 +01:00
|
|
|
api.mu.Lock()
|
|
|
|
defer api.mu.Unlock()
|
2015-11-27 13:46:13 +01:00
|
|
|
api.errorHandler = fn
|
2015-11-27 19:25:52 +01:00
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2017-07-07 09:18:13 +02:00
|
|
|
// SetRoot sets the default RootURL. You can override this on a per
|
|
|
|
// call basis using the RootURL field in Opts.
|
2015-11-27 19:25:52 +01:00
|
|
|
func (api *Client) SetRoot(RootURL string) *Client {
|
2016-02-23 22:16:13 +01:00
|
|
|
api.mu.Lock()
|
|
|
|
defer api.mu.Unlock()
|
2015-11-27 19:25:52 +01:00
|
|
|
api.rootURL = RootURL
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeader sets a header for all requests
|
2019-10-31 12:46:19 +01:00
|
|
|
// Start the key with "*" for don't canonicalise
|
2015-11-27 19:25:52 +01:00
|
|
|
func (api *Client) SetHeader(key, value string) *Client {
|
2016-02-23 22:16:13 +01:00
|
|
|
api.mu.Lock()
|
|
|
|
defer api.mu.Unlock()
|
2015-11-27 19:25:52 +01:00
|
|
|
api.headers[key] = value
|
|
|
|
return api
|
2015-11-27 13:46:13 +01:00
|
|
|
}
|
|
|
|
|
2018-04-09 10:05:43 +02:00
|
|
|
// RemoveHeader unsets a header for all requests
|
|
|
|
func (api *Client) RemoveHeader(key string) *Client {
|
|
|
|
api.mu.Lock()
|
|
|
|
defer api.mu.Unlock()
|
|
|
|
delete(api.headers, key)
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:16:26 +02:00
|
|
|
// SignerFn is used to sign an outgoing request
|
|
|
|
type SignerFn func(*http.Request) error
|
|
|
|
|
|
|
|
// SetSigner sets a signer for all requests
|
|
|
|
func (api *Client) SetSigner(signer SignerFn) *Client {
|
|
|
|
api.mu.Lock()
|
|
|
|
defer api.mu.Unlock()
|
|
|
|
api.signer = signer
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2017-10-04 22:26:40 +02:00
|
|
|
// SetUserPass creates an Authorization header for all requests with
|
|
|
|
// the UserName and Password passed in
|
|
|
|
func (api *Client) SetUserPass(UserName, Password string) *Client {
|
|
|
|
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
|
|
|
req.SetBasicAuth(UserName, Password)
|
|
|
|
api.SetHeader("Authorization", req.Header.Get("Authorization"))
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// SetCookie creates a Cookies Header for all requests with the supplied
|
2018-04-09 10:05:43 +02:00
|
|
|
// cookies passed in.
|
|
|
|
// All cookies have to be supplied at once, all cookies will be overwritten
|
|
|
|
// on a new call to the method
|
|
|
|
func (api *Client) SetCookie(cks ...*http.Cookie) *Client {
|
|
|
|
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
|
|
|
for _, ck := range cks {
|
|
|
|
req.AddCookie(ck)
|
|
|
|
}
|
|
|
|
api.SetHeader("Cookie", req.Header.Get("Cookie"))
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:07:12 +02:00
|
|
|
// Opts contains parameters for Call, CallJSON, etc.
|
2015-10-04 23:08:31 +02:00
|
|
|
type Opts struct {
|
2020-10-14 00:07:12 +02:00
|
|
|
Method string // GET, POST, etc.
|
2017-07-07 09:18:13 +02:00
|
|
|
Path string // relative to RootURL
|
2017-07-06 23:19:50 +02:00
|
|
|
RootURL string // override RootURL passed into SetRoot()
|
2017-07-05 18:48:43 +02:00
|
|
|
Body io.Reader
|
2022-05-20 11:04:36 +02:00
|
|
|
GetBody func() (io.ReadCloser, error) // body builder, needed to enable low-level HTTP/2 retries
|
|
|
|
NoResponse bool // set to close Body
|
2017-07-05 18:48:43 +02:00
|
|
|
ContentType string
|
|
|
|
ContentLength *int64
|
|
|
|
ContentRange string
|
2019-10-31 12:46:19 +01:00
|
|
|
ExtraHeaders map[string]string // extra headers, start them with "*" for don't canonicalise
|
|
|
|
UserName string // username for Basic Auth
|
|
|
|
Password string // password for Basic Auth
|
2017-07-05 18:48:43 +02:00
|
|
|
Options []fs.OpenOption
|
2021-10-15 00:01:46 +02:00
|
|
|
IgnoreStatus bool // if set then we don't check error status or parse error body
|
|
|
|
MultipartParams url.Values // if set do multipart form upload with attached file
|
|
|
|
MultipartMetadataName string // ..this is used for the name of the metadata form part if set
|
|
|
|
MultipartContentName string // ..name of the parameter which is the attached file
|
|
|
|
MultipartFileName string // ..name of the file for the attached file
|
|
|
|
Parameters url.Values // any parameters for the final URL
|
|
|
|
TransferEncoding []string // transfer encoding, set to "identity" to disable chunked encoding
|
|
|
|
Trailer *http.Header // set the request trailer
|
|
|
|
Close bool // set to close the connection after this transaction
|
|
|
|
NoRedirect bool // if this is set then the client won't follow redirects
|
2017-07-05 18:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy creates a copy of the options
|
|
|
|
func (o *Opts) Copy() *Opts {
|
|
|
|
newOpts := *o
|
|
|
|
return &newOpts
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 11:05:27 +01:00
|
|
|
const drainLimit = 10 * 1024 * 1024
|
|
|
|
|
|
|
|
// drainAndClose discards up to drainLimit bytes from r and closes
|
|
|
|
// it. Any errors from the Read or Close are returned.
|
|
|
|
func drainAndClose(r io.ReadCloser) (err error) {
|
2023-06-23 10:50:01 +02:00
|
|
|
_, readErr := io.CopyN(io.Discard, r, drainLimit)
|
2023-03-24 11:05:27 +01:00
|
|
|
if readErr == io.EOF {
|
|
|
|
readErr = nil
|
|
|
|
}
|
|
|
|
err = r.Close()
|
|
|
|
if readErr != nil {
|
|
|
|
return readErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkDrainAndClose is a utility function used to check the return
|
|
|
|
// from drainAndClose in a defer statement.
|
|
|
|
func checkDrainAndClose(r io.ReadCloser, err *error) {
|
|
|
|
cerr := drainAndClose(r)
|
|
|
|
if *err == nil {
|
|
|
|
*err = cerr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 09:40:14 +01:00
|
|
|
// DecodeJSON decodes resp.Body into result
|
|
|
|
func DecodeJSON(resp *http.Response, result interface{}) (err error) {
|
2023-03-24 11:05:27 +01:00
|
|
|
defer checkDrainAndClose(resp.Body, &err)
|
2015-10-04 23:08:31 +02:00
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
return decoder.Decode(result)
|
|
|
|
}
|
|
|
|
|
2017-07-25 18:38:18 +02:00
|
|
|
// DecodeXML decodes resp.Body into result
|
|
|
|
func DecodeXML(resp *http.Response, result interface{}) (err error) {
|
2023-03-24 11:05:27 +01:00
|
|
|
defer checkDrainAndClose(resp.Body, &err)
|
2017-07-25 18:38:18 +02:00
|
|
|
decoder := xml.NewDecoder(resp.Body)
|
2021-12-01 08:15:05 +01:00
|
|
|
// MEGAcmd has included escaped HTML entities in its XML output, so we have to be able to
|
|
|
|
// decode them.
|
|
|
|
decoder.Strict = false
|
|
|
|
decoder.Entity = xml.HTMLEntity
|
2017-07-25 18:38:18 +02:00
|
|
|
return decoder.Decode(result)
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:21:50 +02:00
|
|
|
// ClientWithNoRedirects makes a new http client which won't follow redirects
|
|
|
|
func ClientWithNoRedirects(c *http.Client) *http.Client {
|
|
|
|
clientCopy := *c
|
|
|
|
clientCopy.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
return &clientCopy
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:08:31 +02:00
|
|
|
// Call makes the call and returns the http.Response
|
|
|
|
//
|
2019-08-28 12:21:38 +02:00
|
|
|
// if err == nil then resp.Body will need to be closed unless
|
2018-10-13 13:32:01 +02:00
|
|
|
// opt.NoResponse is set
|
2015-10-04 23:08:31 +02:00
|
|
|
//
|
2019-08-28 12:21:38 +02:00
|
|
|
// if err != nil then resp.Body will have been closed
|
|
|
|
//
|
2015-10-04 23:08:31 +02:00
|
|
|
// it will return resp if at all possible, even if err is set
|
2019-09-04 21:00:37 +02:00
|
|
|
func (api *Client) Call(ctx context.Context, opts *Opts) (resp *http.Response, err error) {
|
2016-02-23 22:16:13 +01:00
|
|
|
api.mu.RLock()
|
|
|
|
defer api.mu.RUnlock()
|
2015-10-04 23:08:31 +02:00
|
|
|
if opts == nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.New("call() called with nil opts")
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
2017-07-07 09:18:13 +02:00
|
|
|
url := api.rootURL
|
|
|
|
if opts.RootURL != "" {
|
|
|
|
url = opts.RootURL
|
|
|
|
}
|
|
|
|
if url == "" {
|
|
|
|
return nil, errors.New("RootURL not set")
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
2017-07-07 09:18:13 +02:00
|
|
|
url += opts.Path
|
2017-07-06 13:12:01 +02:00
|
|
|
if opts.Parameters != nil && len(opts.Parameters) > 0 {
|
|
|
|
url += "?" + opts.Parameters.Encode()
|
|
|
|
}
|
2019-08-26 13:18:36 +02:00
|
|
|
body := readers.NoCloser(opts.Body)
|
2019-01-23 11:59:19 +01:00
|
|
|
// If length is set and zero then nil out the body to stop use
|
|
|
|
// use of chunked encoding and insert a "Content-Length: 0"
|
|
|
|
// header.
|
|
|
|
//
|
|
|
|
// If we don't do this we get "Content-Length" headers for all
|
|
|
|
// files except 0 length files.
|
|
|
|
if opts.ContentLength != nil && *opts.ContentLength == 0 {
|
|
|
|
body = nil
|
|
|
|
}
|
2021-02-03 18:41:27 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, opts.Method, url, body)
|
2015-10-04 23:08:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-11-27 19:25:52 +01:00
|
|
|
headers := make(map[string]string)
|
|
|
|
// Set default headers
|
|
|
|
for k, v := range api.headers {
|
|
|
|
headers[k] = v
|
|
|
|
}
|
2015-10-04 23:08:31 +02:00
|
|
|
if opts.ContentType != "" {
|
2015-11-27 19:25:52 +01:00
|
|
|
headers["Content-Type"] = opts.ContentType
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
|
|
|
if opts.ContentLength != nil {
|
|
|
|
req.ContentLength = *opts.ContentLength
|
|
|
|
}
|
|
|
|
if opts.ContentRange != "" {
|
2015-11-27 19:25:52 +01:00
|
|
|
headers["Content-Range"] = opts.ContentRange
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
2017-09-20 11:53:20 +02:00
|
|
|
if len(opts.TransferEncoding) != 0 {
|
|
|
|
req.TransferEncoding = opts.TransferEncoding
|
|
|
|
}
|
2022-05-20 11:04:36 +02:00
|
|
|
if opts.GetBody != nil {
|
|
|
|
req.GetBody = opts.GetBody
|
|
|
|
}
|
2021-10-15 00:01:46 +02:00
|
|
|
if opts.Trailer != nil {
|
|
|
|
req.Trailer = *opts.Trailer
|
|
|
|
}
|
2017-09-20 11:53:20 +02:00
|
|
|
if opts.Close {
|
|
|
|
req.Close = true
|
|
|
|
}
|
2015-11-27 19:25:52 +01:00
|
|
|
// Set any extra headers
|
2023-08-08 13:55:44 +02:00
|
|
|
for k, v := range opts.ExtraHeaders {
|
|
|
|
headers[k] = v
|
2015-10-30 09:40:14 +01:00
|
|
|
}
|
2016-09-10 12:29:57 +02:00
|
|
|
// add any options to the headers
|
|
|
|
fs.OpenOptionAddHeaders(opts.Options, headers)
|
2015-11-27 19:25:52 +01:00
|
|
|
// Now set the headers
|
|
|
|
for k, v := range headers {
|
2019-10-31 12:46:19 +01:00
|
|
|
if k != "" && v != "" {
|
|
|
|
if k[0] == '*' {
|
|
|
|
// Add non-canonical version if header starts with *
|
|
|
|
k = k[1:]
|
|
|
|
req.Header[k] = append(req.Header[k], v)
|
|
|
|
} else {
|
|
|
|
req.Header.Add(k, v)
|
|
|
|
}
|
2016-02-23 22:16:13 +01:00
|
|
|
}
|
2015-11-27 19:25:52 +01:00
|
|
|
}
|
2019-10-31 12:46:19 +01:00
|
|
|
|
2015-11-27 19:25:52 +01:00
|
|
|
if opts.UserName != "" || opts.Password != "" {
|
|
|
|
req.SetBasicAuth(opts.UserName, opts.Password)
|
|
|
|
}
|
2018-06-18 13:21:50 +02:00
|
|
|
var c *http.Client
|
|
|
|
if opts.NoRedirect {
|
|
|
|
c = ClientWithNoRedirects(api.c)
|
|
|
|
} else {
|
2019-05-07 14:53:16 +02:00
|
|
|
c = api.c
|
2018-06-18 13:21:50 +02:00
|
|
|
}
|
2017-07-25 20:16:26 +02:00
|
|
|
if api.signer != nil {
|
2019-11-02 11:16:58 +01:00
|
|
|
api.mu.RUnlock()
|
2017-07-25 20:16:26 +02:00
|
|
|
err = api.signer(req)
|
2019-11-02 11:16:58 +01:00
|
|
|
api.mu.RLock()
|
2017-07-25 20:16:26 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("signer failed: %w", err)
|
2017-07-25 20:16:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 22:16:13 +01:00
|
|
|
api.mu.RUnlock()
|
2016-09-10 12:29:57 +02:00
|
|
|
resp, err = c.Do(req)
|
2016-02-23 22:16:13 +01:00
|
|
|
api.mu.RLock()
|
2015-10-04 23:08:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-12 12:44:43 +01:00
|
|
|
if !opts.IgnoreStatus {
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
2018-03-13 17:05:06 +01:00
|
|
|
err = api.errorHandler(resp)
|
|
|
|
if err.Error() == "" {
|
|
|
|
// replace empty errors with something
|
2021-11-04 11:12:57 +01:00
|
|
|
err = fmt.Errorf("http error %d: %v", resp.StatusCode, resp.Status)
|
2018-03-13 17:05:06 +01:00
|
|
|
}
|
|
|
|
return resp, err
|
2017-03-12 12:44:43 +01:00
|
|
|
}
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
|
|
|
if opts.NoResponse {
|
2023-03-24 11:05:27 +01:00
|
|
|
return resp, drainAndClose(resp.Body)
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-09-21 11:18:34 +02:00
|
|
|
// MultipartUpload creates an io.Reader which produces an encoded a
|
|
|
|
// multipart form upload from the params passed in and the passed in
|
|
|
|
//
|
2019-07-06 15:36:17 +02:00
|
|
|
// in - the body of the file (may be nil)
|
2017-09-21 11:18:34 +02:00
|
|
|
// params - the form parameters
|
|
|
|
// fileName - is the name of the attached file
|
|
|
|
// contentName - the name of the parameter for the file
|
|
|
|
//
|
2019-07-06 15:36:17 +02:00
|
|
|
// the int64 returned is the overhead in addition to the file contents, in case Content-Length is required
|
2019-06-24 20:34:49 +02:00
|
|
|
//
|
2017-09-21 11:18:34 +02:00
|
|
|
// NB This doesn't allow setting the content type of the attachment
|
2021-03-25 16:35:08 +01:00
|
|
|
func MultipartUpload(ctx context.Context, in io.Reader, params url.Values, contentName, fileName string) (io.ReadCloser, string, int64, error) {
|
2017-09-21 11:18:34 +02:00
|
|
|
bodyReader, bodyWriter := io.Pipe()
|
|
|
|
writer := multipart.NewWriter(bodyWriter)
|
|
|
|
contentType := writer.FormDataContentType()
|
|
|
|
|
2019-06-24 20:34:49 +02:00
|
|
|
// Create a Multipart Writer as base for calculating the Content-Length
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
dummyMultipartWriter := multipart.NewWriter(buf)
|
|
|
|
err := dummyMultipartWriter.SetBoundary(writer.Boundary())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, vals := range params {
|
|
|
|
for _, val := range vals {
|
|
|
|
err := dummyMultipartWriter.WriteField(key, val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 15:36:17 +02:00
|
|
|
if in != nil {
|
|
|
|
_, err = dummyMultipartWriter.CreateFormFile(contentName, fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
2019-06-24 20:34:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = dummyMultipartWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multipartLength := int64(buf.Len())
|
|
|
|
|
2021-03-25 16:35:08 +01:00
|
|
|
// Make sure we close the pipe writer to release the reader on context cancel
|
|
|
|
quit := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
_ = bodyWriter.CloseWithError(ctx.Err())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-09-21 11:18:34 +02:00
|
|
|
// Pump the data in the background
|
|
|
|
go func() {
|
2021-03-25 16:35:08 +01:00
|
|
|
defer close(quit)
|
|
|
|
|
2017-09-21 11:18:34 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
for key, vals := range params {
|
|
|
|
for _, val := range vals {
|
|
|
|
err = writer.WriteField(key, val)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
_ = bodyWriter.CloseWithError(fmt.Errorf("create metadata part: %w", err))
|
2017-09-21 11:18:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 15:36:17 +02:00
|
|
|
if in != nil {
|
|
|
|
part, err := writer.CreateFormFile(contentName, fileName)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
_ = bodyWriter.CloseWithError(fmt.Errorf("failed to create form file: %w", err))
|
2019-07-06 15:36:17 +02:00
|
|
|
return
|
|
|
|
}
|
2017-09-21 11:18:34 +02:00
|
|
|
|
2019-07-06 15:36:17 +02:00
|
|
|
_, err = io.Copy(part, in)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
_ = bodyWriter.CloseWithError(fmt.Errorf("failed to copy data: %w", err))
|
2019-07-06 15:36:17 +02:00
|
|
|
return
|
|
|
|
}
|
2017-09-21 11:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
_ = bodyWriter.CloseWithError(fmt.Errorf("failed to close form: %w", err))
|
2017-09-21 11:18:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = bodyWriter.Close()
|
|
|
|
}()
|
|
|
|
|
2019-06-24 20:34:49 +02:00
|
|
|
return bodyReader, contentType, multipartLength, nil
|
2017-09-21 11:18:34 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 09:40:14 +01:00
|
|
|
// CallJSON runs Call and decodes the body as a JSON object into response (if not nil)
|
2015-10-04 23:08:31 +02:00
|
|
|
//
|
2022-08-05 17:35:41 +02:00
|
|
|
// If request is not nil then it will be JSON encoded as the body of the request.
|
2015-10-04 23:08:31 +02:00
|
|
|
//
|
2018-10-13 13:32:01 +02:00
|
|
|
// If response is not nil then the response will be JSON decoded into
|
|
|
|
// it and resp.Body will be closed.
|
|
|
|
//
|
|
|
|
// If response is nil then the resp.Body will be closed only if
|
|
|
|
// opts.NoResponse is set.
|
|
|
|
//
|
2017-09-21 11:18:34 +02:00
|
|
|
// If (opts.MultipartParams or opts.MultipartContentName) and
|
|
|
|
// opts.Body are set then CallJSON will do a multipart upload with a
|
|
|
|
// file attached. opts.MultipartContentName is the name of the
|
|
|
|
// parameter and opts.MultipartFileName is the name of the file. If
|
2022-08-14 04:56:32 +02:00
|
|
|
// MultipartContentName is set, and request != nil is supplied, then
|
2017-09-21 11:18:34 +02:00
|
|
|
// the request will be marshalled into JSON and added to the form with
|
|
|
|
// parameter name MultipartMetadataName.
|
|
|
|
//
|
2015-10-04 23:08:31 +02:00
|
|
|
// It will return resp if at all possible, even if err is set
|
2019-09-04 21:00:37 +02:00
|
|
|
func (api *Client) CallJSON(ctx context.Context, opts *Opts, request interface{}, response interface{}) (resp *http.Response, err error) {
|
|
|
|
return api.callCodec(ctx, opts, request, response, json.Marshal, DecodeJSON, "application/json")
|
2017-07-25 18:38:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// CallXML runs Call and decodes the body as an XML object into response (if not nil)
|
2017-07-25 18:38:18 +02:00
|
|
|
//
|
2022-08-05 17:35:41 +02:00
|
|
|
// If request is not nil then it will be XML encoded as the body of the request.
|
2017-07-25 18:38:18 +02:00
|
|
|
//
|
2018-10-13 13:32:01 +02:00
|
|
|
// If response is not nil then the response will be XML decoded into
|
|
|
|
// it and resp.Body will be closed.
|
|
|
|
//
|
|
|
|
// If response is nil then the resp.Body will be closed only if
|
|
|
|
// opts.NoResponse is set.
|
|
|
|
//
|
2022-08-05 17:35:41 +02:00
|
|
|
// See CallJSON for a description of MultipartParams and related opts.
|
2017-07-25 18:38:18 +02:00
|
|
|
//
|
|
|
|
// It will return resp if at all possible, even if err is set
|
2019-09-04 21:00:37 +02:00
|
|
|
func (api *Client) CallXML(ctx context.Context, opts *Opts, request interface{}, response interface{}) (resp *http.Response, err error) {
|
|
|
|
return api.callCodec(ctx, opts, request, response, xml.Marshal, DecodeXML, "application/xml")
|
2017-07-25 18:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type marshalFn func(v interface{}) ([]byte, error)
|
|
|
|
type decodeFn func(resp *http.Response, result interface{}) (err error)
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (api *Client) callCodec(ctx context.Context, opts *Opts, request interface{}, response interface{}, marshal marshalFn, decode decodeFn, contentType string) (resp *http.Response, err error) {
|
2017-07-05 18:48:43 +02:00
|
|
|
var requestBody []byte
|
|
|
|
// Marshal the request if given
|
|
|
|
if request != nil {
|
2017-07-25 18:38:18 +02:00
|
|
|
requestBody, err = marshal(request)
|
2015-10-04 23:08:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-25 18:38:18 +02:00
|
|
|
// Set the body up as a marshalled object if no body passed in
|
2017-07-05 18:48:43 +02:00
|
|
|
if opts.Body == nil {
|
2017-09-21 11:18:34 +02:00
|
|
|
opts = opts.Copy()
|
2017-07-25 18:38:18 +02:00
|
|
|
opts.ContentType = contentType
|
2017-07-05 18:48:43 +02:00
|
|
|
opts.Body = bytes.NewBuffer(requestBody)
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 15:36:17 +02:00
|
|
|
if opts.MultipartParams != nil || opts.MultipartContentName != "" {
|
2017-09-21 11:18:34 +02:00
|
|
|
params := opts.MultipartParams
|
|
|
|
if params == nil {
|
|
|
|
params = url.Values{}
|
|
|
|
}
|
|
|
|
if opts.MultipartMetadataName != "" {
|
|
|
|
params.Add(opts.MultipartMetadataName, string(requestBody))
|
|
|
|
}
|
|
|
|
opts = opts.Copy()
|
2019-06-24 20:34:49 +02:00
|
|
|
|
|
|
|
var overhead int64
|
2021-03-25 16:35:08 +01:00
|
|
|
opts.Body, opts.ContentType, overhead, err = MultipartUpload(ctx, opts.Body, params, opts.MultipartContentName, opts.MultipartFileName)
|
2019-09-14 00:50:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-24 20:34:49 +02:00
|
|
|
if opts.ContentLength != nil {
|
|
|
|
*opts.ContentLength += overhead
|
2017-09-21 11:18:34 +02:00
|
|
|
}
|
2015-10-04 23:08:31 +02:00
|
|
|
}
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err = api.Call(ctx, opts)
|
2015-10-04 23:08:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
2018-10-13 13:32:01 +02:00
|
|
|
// if opts.NoResponse is set, resp.Body will have been closed by Call()
|
2015-11-27 19:25:52 +01:00
|
|
|
if response == nil || opts.NoResponse {
|
2015-10-30 09:40:14 +01:00
|
|
|
return resp, nil
|
|
|
|
}
|
2017-07-25 18:38:18 +02:00
|
|
|
err = decode(resp, response)
|
2015-10-04 23:08:31 +02:00
|
|
|
return resp, err
|
|
|
|
}
|