internal: add Depth, Client.Propfind

This commit is contained in:
Simon Ser 2020-01-15 12:30:42 +01:00
parent 2b841a9234
commit 42765234a8
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 37 additions and 12 deletions

View File

@ -50,14 +50,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
descName := xml.Name{namespace, "addressbook-description"} descName := xml.Name{namespace, "addressbook-description"}
propfind := internal.NewPropNamePropfind(resTypeName, descName) propfind := internal.NewPropNamePropfind(resTypeName, descName)
req, err := c.ic.NewXMLRequest("PROPFIND", addressBookHomeSet, propfind) ms, err := c.ic.Propfind(addressBookHomeSet, internal.DepthOne, propfind)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "1")
ms, err := c.ic.DoMultiStatus(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -10,6 +10,34 @@ import (
"path" "path"
) )
// Depth indicates whether a request applies to the resource's members. It's
// defined in RFC 4918 section 10.2.
type Depth int
const (
// DepthZero indicates that the request applies only to the resource.
DepthZero Depth = 0
// DepthOne indicates that the request applies to the resource and its
// internal members only.
DepthOne Depth = 1
// DepthInfinity indicates that the request applies to the resource and all
// of its members.
DepthInfinity Depth = -1
)
// String formats the depth.
func (d Depth) String() string {
switch d {
case DepthZero:
return "0"
case DepthOne:
return "1"
case DepthInfinity:
return "infinity"
}
panic("webdav: invalid Depth value")
}
type Client struct { type Client struct {
http *http.Client http *http.Client
endpoint *url.URL endpoint *url.URL
@ -80,16 +108,20 @@ func (c *Client) DoMultiStatus(req *http.Request) (*Multistatus, error) {
return &ms, nil return &ms, nil
} }
// PropfindFlat performs a PROPFIND request with a zero depth. func (c *Client) Propfind(href string, depth Depth, propfind *Propfind) (*Multistatus, error) {
func (c *Client) PropfindFlat(href string, propfind *Propfind) (*Response, error) {
req, err := c.NewXMLRequest("PROPFIND", href, propfind) req, err := c.NewXMLRequest("PROPFIND", href, propfind)
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Add("Depth", "0") req.Header.Add("Depth", depth.String())
ms, err := c.DoMultiStatus(req) return c.DoMultiStatus(req)
}
// PropfindFlat performs a PROPFIND request with a zero depth.
func (c *Client) PropfindFlat(href string, propfind *Propfind) (*Response, error) {
ms, err := c.Propfind(href, DepthZero, propfind)
if err != nil { if err != nil {
return nil, err return nil, err
} }