go-webdav/client.go

57 lines
1.1 KiB
Go
Raw Normal View History

2020-01-14 17:51:17 +00:00
package webdav
import (
"encoding/xml"
2020-01-14 17:51:17 +00:00
"fmt"
"net/http"
"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
}
func (c *Client) FindCurrentUserPrincipal() (string, error) {
name := xml.Name{"DAV:", "current-user-principal"}
propfind := internal.NewPropPropfind(name)
req, err := c.c.NewXMLRequest("PROPFIND", "", propfind)
2020-01-14 17:51:17 +00:00
if err != nil {
return "", err
}
req.Header.Add("Depth", "0")
resps, err := c.c.DoMultiStatus(req)
if err != nil {
return "", err
}
if len(resps) != 1 {
return "", fmt.Errorf("expected exactly one response in multistatus, got %v", len(resps))
}
resp := &resps[0]
// TODO: handle propstats with errors
if len(resp.Propstats) != 1 {
return "", fmt.Errorf("expected exactly one propstat in response")
}
propstat := &resp.Propstats[0]
var prop currentUserPrincipalProp
2020-01-14 17:53:29 +00:00
if err := propstat.Prop.Decode(&prop); err != nil {
2020-01-14 17:51:17 +00:00
return "", err
}
return prop.Href, nil
}