internal: add EncodeProp

This allows to simplify carddav.QueryAddressBook's request marshaling.
This commit is contained in:
Simon Ser 2020-01-15 11:17:38 +01:00
parent 45cd1977d4
commit 25ab0b2076
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 26 additions and 13 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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)