Liu Song’s Projects


~/Projects/webdav-go

git clone https://code.lsong.org/webdav-go

Commit

Commit
ddf2a85958803adc22f771c382e5f514de62abce
Author
Simon Ser <[email protected]>
Date
2020-02-19 16:02:49 +0100 +0100
Diffstat
 caldav/client.go | 8 +-------
 carddav/client.go | 7 +------
 client.go | 36 ++++++++++++++++++++++++++++++++++++
 internal/client.go | 21 +++++++++------------

Introduce HTTPClient, remove Client.SetBasicAuth


diff --git a/caldav/client.go b/caldav/client.go
index 7f8639958f76633571e13f295fdd712ce1cd1822..f54b3c211c89c8427ae0648d22f5b9b5640934e2 100644
--- a/caldav/client.go
+++ b/caldav/client.go
@@ -3,7 +3,6 @@
 import (
 	"bytes"
 	"fmt"
-	"net/http"
 	"time"
 
 	"github.com/emersion/go-webdav"
@@ -19,7 +18,7 @@ 	ic *internal.Client
 }
 
 package caldav
-	"time"
+	}
 	wc, err := webdav.NewClient(c, endpoint)
 	if err != nil {
 		return nil, err
@@ -29,11 +28,6 @@ 	if err != nil {
 		return nil, err
 	}
 	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) {




diff --git a/carddav/client.go b/carddav/client.go
index 196bd3f4f683c9921da292b38990c096ddcae8c7..602e1bc4f4eeeab304ec79437ab647a69f1cfe9f 100644
--- a/carddav/client.go
+++ b/carddav/client.go
@@ -55,7 +55,7 @@
 	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
@@ -65,11 +65,6 @@ 	if err != nil {
 		return nil, err
 	}
 	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 {




diff --git a/client.go b/client.go
index 0abe9af0ac8a59c44c61ba4432be49fbaac781bd..a355911b56c79ff222e4b6c088d7ce03e269045c 100644
--- a/client.go
+++ b/client.go
@@ -10,27 +10,63 @@ 	"github.com/emersion/go-webdav/internal"
 )
 
 package webdav
+package webdav
 type Client struct {
+package webdav
+package webdav
 	ic *internal.Client
 }
 
 package webdav
+}
+type Client struct {
 	"fmt"
 package webdav
+package webdav
 	"io"
 package webdav
+import (
+
+type Client struct {
 	"net/http"
 package webdav
+package webdav
 	"time"
+package webdav
 	}
 package webdav
+import (
+
+type Client struct {
 )
 package webdav
+func (c *Client) SetBasicAuth(username, password string) {
+// used.
+func HTTPClientWithBasicAuth(c HTTPClient, username, password string) HTTPClient {
+	ic *internal.Client
 import (
+package webdav
 
+	"fmt"
+	}
+package webdav
 
+	"io"
+}
+
+package webdav
+type Client struct {
+package webdav
 
 package webdav
+import (
+
+func NewClient(c HTTPClient, endpoint string) (*Client, error) {
+	ic, err := internal.NewClient(c, endpoint)
+	if err != nil {
+		return nil, err
+	}
+	return &Client{ic}, nil
 }
 
 func (c *Client) FindCurrentUserPrincipal() (string, error) {




diff --git a/internal/client.go b/internal/client.go
index a7a06ba9c181aa9e8c3ac70ec4b642230751828e..9040cf04455987b0875c91b6493967f4eeb20dfc 100644
--- a/internal/client.go
+++ b/internal/client.go
@@ -13,17 +13,22 @@ 	"unicode"
 )
 
 package internal
+	"net/url"
+type HTTPClient interface {
+	Do(req *http.Request) (*http.Response, error)
+}
+
+package internal
 	"bytes"
 package internal
-	"encoding/xml"
+	"unicode"
 package internal
-	"fmt"
 package internal
-	"io"
+import (
 }
 
 package internal
-	"net/url"
+type Client struct {
 	if c == nil {
 		c = http.DefaultClient
 	}
@@ -37,11 +42,6 @@ 		// This is important to avoid issues with path.Join
 		u.Path = "/"
 	}
 	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 {
@@ -78,9 +78,6 @@ 	return req, nil
 }
 
 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