forked from TrueCloudLab/rclone
rest: add a Signer callback
This commit is contained in:
parent
efd88c5676
commit
2453abfbea
1 changed files with 18 additions and 0 deletions
18
rest/rest.go
18
rest/rest.go
|
@ -25,6 +25,7 @@ type Client struct {
|
||||||
rootURL string
|
rootURL string
|
||||||
errorHandler func(resp *http.Response) error
|
errorHandler func(resp *http.Response) error
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
|
signer SignerFn
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient takes an oauth http.Client and makes a new api instance
|
// NewClient takes an oauth http.Client and makes a new api instance
|
||||||
|
@ -79,6 +80,17 @@ func (api *Client) SetHeader(key, value string) *Client {
|
||||||
return api
|
return api
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// Opts contains parameters for Call, CallJSON etc
|
// Opts contains parameters for Call, CallJSON etc
|
||||||
type Opts struct {
|
type Opts struct {
|
||||||
Method string // GET, POST etc
|
Method string // GET, POST etc
|
||||||
|
@ -211,6 +223,12 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
|
||||||
req.SetBasicAuth(opts.UserName, opts.Password)
|
req.SetBasicAuth(opts.UserName, opts.Password)
|
||||||
}
|
}
|
||||||
c := ClientWithHeaderReset(api.c, headers)
|
c := ClientWithHeaderReset(api.c, headers)
|
||||||
|
if api.signer != nil {
|
||||||
|
err = api.signer(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "signer failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
api.mu.RUnlock()
|
api.mu.RUnlock()
|
||||||
resp, err = c.Do(req)
|
resp, err = c.Do(req)
|
||||||
api.mu.RLock()
|
api.mu.RLock()
|
||||||
|
|
Loading…
Add table
Reference in a new issue