carddav: add Client.FindAddressBooks
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
package carddav
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
const namespace = "urn:ietf:params:xml:ns:carddav"
|
||||
|
||||
type AddressBook struct {
|
||||
Href string
|
||||
Description string
|
||||
}
|
||||
|
||||
var addressBookName = xml.Name{namespace, "addressbook"}
|
||||
|
||||
type AddressBookQuery struct {
|
||||
AddressDataProps []string
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
||||
return &Client{wc, ic}, nil
|
||||
}
|
||||
|
||||
func (c *Client) FindAddressbookHomeSet(principal string) (string, error) {
|
||||
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
|
||||
name := xml.Name{namespace, "addressbook-home-set"}
|
||||
propfind := internal.NewPropPropfind(name)
|
||||
|
||||
@@ -42,3 +42,50 @@ func (c *Client) FindAddressbookHomeSet(principal string) (string, error) {
|
||||
|
||||
return prop.Href, nil
|
||||
}
|
||||
|
||||
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
|
||||
resTypeName := xml.Name{"DAV:", "resourcetype"}
|
||||
descName := xml.Name{namespace, "addressbook-description"}
|
||||
propfind := internal.NewPropPropfind(resTypeName, descName)
|
||||
|
||||
req, err := c.ic.NewXMLRequest("PROPFIND", addressBookHomeSet, propfind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Depth", "1")
|
||||
|
||||
ms, err := c.ic.DoMultiStatus(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l := make([]AddressBook, 0, len(ms.Responses))
|
||||
for i := range ms.Responses {
|
||||
resp := &ms.Responses[i]
|
||||
href, err := resp.Href()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resTypeProp internal.ResourceType
|
||||
if err := resp.DecodeProp(resTypeName, &resTypeProp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !resTypeProp.Is(addressBookName) {
|
||||
continue
|
||||
}
|
||||
|
||||
var descProp addressbookDescription
|
||||
if err := resp.DecodeProp(descName, &descProp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l = append(l, AddressBook{
|
||||
Href: href,
|
||||
Description: descProp.Data,
|
||||
})
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
@@ -5,6 +5,19 @@ import (
|
||||
)
|
||||
|
||||
type addressbookHomeSet struct {
|
||||
Name xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"`
|
||||
Href string `xml:"href"`
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"`
|
||||
Href string `xml:"href"`
|
||||
}
|
||||
|
||||
type addressbookDescription struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-description"`
|
||||
Data string `xml:",chardata"`
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc6352#section-10.3
|
||||
type addressbookQuery struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-query"`
|
||||
Prop *internal.Prop `xml:"DAV: prop,omitempty"`
|
||||
// TODO: DAV:allprop | DAV:propname
|
||||
// TODO: filter, limit?
|
||||
}
|
||||
|
Reference in New Issue
Block a user