Add option for client with custom http client (#159)

This commit is contained in:
Jonas Franz 2019-04-14 22:29:52 +02:00 committed by Lauris BH
parent a669487e86
commit ac8746b2cf

View file

@ -36,6 +36,12 @@ func NewClient(url, token string) *Client {
}
}
// NewClientWithHTTP creates an API client with a custom http client
func NewClientWithHTTP(url string, httpClient *http.Client) {
client := NewClient(url, "")
client.client = httpClient
}
// SetHTTPClient replaces default http.Client with user given one.
func (c *Client) SetHTTPClient(client *http.Client) {
c.client = client
@ -51,7 +57,9 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
if err != nil {
return nil, err
}
if len(c.accessToken) != 0 {
req.Header.Set("Authorization", "token "+c.accessToken)
}
if c.sudo != "" {
req.Header.Set("Sudo", c.sudo)
}