carddav: add Client.PutAddressObject

This commit is contained in:
Simon Ser 2020-02-12 19:47:16 +01:00
parent 30eac28d2b
commit 842acb3647
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 51 additions and 1 deletions

View File

@ -3,9 +3,11 @@ package carddav
import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
@ -317,3 +319,51 @@ func (c *Client) MultiGetAddressBook(path string, multiGet *AddressBookMultiGet)
return decodeAddressList(ms)
}
func (c *Client) PutAddressObject(path string, card vcard.Card) (*AddressObject, error) {
// TODO: add support for If-None-Match and If-Match
pr, pw := io.Pipe()
go func() {
err := vcard.NewEncoder(pw).Encode(card)
pw.CloseWithError(err)
}()
req, err := c.ic.NewRequest(http.MethodPut, path, pr)
if err != nil {
pr.Close()
return nil, err
}
req.Header.Set("Content-Type", vcard.MIMEType)
resp, err := c.ic.Do(req)
if err != nil {
return nil, err
}
ao := &AddressObject{Path: path}
if loc := resp.Header.Get("Location"); loc != "" {
u, err := url.Parse(loc)
if err != nil {
return nil, err
}
ao.Path = u.Path
}
if etag := resp.Header.Get("ETag"); etag != "" {
etag, err := strconv.Unquote(etag)
if err != nil {
return nil, err
}
ao.ETag = etag
}
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" {
t, err := http.ParseTime(lastModified)
if err != nil {
return nil, err
}
ao.ModTime = t
}
return ao, nil
}

View File

@ -386,7 +386,7 @@ func (b *backend) Proppatch(r *http.Request, update *internal.Propertyupdate) (*
}
func (b *backend) Put(r *http.Request) (*internal.Href, error) {
// TODO: add support for If-None-Match
// TODO: add support for If-None-Match and If-Match
t, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {