go-webdav/client.go

44 lines
857 B
Go
Raw Normal View History

2020-01-14 17:51:17 +00:00
package webdav
import (
"net/http"
"fmt"
2020-01-14 17:51:17 +00:00
"github.com/emersion/go-webdav/internal"
)
type Client struct {
c *internal.Client
}
func NewClient(c *http.Client, endpoint string) (*Client, error) {
ic, err := internal.NewClient(c, endpoint)
if err != nil {
return nil, err
}
return &Client{ic}, nil
}
2020-01-15 22:45:37 +00:00
func (c *Client) SetBasicAuth(username, password string) {
c.c.SetBasicAuth(username, password)
}
2020-01-14 17:51:17 +00:00
func (c *Client) FindCurrentUserPrincipal() (string, error) {
propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName)
2020-01-14 20:43:09 +00:00
resp, err := c.c.PropfindFlat("/", propfind)
if err != nil {
return "", err
2020-01-14 17:51:17 +00:00
}
var prop internal.CurrentUserPrincipal
if err := resp.DecodeProp(&prop); err != nil {
2020-01-14 17:51:17 +00:00
return "", err
}
if prop.Unauthenticated != nil {
return "", fmt.Errorf("webdav: unauthenticated")
}
2020-01-14 17:51:17 +00:00
return prop.Href, nil
}