carddav: add very basic Client

This commit is contained in:
Simon Ser 2020-01-14 22:19:54 +01:00
parent 931602e55d
commit 3d05533a31
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
5 changed files with 59 additions and 2 deletions

3
carddav/carddav.go Normal file
View File

@ -0,0 +1,3 @@
package carddav
const namespace = "urn:ietf:params:xml:ns:carddav"

44
carddav/client.go Normal file
View File

@ -0,0 +1,44 @@
package carddav
import (
"encoding/xml"
"net/http"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal"
)
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) FindAddressbookHomeSet(principal string) (string, error) {
name := xml.Name{namespace, "addressbook-home-set"}
propfind := internal.NewPropPropfind(name)
resp, err := c.ic.PropfindFlat(principal, propfind)
if err != nil {
return "", err
}
var prop addressbookHomeSet
if err := resp.DecodeProp(name, &prop); err != nil {
return "", err
}
return prop.Href, nil
}

10
carddav/elements.go Normal file
View File

@ -0,0 +1,10 @@
package carddav
import (
"encoding/xml"
)
type addressbookHomeSet struct {
Name xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"`
Href string `xml:"href"`
}

View File

@ -28,7 +28,7 @@ func (c *Client) FindCurrentUserPrincipal() (string, error) {
return "", err
}
var prop currentUserPrincipalProp
var prop currentUserPrincipal
if err := resp.DecodeProp(name, &prop); err != nil {
return "", err
}

View File

@ -4,7 +4,7 @@ import (
"encoding/xml"
)
type currentUserPrincipalProp struct {
type currentUserPrincipal struct {
Name xml.Name `xml:"DAV: current-user-principal"`
Href string `xml:"href"`
}