mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 22:14:05 +01:00
rest: add context propagation to rest library #3257
This fixes up the calling and propagates the contexts for the backends which use lib/rest.
This commit is contained in:
@@ -5,6 +5,7 @@ package rest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
@@ -184,7 +185,7 @@ func ClientWithNoRedirects(c *http.Client) *http.Client {
|
||||
// if err != nil then resp.Body will have been closed
|
||||
//
|
||||
// it will return resp if at all possible, even if err is set
|
||||
func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
|
||||
func (api *Client) Call(ctx context.Context, opts *Opts) (resp *http.Response, err error) {
|
||||
api.mu.RLock()
|
||||
defer api.mu.RUnlock()
|
||||
if opts == nil {
|
||||
@@ -211,7 +212,7 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
|
||||
if opts.ContentLength != nil && *opts.ContentLength == 0 {
|
||||
body = nil
|
||||
}
|
||||
req, err := http.NewRequest(opts.Method, url, body)
|
||||
req, err := http.NewRequestWithContext(ctx, opts.Method, url, body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -391,8 +392,8 @@ func MultipartUpload(in io.Reader, params url.Values, contentName, fileName stri
|
||||
// parameter name MultipartMetadataName.
|
||||
//
|
||||
// It will return resp if at all possible, even if err is set
|
||||
func (api *Client) CallJSON(opts *Opts, request interface{}, response interface{}) (resp *http.Response, err error) {
|
||||
return api.callCodec(opts, request, response, json.Marshal, DecodeJSON, "application/json")
|
||||
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")
|
||||
}
|
||||
|
||||
// CallXML runs Call and decodes the body as a XML object into response (if not nil)
|
||||
@@ -408,14 +409,14 @@ func (api *Client) CallJSON(opts *Opts, request interface{}, response interface{
|
||||
// See CallJSON for a description of MultipartParams and related opts
|
||||
//
|
||||
// It will return resp if at all possible, even if err is set
|
||||
func (api *Client) CallXML(opts *Opts, request interface{}, response interface{}) (resp *http.Response, err error) {
|
||||
return api.callCodec(opts, request, response, xml.Marshal, DecodeXML, "application/xml")
|
||||
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")
|
||||
}
|
||||
|
||||
type marshalFn func(v interface{}) ([]byte, error)
|
||||
type decodeFn func(resp *http.Response, result interface{}) (err error)
|
||||
|
||||
func (api *Client) callCodec(opts *Opts, request interface{}, response interface{}, marshal marshalFn, decode decodeFn, contentType string) (resp *http.Response, err error) {
|
||||
func (api *Client) callCodec(ctx context.Context, opts *Opts, request interface{}, response interface{}, marshal marshalFn, decode decodeFn, contentType string) (resp *http.Response, err error) {
|
||||
var requestBody []byte
|
||||
// Marshal the request if given
|
||||
if request != nil {
|
||||
@@ -446,7 +447,7 @@ func (api *Client) callCodec(opts *Opts, request interface{}, response interface
|
||||
*opts.ContentLength += overhead
|
||||
}
|
||||
}
|
||||
resp, err = api.Call(opts)
|
||||
resp, err = api.Call(ctx, opts)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user