caldav: add Client boilerplate

This commit is contained in:
Simon Ser 2020-01-30 13:18:05 +01:00
parent feea39c898
commit 6aea0eda2d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 69 additions and 1 deletions

4
caldav/caldav.go Normal file
View File

@ -0,0 +1,4 @@
// Package caldav provides a client and server CalDAV implementation.
//
// CalDAV is defined in RFC 4791.
package caldav

47
caldav/client.go Normal file
View File

@ -0,0 +1,47 @@
package caldav
import (
"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
}

17
caldav/elements.go Normal file
View File

@ -0,0 +1,17 @@
package caldav
import (
"encoding/xml"
"github.com/emersion/go-webdav/internal"
)
const namespace = "urn:ietf:params:xml:ns:caldav"
var calendarHomeSetName = xml.Name{namespace, "calendar-home-set"}
// https://tools.ietf.org/html/rfc4791#section-6.2.1
type calendarHomeSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-home-set"`
Href internal.Href `xml:"DAV: href"`
}

View File

@ -71,7 +71,7 @@ const (
) )
type AddressBookMultiGet struct { type AddressBookMultiGet struct {
Paths []string Paths []string
DataRequest AddressDataRequest DataRequest AddressDataRequest
} }