Add Client.SetBasicAuth

This commit is contained in:
Simon Ser 2020-01-15 23:45:37 +01:00
parent dbdd296d38
commit cabf33219e
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 22 additions and 5 deletions

View File

@ -28,6 +28,11 @@ 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) FindAddressBookHomeSet(principal string) (string, error) {
name := xml.Name{namespace, "addressbook-home-set"}
propfind := internal.NewPropNamePropfind(name)

View File

@ -19,6 +19,10 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
return &Client{ic}, nil
}
func (c *Client) SetBasicAuth(username, password string) {
c.c.SetBasicAuth(username, password)
}
func (c *Client) FindCurrentUserPrincipal() (string, error) {
name := xml.Name{"DAV:", "current-user-principal"}
propfind := internal.NewPropNamePropfind(name)

View File

@ -54,6 +54,8 @@ func (d Depth) String() string {
type Client struct {
http *http.Client
endpoint *url.URL
username, password string
insecure bool
}
func NewClient(c *http.Client, endpoint string) (*Client, error) {
@ -65,7 +67,12 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
if err != nil {
return nil, err
}
return &Client{c, 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) NewRequest(method string, href string, body io.Reader) (*http.Request, error) {
@ -96,8 +103,9 @@ func (c *Client) NewXMLRequest(method string, href string, v interface{}) (*http
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
// TODO: remove this quirk
req.SetBasicAuth("emersion", "")
if c.username != "" || c.password != "" {
req.SetBasicAuth(c.username, c.password)
}
return c.http.Do(req)
}