2020-05-13 12:07:44 +00:00
|
|
|
package alpscarddav
|
2020-02-05 13:58:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
"git.sr.ht/~emersion/alps"
|
2020-02-05 13:58:56 +00:00
|
|
|
"github.com/emersion/go-webdav/carddav"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errNoAddressBook = fmt.Errorf("carddav: no address book found")
|
|
|
|
|
|
|
|
type authRoundTripper struct {
|
|
|
|
upstream http.RoundTripper
|
2020-05-13 12:07:44 +00:00
|
|
|
session *alps.Session
|
2020-02-05 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
rt.session.SetHTTPBasicAuth(req)
|
|
|
|
return rt.upstream.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
func newClient(u *url.URL, session *alps.Session) (*carddav.Client, error) {
|
2020-02-05 13:58:56 +00:00
|
|
|
rt := authRoundTripper{
|
|
|
|
upstream: http.DefaultTransport,
|
|
|
|
session: session,
|
|
|
|
}
|
2020-02-11 18:14:05 +00:00
|
|
|
return carddav.NewClient(&http.Client{Transport: &rt}, u.String())
|
2020-02-05 13:58:56 +00:00
|
|
|
}
|
2020-02-27 11:17:23 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|