mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
carddav: populate AddressBook.{Name,MaxResourceSize} in client
This commit is contained in:
parent
c4718a3a49
commit
6e0ea58de1
@ -2,6 +2,7 @@ package carddav
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/emersion/go-vcard"
|
"github.com/emersion/go-vcard"
|
||||||
@ -48,7 +49,12 @@ func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
|
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
|
||||||
propfind := internal.NewPropNamePropfind(internal.ResourceTypeName, addressBookDescriptionName)
|
propfind := internal.NewPropNamePropfind(
|
||||||
|
internal.ResourceTypeName,
|
||||||
|
internal.DisplayNameName,
|
||||||
|
addressBookDescriptionName,
|
||||||
|
maxResourceSizeName,
|
||||||
|
)
|
||||||
ms, err := c.ic.Propfind(addressBookHomeSet, internal.DepthOne, propfind)
|
ms, err := c.ic.Propfind(addressBookHomeSet, internal.DepthOne, propfind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -70,13 +76,28 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
var descProp addressbookDescription
|
var descProp addressbookDescription
|
||||||
if err := resp.DecodeProp(&descProp); err != nil {
|
if err := resp.DecodeProp(&descProp); err != nil && !internal.IsNotFound(err) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dispNameProp internal.DisplayName
|
||||||
|
if err := resp.DecodeProp(&dispNameProp); err != nil && !internal.IsNotFound(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var maxResSize maxResourceSize
|
||||||
|
if err := resp.DecodeProp(&maxResSize); err != nil && !internal.IsNotFound(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if maxResSize.Size < 0 {
|
||||||
|
return nil, fmt.Errorf("carddav: max-resource-size must be a positive integer")
|
||||||
|
}
|
||||||
|
|
||||||
l = append(l, AddressBook{
|
l = append(l, AddressBook{
|
||||||
Href: href,
|
Href: href,
|
||||||
Description: descProp.Description,
|
Name: dispNameProp.Name,
|
||||||
|
Description: descProp.Description,
|
||||||
|
MaxResourceSize: maxResSize.Size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,13 +14,13 @@ var (
|
|||||||
|
|
||||||
addressBookName = xml.Name{namespace, "addressbook"}
|
addressBookName = xml.Name{namespace, "addressbook"}
|
||||||
addressBookDescriptionName = xml.Name{namespace, "addressbook-description"}
|
addressBookDescriptionName = xml.Name{namespace, "addressbook-description"}
|
||||||
addressBookQueryName = xml.Name{namespace, "addressbook-query"}
|
|
||||||
addressBookMultigetName = xml.Name{namespace, "addressbook-multiget"}
|
|
||||||
addressBookSupportedAddressData = xml.Name{namespace, "addressbook-supported-address-data"}
|
addressBookSupportedAddressData = xml.Name{namespace, "addressbook-supported-address-data"}
|
||||||
|
maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
|
||||||
|
|
||||||
|
addressBookQueryName = xml.Name{namespace, "addressbook-query"}
|
||||||
|
addressBookMultigetName = xml.Name{namespace, "addressbook-multiget"}
|
||||||
|
|
||||||
addressDataName = xml.Name{namespace, "address-data"}
|
addressDataName = xml.Name{namespace, "address-data"}
|
||||||
|
|
||||||
maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6352#section-6.2.3
|
// https://tools.ietf.org/html/rfc6352#section-6.2.3
|
||||||
|
@ -50,7 +50,7 @@ func (s *Status) Err() error {
|
|||||||
|
|
||||||
// TODO: handle 2xx, 3xx
|
// TODO: handle 2xx, 3xx
|
||||||
if s.Code != http.StatusOK {
|
if s.Code != http.StatusOK {
|
||||||
return fmt.Errorf("webdav: HTTP error: %v %v", s.Code, s.Text)
|
return &HTTPError{Code: s.Code}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,10 @@ func HTTPErrorFromError(err error) *HTTPError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsNotFound(err error) bool {
|
||||||
|
return HTTPErrorFromError(err).Code == http.StatusNotFound
|
||||||
|
}
|
||||||
|
|
||||||
func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
|
func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
|
||||||
return &HTTPError{code, fmt.Errorf(format, a...)}
|
return &HTTPError{code, fmt.Errorf(format, a...)}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user