Introduce HTTPClient, remove Client.SetBasicAuth

This commit is contained in:
Simon Ser
2020-02-19 16:02:49 +01:00
parent c52097fefb
commit ddf2a85958
4 changed files with 37 additions and 31 deletions

View File

@@ -12,13 +12,17 @@ import (
"unicode"
)
type Client struct {
http *http.Client
endpoint *url.URL
username, password string
// HTTPClient performs HTTP requests. It's implemented by *http.Client.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
func NewClient(c *http.Client, endpoint string) (*Client, error) {
type Client struct {
http HTTPClient
endpoint *url.URL
}
func NewClient(c HTTPClient, endpoint string) (*Client, error) {
if c == nil {
c = http.DefaultClient
}
@@ -34,11 +38,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
return &Client{http: c, endpoint: u}, nil
}
func (c *Client) SetBasicAuth(username, password string) {
c.username = username
c.password = password
}
func (c *Client) ResolveHref(p string) *url.URL {
if !strings.HasPrefix(p, "/") {
p = path.Join(c.endpoint.Path, p)
@@ -73,9 +72,6 @@ func (c *Client) NewXMLRequest(method string, path string, v interface{}) (*http
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
if c.username != "" || c.password != "" {
req.SetBasicAuth(c.username, c.password)
}
resp, err := c.http.Do(req)
if err != nil {
return nil, err