mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
Introduce HTTPClient, remove Client.SetBasicAuth
This commit is contained in:
parent
c52097fefb
commit
ddf2a85958
@ -3,7 +3,6 @@ package caldav
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/emersion/go-webdav"
|
"github.com/emersion/go-webdav"
|
||||||
@ -18,7 +17,7 @@ type Client struct {
|
|||||||
ic *internal.Client
|
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)
|
wc, err := webdav.NewClient(c, endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -30,11 +29,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|||||||
return &Client{wc, ic}, nil
|
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) {
|
func (c *Client) FindCalendarHomeSet(principal string) (string, error) {
|
||||||
propfind := internal.NewPropNamePropfind(calendarHomeSetName)
|
propfind := internal.NewPropNamePropfind(calendarHomeSetName)
|
||||||
resp, err := c.ic.PropfindFlat(principal, propfind)
|
resp, err := c.ic.PropfindFlat(principal, propfind)
|
||||||
|
@ -55,7 +55,7 @@ type Client struct {
|
|||||||
ic *internal.Client
|
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)
|
wc, err := webdav.NewClient(c, endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -67,11 +67,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|||||||
return &Client{wc, ic}, nil
|
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 {
|
func (c *Client) HasSupport() error {
|
||||||
classes, _, err := c.ic.Options("")
|
classes, _, err := c.ic.Options("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
31
client.go
31
client.go
@ -9,12 +9,37 @@ import (
|
|||||||
"github.com/emersion/go-webdav/internal"
|
"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.
|
// Client provides access to a remote WebDAV filesystem.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ic *internal.Client
|
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)
|
ic, err := internal.NewClient(c, endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -22,10 +47,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|||||||
return &Client{ic}, nil
|
return &Client{ic}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetBasicAuth(username, password string) {
|
|
||||||
c.ic.SetBasicAuth(username, password)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) FindCurrentUserPrincipal() (string, error) {
|
func (c *Client) FindCurrentUserPrincipal() (string, error) {
|
||||||
propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName)
|
propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName)
|
||||||
|
|
||||||
|
@ -12,13 +12,17 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
// HTTPClient performs HTTP requests. It's implemented by *http.Client.
|
||||||
http *http.Client
|
type HTTPClient interface {
|
||||||
endpoint *url.URL
|
Do(req *http.Request) (*http.Response, error)
|
||||||
username, password string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if c == nil {
|
||||||
c = http.DefaultClient
|
c = http.DefaultClient
|
||||||
}
|
}
|
||||||
@ -34,11 +38,6 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|||||||
return &Client{http: c, endpoint: u}, nil
|
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 {
|
func (c *Client) ResolveHref(p string) *url.URL {
|
||||||
if !strings.HasPrefix(p, "/") {
|
if !strings.HasPrefix(p, "/") {
|
||||||
p = path.Join(c.endpoint.Path, 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) {
|
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)
|
resp, err := c.http.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user