rest: Add RemoveHeader and SetCookie method

These methods extend the rest package to support the cookie header and
header deletion.

The deletion is necessary to delete an existing authorization header if
cookie auth should be used.
This commit is contained in:
hensur 2018-04-09 10:05:43 +02:00 committed by Nick Craig-Wood
parent dc59836021
commit ba7ae2ee8c

View File

@ -80,6 +80,14 @@ func (api *Client) SetHeader(key, value string) *Client {
return api
}
// 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
}
// SignerFn is used to sign an outgoing request
type SignerFn func(*http.Request) error
@ -100,6 +108,19 @@ func (api *Client) SetUserPass(UserName, Password string) *Client {
return api
}
// SetCookie creates an Cookies Header for all requests with the supplied
// 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
}
// Opts contains parameters for Call, CallJSON etc
type Opts struct {
Method string // GET, POST etc