rest: add a Signer callback

This commit is contained in:
Nick Craig-Wood 2017-07-25 19:16:26 +01:00
parent efd88c5676
commit 2453abfbea

View File

@ -25,6 +25,7 @@ type Client struct {
rootURL string
errorHandler func(resp *http.Response) error
headers map[string]string
signer SignerFn
}
// 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
}
// 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
type Opts struct {
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)
}
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()
resp, err = c.Do(req)
api.mu.RLock()