carddav: populate AddressBook.{Name,MaxResourceSize} in client

This commit is contained in:
Simon Ser 2020-01-19 15:29:51 +01:00
parent c4718a3a49
commit 6e0ea58de1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 34 additions and 9 deletions

View File

@ -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,
Name: dispNameProp.Name,
Description: descProp.Description, Description: descProp.Description,
MaxResourceSize: maxResSize.Size,
}) })
} }

View File

@ -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"}
addressBookSupportedAddressData = xml.Name{namespace, "addressbook-supported-address-data"}
maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
addressBookQueryName = xml.Name{namespace, "addressbook-query"} addressBookQueryName = xml.Name{namespace, "addressbook-query"}
addressBookMultigetName = xml.Name{namespace, "addressbook-multiget"} addressBookMultigetName = xml.Name{namespace, "addressbook-multiget"}
addressBookSupportedAddressData = xml.Name{namespace, "addressbook-supported-address-data"}
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

View File

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

View File

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