Introduce HTTPClient, remove Client.SetBasicAuth

This commit is contained in:
Simon Ser 2020-02-19 16:02:49 +01:00
parent c52097fefb
commit ddf2a85958
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 37 additions and 31 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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)

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