mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
internal: add EncodeProp
This allows to simplify carddav.QueryAddressBook's request marshaling.
This commit is contained in:
parent
45cd1977d4
commit
25ab0b2076
@ -30,7 +30,7 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|||||||
|
|
||||||
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
|
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
|
||||||
name := xml.Name{namespace, "addressbook-home-set"}
|
name := xml.Name{namespace, "addressbook-home-set"}
|
||||||
propfind := internal.NewPropPropfind(name)
|
propfind := internal.NewPropNamePropfind(name)
|
||||||
|
|
||||||
resp, err := c.ic.PropfindFlat(principal, propfind)
|
resp, err := c.ic.PropfindFlat(principal, propfind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -48,7 +48,7 @@ func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
|
|||||||
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
|
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
|
||||||
resTypeName := xml.Name{"DAV:", "resourcetype"}
|
resTypeName := xml.Name{"DAV:", "resourcetype"}
|
||||||
descName := xml.Name{namespace, "addressbook-description"}
|
descName := xml.Name{namespace, "addressbook-description"}
|
||||||
propfind := internal.NewPropPropfind(resTypeName, descName)
|
propfind := internal.NewPropNamePropfind(resTypeName, descName)
|
||||||
|
|
||||||
req, err := c.ic.NewXMLRequest("PROPFIND", addressBookHomeSet, propfind)
|
req, err := c.ic.NewXMLRequest("PROPFIND", addressBookHomeSet, propfind)
|
||||||
if err != nil {
|
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) {
|
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"}
|
addrDataName := xml.Name{namespace, "address-data"}
|
||||||
addrDataReq := internal.NewRawXMLElement(addrDataName, nil, addrProps)
|
|
||||||
|
|
||||||
addressbookQuery := addressbookQuery{
|
var addrDataReq addressDataReq
|
||||||
Prop: &internal.Prop{Raw: []internal.RawXMLValue{*addrDataReq}},
|
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)
|
req, err := c.ic.NewXMLRequest("REPORT", addressBook, &addressbookQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -21,7 +21,7 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|||||||
|
|
||||||
func (c *Client) FindCurrentUserPrincipal() (string, error) {
|
func (c *Client) FindCurrentUserPrincipal() (string, error) {
|
||||||
name := xml.Name{"DAV:", "current-user-principal"}
|
name := xml.Name{"DAV:", "current-user-principal"}
|
||||||
propfind := internal.NewPropPropfind(name)
|
propfind := internal.NewPropNamePropfind(name)
|
||||||
|
|
||||||
resp, err := c.c.PropfindFlat("/", propfind)
|
resp, err := c.c.PropfindFlat("/", propfind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -114,6 +114,18 @@ type Prop struct {
|
|||||||
Raw []RawXMLValue `xml:",any"`
|
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
|
// https://tools.ietf.org/html/rfc4918#section-14.20
|
||||||
type Propfind struct {
|
type Propfind struct {
|
||||||
XMLName xml.Name `xml:"DAV: propfind"`
|
XMLName xml.Name `xml:"DAV: propfind"`
|
||||||
@ -121,7 +133,7 @@ type Propfind struct {
|
|||||||
// TODO: propname | (allprop, include?)
|
// TODO: propname | (allprop, include?)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPropPropfind(names ...xml.Name) *Propfind {
|
func NewPropNamePropfind(names ...xml.Name) *Propfind {
|
||||||
children := make([]RawXMLValue, len(names))
|
children := make([]RawXMLValue, len(names))
|
||||||
for i, name := range names {
|
for i, name := range names {
|
||||||
children[i] = *NewRawXMLElement(name, nil, nil)
|
children[i] = *NewRawXMLElement(name, nil, nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user