mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 08:14:15 +00:00
carddav: add Client.PutAddressObject
This commit is contained in:
parent
30eac28d2b
commit
842acb3647
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user