From ac8746b2cfaf20b26a14b0596e2b902dbd92bfa0 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sun, 14 Apr 2019 22:29:52 +0200 Subject: [PATCH] Add option for client with custom http client (#159) --- gitea/gitea.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gitea/gitea.go b/gitea/gitea.go index d90a6eb..fa35e09 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -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 } - req.Header.Set("Authorization", "token "+c.accessToken) + if len(c.accessToken) != 0 { + req.Header.Set("Authorization", "token "+c.accessToken) + } if c.sudo != "" { req.Header.Set("Sudo", c.sudo) }