diff --git a/carddav/client.go b/carddav/client.go index cd9d157..9d03987 100644 --- a/carddav/client.go +++ b/carddav/client.go @@ -30,7 +30,7 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) { func (c *Client) FindAddressBookHomeSet(principal string) (string, error) { name := xml.Name{namespace, "addressbook-home-set"} - propfind := internal.NewPropPropfind(name) + propfind := internal.NewPropNamePropfind(name) resp, err := c.ic.PropfindFlat(principal, propfind) if err != nil { @@ -48,7 +48,7 @@ func (c *Client) FindAddressBookHomeSet(principal string) (string, error) { func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) { resTypeName := xml.Name{"DAV:", "resourcetype"} descName := xml.Name{namespace, "addressbook-description"} - propfind := internal.NewPropPropfind(resTypeName, descName) + propfind := internal.NewPropNamePropfind(resTypeName, descName) req, err := c.ic.NewXMLRequest("PROPFIND", addressBookHomeSet, propfind) if err != nil { @@ -92,19 +92,20 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err } 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}}, + var addrDataReq addressDataReq + for _, name := range query.Props { + addrDataReq.Props = append(addrDataReq.Props, prop{Name: name}) } + propReq, err := internal.EncodeProp(&addrDataReq) + if err != nil { + return nil, err + } + + addressbookQuery := addressbookQuery{Prop: propReq} + req, err := c.ic.NewXMLRequest("REPORT", addressBook, &addressbookQuery) if err != nil { return nil, err diff --git a/client.go b/client.go index ca9a844..9aab567 100644 --- a/client.go +++ b/client.go @@ -21,7 +21,7 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) { func (c *Client) FindCurrentUserPrincipal() (string, error) { name := xml.Name{"DAV:", "current-user-principal"} - propfind := internal.NewPropPropfind(name) + propfind := internal.NewPropNamePropfind(name) resp, err := c.c.PropfindFlat("/", propfind) if err != nil { diff --git a/internal/elements.go b/internal/elements.go index aa2ace1..9f441e2 100644 --- a/internal/elements.go +++ b/internal/elements.go @@ -114,6 +114,18 @@ type Prop struct { Raw []RawXMLValue `xml:",any"` } +func EncodeProp(values ...interface{}) (*Prop, error) { + l := make([]RawXMLValue, len(values)) + for i, v := range values { + raw, err := EncodeRawXMLElement(v) + if err != nil { + return nil, err + } + l[i] = *raw + } + return &Prop{Raw: l}, nil +} + // https://tools.ietf.org/html/rfc4918#section-14.20 type Propfind struct { XMLName xml.Name `xml:"DAV: propfind"` @@ -121,7 +133,7 @@ type Propfind struct { // TODO: propname | (allprop, include?) } -func NewPropPropfind(names ...xml.Name) *Propfind { +func NewPropNamePropfind(names ...xml.Name) *Propfind { children := make([]RawXMLValue, len(names)) for i, name := range names { children[i] = *NewRawXMLElement(name, nil, nil)