go-webdav/caldav/client.go
2020-01-30 13:51:02 +01:00

105 lines
2.3 KiB
Go

package caldav
import (
"fmt"
"net/http"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal"
)
// Client provides access to a remote CardDAV server.
type Client struct {
*webdav.Client
ic *internal.Client
}
func NewClient(c *http.Client, endpoint string) (*Client, error) {
wc, err := webdav.NewClient(c, endpoint)
if err != nil {
return nil, err
}
ic, err := internal.NewClient(c, endpoint)
if err != nil {
return nil, err
}
return &Client{wc, ic}, nil
}
func (c *Client) SetBasicAuth(username, password string) {
c.Client.SetBasicAuth(username, password)
c.ic.SetBasicAuth(username, password)
}
func (c *Client) FindCalendarHomeSet(principal string) (string, error) {
propfind := internal.NewPropNamePropfind(calendarHomeSetName)
resp, err := c.ic.PropfindFlat(principal, propfind)
if err != nil {
return "", err
}
var prop calendarHomeSet
if err := resp.DecodeProp(&prop); err != nil {
return "", err
}
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
}