go-webdav/carddav/client.go

146 lines
3.1 KiB
Go
Raw Normal View History

2020-01-14 21:19:54 +00:00
package carddav
import (
2020-01-14 22:44:21 +00:00
"bytes"
2020-01-14 21:19:54 +00:00
"encoding/xml"
"net/http"
2020-01-14 22:44:21 +00:00
"github.com/emersion/go-vcard"
2020-01-14 21:19:54 +00:00
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal"
)
type Client struct {
*webdav.Client
ic *internal.Client
}
func NewClient(c *http.Client, endpoint string) (*Client, error) {
wc, err := webdav.NewClient(c, endpoint)
if err != nil {
return nil, err
}
ic, err := internal.NewClient(c, endpoint)
if err != nil {
return nil, err
}
return &Client{wc, ic}, nil
}
2020-01-14 22:13:23 +00:00
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
2020-01-14 21:19:54 +00:00
name := xml.Name{namespace, "addressbook-home-set"}
propfind := internal.NewPropPropfind(name)
resp, err := c.ic.PropfindFlat(principal, propfind)
if err != nil {
return "", err
}
var prop addressbookHomeSet
if err := resp.DecodeProp(name, &prop); err != nil {
return "", err
}
return prop.Href, nil
}
2020-01-14 22:13:23 +00:00
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))
2020-01-14 22:44:21 +00:00
for _, resp := range ms.Responses {
2020-01-14 22:13:23 +00:00
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
}
2020-01-14 22:44:21 +00:00
func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) ([]Address, error) {
// TODO: add a better way to format the request
addrProps := make([]internal.RawXMLValue, 0, len(query.Props))
for _, name := range query.Props {
addrProps = append(addrProps, *newProp(name, false))
}
addrDataName := xml.Name{namespace, "address-data"}
addrDataReq := internal.NewRawXMLElement(addrDataName, nil, addrProps)
addressbookQuery := addressbookQuery{
Prop: &internal.Prop{Raw: []internal.RawXMLValue{*addrDataReq}},
}
req, err := c.ic.NewXMLRequest("REPORT", addressBook, &addressbookQuery)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "1")
ms, err := c.ic.DoMultiStatus(req)
if err != nil {
return nil, err
}
addrs := make([]Address, 0, len(ms.Responses))
for _, resp := range ms.Responses {
href, err := resp.Href()
if err != nil {
return nil, err
}
var addrData addressDataResp
if err := resp.DecodeProp(addrDataName, &addrData); err != nil {
return nil, err
}
r := bytes.NewReader(addrData.Data)
card, err := vcard.NewDecoder(r).Decode()
if err != nil {
return nil, err
}
addrs = append(addrs, Address{
Href: href,
Card: card,
})
}
return addrs, nil
}