mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 08:14:15 +00:00
caldav: add Client.FindCalendars
This commit is contained in:
parent
936b9451cc
commit
bae7dcce43
@ -2,3 +2,10 @@
|
||||
//
|
||||
// CalDAV is defined in RFC 4791.
|
||||
package caldav
|
||||
|
||||
type Calendar struct {
|
||||
Path string
|
||||
Name string
|
||||
Description string
|
||||
MaxResourceSize int64
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package caldav
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/emersion/go-webdav"
|
||||
@ -45,3 +46,59 @@ func (c *Client) FindCalendarHomeSet(principal string) (string, error) {
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ var (
|
||||
calendarDescriptionName = xml.Name{namespace, "calendar-description"}
|
||||
supportedCalendarDataName = xml.Name{namespace, "supported-calendar-data"}
|
||||
maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
|
||||
|
||||
calendarName = xml.Name{namespace, "calendar"}
|
||||
)
|
||||
|
||||
// https://tools.ietf.org/html/rfc4791#section-6.2.1
|
||||
|
Loading…
Reference in New Issue
Block a user