caldav: add Client.PutCalendarObject

This commit is contained in:
Simon Ser 2020-02-24 18:13:24 +01:00
parent 07d4dfae5e
commit 7bb9b3aa0b
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 63 additions and 3 deletions

View File

@ -3,6 +3,9 @@ package caldav
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"net/http"
"net/url"
"strconv"
"time" "time"
"github.com/emersion/go-ical" "github.com/emersion/go-ical"
@ -211,3 +214,60 @@ func (c *Client) QueryCalendar(calendar string, query *CalendarQuery) ([]Calenda
return decodeCalendarObjectList(ms) return decodeCalendarObjectList(ms)
} }
func populateCalendarObject(co *CalendarObject, resp *http.Response) error {
if loc := resp.Header.Get("Location"); loc != "" {
u, err := url.Parse(loc)
if err != nil {
return err
}
co.Path = u.Path
}
if etag := resp.Header.Get("ETag"); etag != "" {
etag, err := strconv.Unquote(etag)
if err != nil {
return err
}
co.ETag = etag
}
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" {
t, err := http.ParseTime(lastModified)
if err != nil {
return err
}
co.ModTime = t
}
return nil
}
func (c *Client) PutCalendarObject(path string, cal *ical.Calendar) (*CalendarObject, error) {
// TODO: add support for If-None-Match and If-Match
// TODO: some servers want a Content-Length header, so we can't stream the
// request body here. See the Radicale issue:
// https://github.com/Kozea/Radicale/issues/1016
var buf bytes.Buffer
if err := ical.NewEncoder(&buf).EncodeCalendar(cal); err != nil {
return nil, err
}
req, err := c.ic.NewRequest(http.MethodPut, path, &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", ical.MIMEType)
resp, err := c.ic.Do(req)
if err != nil {
return nil, err
}
resp.Body.Close()
co := &CalendarObject{Path: path}
if err := populateCalendarObject(co, resp); err != nil {
return nil, err
}
return co, nil
}

2
go.mod
View File

@ -3,6 +3,6 @@ module github.com/emersion/go-webdav
go 1.13 go 1.13
require ( require (
github.com/emersion/go-ical v0.0.0-20200224161826-aa4584e92c62 github.com/emersion/go-ical v0.0.0-20200224170602-c9b8621220bc
github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7 github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7
) )

4
go.sum
View File

@ -1,4 +1,4 @@
github.com/emersion/go-ical v0.0.0-20200224161826-aa4584e92c62 h1:kP9BxopsBc1jfYfHgbgnDBJeCfZt9GkyWXFmNOcRsps= github.com/emersion/go-ical v0.0.0-20200224170602-c9b8621220bc h1:kpISLFxf4eYcslxD/XkzE2Su7UQdNKt0XYgnN6MEUxg=
github.com/emersion/go-ical v0.0.0-20200224161826-aa4584e92c62/go.mod h1:4xVTBPcT43a1pp3vdaa+FuRdX5XhKCZPpWv7m0z9ByM= github.com/emersion/go-ical v0.0.0-20200224170602-c9b8621220bc/go.mod h1:4xVTBPcT43a1pp3vdaa+FuRdX5XhKCZPpWv7m0z9ByM=
github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7 h1:SE+tcd+0kn0cT4MqTo66gmkjqWHF1Z+Yha5/rhLs/H8= github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7 h1:SE+tcd+0kn0cT4MqTo66gmkjqWHF1Z+Yha5/rhLs/H8=
github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM= github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM=