From ddf2a85958803adc22f771c382e5f514de62abce Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 19 Feb 2020 16:02:49 +0100 Subject: [PATCH] Introduce HTTPClient, remove Client.SetBasicAuth --- caldav/client.go | 8 +------- carddav/client.go | 7 +------ client.go | 31 ++++++++++++++++++++++++++----- internal/client.go | 22 +++++++++------------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/caldav/client.go b/caldav/client.go index 7f86399..f54b3c2 100644 --- a/caldav/client.go +++ b/caldav/client.go @@ -3,7 +3,6 @@ package caldav import ( "bytes" "fmt" - "net/http" "time" "github.com/emersion/go-webdav" @@ -18,7 +17,7 @@ type Client struct { ic *internal.Client } -func NewClient(c *http.Client, endpoint string) (*Client, error) { +func NewClient(c webdav.HTTPClient, endpoint string) (*Client, error) { wc, err := webdav.NewClient(c, endpoint) if err != nil { return nil, err @@ -30,11 +29,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) { return &Client{wc, ic}, nil } -func (c *Client) SetBasicAuth(username, password string) { - c.Client.SetBasicAuth(username, password) - c.ic.SetBasicAuth(username, password) -} - func (c *Client) FindCalendarHomeSet(principal string) (string, error) { propfind := internal.NewPropNamePropfind(calendarHomeSetName) resp, err := c.ic.PropfindFlat(principal, propfind) diff --git a/carddav/client.go b/carddav/client.go index 196bd3f..602e1bc 100644 --- a/carddav/client.go +++ b/carddav/client.go @@ -55,7 +55,7 @@ type Client struct { ic *internal.Client } -func NewClient(c *http.Client, endpoint string) (*Client, error) { +func NewClient(c webdav.HTTPClient, endpoint string) (*Client, error) { wc, err := webdav.NewClient(c, endpoint) if err != nil { return nil, err @@ -67,11 +67,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) { return &Client{wc, ic}, nil } -func (c *Client) SetBasicAuth(username, password string) { - c.Client.SetBasicAuth(username, password) - c.ic.SetBasicAuth(username, password) -} - func (c *Client) HasSupport() error { classes, _, err := c.ic.Options("") if err != nil { diff --git a/client.go b/client.go index 0abe9af..a355911 100644 --- a/client.go +++ b/client.go @@ -9,12 +9,37 @@ import ( "github.com/emersion/go-webdav/internal" ) +// HTTPClient performs HTTP requests. It's implemented by *http.Client. +type HTTPClient interface { + Do(req *http.Request) (*http.Response, error) +} + +type basicAuthHTTPClient struct { + c HTTPClient + username, password string +} + +func (c *basicAuthHTTPClient) Do(req *http.Request) (*http.Response, error) { + req.SetBasicAuth(c.username, c.password) + return c.c.Do(req) +} + +// HTTPClientWithBasicAuth returns an HTTP client that adds basic +// authentication to all outgoing requests. If c is nil, http.DefaultClient is +// used. +func HTTPClientWithBasicAuth(c HTTPClient, username, password string) HTTPClient { + if c == nil { + c = http.DefaultClient + } + return &basicAuthHTTPClient{c, username, password} +} + // Client provides access to a remote WebDAV filesystem. type Client struct { ic *internal.Client } -func NewClient(c *http.Client, endpoint string) (*Client, error) { +func NewClient(c HTTPClient, endpoint string) (*Client, error) { ic, err := internal.NewClient(c, endpoint) if err != nil { return nil, err @@ -22,10 +47,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) { return &Client{ic}, nil } -func (c *Client) SetBasicAuth(username, password string) { - c.ic.SetBasicAuth(username, password) -} - func (c *Client) FindCurrentUserPrincipal() (string, error) { propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName) diff --git a/internal/client.go b/internal/client.go index a7a06ba..9040cf0 100644 --- a/internal/client.go +++ b/internal/client.go @@ -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