aboutsummaryrefslogtreecommitdiff
path: root/plugins/carddav/carddav.go
blob: b863827c23c7f2ad8cd3bd1a7a799ea7b41ca18d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package alpscarddav

import (
	"fmt"
	"net/http"
	"net/url"

	"git.sr.ht/~emersion/alps"
	"github.com/emersion/go-webdav/carddav"
)

var errNoAddressBook = fmt.Errorf("carddav: no address book found")

type authRoundTripper struct {
	upstream http.RoundTripper
	session  *alps.Session
}

func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	rt.session.SetHTTPBasicAuth(req)
	return rt.upstream.RoundTrip(req)
}

func newClient(u *url.URL, session *alps.Session) (*carddav.Client, error) {
	rt := authRoundTripper{
		upstream: http.DefaultTransport,
		session:  session,
	}
	return carddav.NewClient(&http.Client{Transport: &rt}, u.String())
}

type AddressObject struct {
	*carddav.AddressObject
}

func newAddressObjectList(aos []carddav.AddressObject) []AddressObject {
	l := make([]AddressObject, len(aos))
	for i := range aos {
		l[i] = AddressObject{&aos[i]}
	}
	return l
}

func (ao AddressObject) URL() string {
	return "/contacts/" + url.PathEscape(ao.Path)
}