caldav: add Client.FindCalendars

This commit is contained in:
Simon Ser 2020-01-30 13:51:02 +01:00
parent 936b9451cc
commit bae7dcce43
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 66 additions and 0 deletions

View File

@ -2,3 +2,10 @@
// //
// CalDAV is defined in RFC 4791. // CalDAV is defined in RFC 4791.
package caldav package caldav
type Calendar struct {
Path string
Name string
Description string
MaxResourceSize int64
}

View File

@ -1,6 +1,7 @@
package caldav package caldav
import ( import (
"fmt"
"net/http" "net/http"
"github.com/emersion/go-webdav" "github.com/emersion/go-webdav"
@ -45,3 +46,59 @@ func (c *Client) FindCalendarHomeSet(principal string) (string, error) {
return prop.Href.Path, nil return prop.Href.Path, nil
} }
func (c *Client) FindCalendars(calendarHomeSet string) ([]Calendar, error) {
propfind := internal.NewPropNamePropfind(
internal.ResourceTypeName,
internal.DisplayNameName,
calendarDescriptionName,
maxResourceSizeName,
)
ms, err := c.ic.Propfind(calendarHomeSet, internal.DepthOne, propfind)
if err != nil {
return nil, err
}
l := make([]Calendar, 0, len(ms.Responses))
for _, resp := range ms.Responses {
path, err := resp.Path()
if err != nil {
return nil, err
}
var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
return nil, err
}
if !resType.Is(calendarName) {
continue
}
var desc calendarDescription
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var dispName internal.DisplayName
if err := resp.DecodeProp(&dispName); 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, Calendar{
Path: path,
Name: dispName.Name,
Description: desc.Description,
MaxResourceSize: maxResSize.Size,
})
}
return l, nil
}

View File

@ -14,6 +14,8 @@ var (
calendarDescriptionName = xml.Name{namespace, "calendar-description"} calendarDescriptionName = xml.Name{namespace, "calendar-description"}
supportedCalendarDataName = xml.Name{namespace, "supported-calendar-data"} supportedCalendarDataName = xml.Name{namespace, "supported-calendar-data"}
maxResourceSizeName = xml.Name{namespace, "max-resource-size"} maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
calendarName = xml.Name{namespace, "calendar"}
) )
// https://tools.ietf.org/html/rfc4791#section-6.2.1 // https://tools.ietf.org/html/rfc4791#section-6.2.1