internal: add Client.PropfindFlat

This commit is contained in:
Simon Ser 2020-01-14 21:43:09 +01:00
parent 94d597c1f5
commit 931602e55d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 22 additions and 17 deletions

View File

@ -23,19 +23,7 @@ func (c *Client) FindCurrentUserPrincipal() (string, error) {
name := xml.Name{"DAV:", "current-user-principal"}
propfind := internal.NewPropPropfind(name)
req, err := c.c.NewXMLRequest("PROPFIND", "/", propfind)
if err != nil {
return "", err
}
req.Header.Add("Depth", "0")
ms, err := c.c.DoMultiStatus(req)
if err != nil {
return "", err
}
resp, err := ms.Get("/")
resp, err := c.c.PropfindFlat("/", propfind)
if err != nil {
return "", err
}

View File

@ -27,24 +27,24 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
return &Client{c, u}, nil
}
func (c *Client) NewRequest(method string, p string, body io.Reader) (*http.Request, error) {
func (c *Client) NewRequest(method string, href string, body io.Reader) (*http.Request, error) {
u := url.URL{
Scheme: c.endpoint.Scheme,
User: c.endpoint.User,
Host: c.endpoint.Host,
Path: path.Join(c.endpoint.Path, p),
Path: path.Join(c.endpoint.Path, href),
}
return http.NewRequest(method, u.String(), body)
}
func (c *Client) NewXMLRequest(method string, p string, v interface{}) (*http.Request, error) {
func (c *Client) NewXMLRequest(method string, href string, v interface{}) (*http.Request, error) {
var buf bytes.Buffer
buf.WriteString(xml.Header)
if err := xml.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
req, err := c.NewRequest(method, p, &buf)
req, err := c.NewRequest(method, href, &buf)
if err != nil {
return nil, err
}
@ -79,3 +79,20 @@ func (c *Client) DoMultiStatus(req *http.Request) (*Multistatus, error) {
return &ms, nil
}
// PropfindFlat performs a PROPFIND request with a zero depth.
func (c *Client) PropfindFlat(href string, propfind *Propfind) (*Response, error) {
req, err := c.NewXMLRequest("PROPFIND", href, propfind)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "0")
ms, err := c.DoMultiStatus(req)
if err != nil {
return nil, err
}
return ms.Get(href)
}