caldav: add Client.GetCalendarObject

This commit is contained in:
Simon Ser 2020-02-24 18:19:39 +01:00
parent 7bb9b3aa0b
commit 4c419a961d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -3,9 +3,11 @@ package caldav
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"mime"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/emersion/go-ical" "github.com/emersion/go-ical"
@ -241,6 +243,42 @@ func populateCalendarObject(co *CalendarObject, resp *http.Response) error {
return nil return nil
} }
func (c *Client) GetCalendarObject(path string) (*CalendarObject, error) {
req, err := c.ic.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", ical.MIMEType)
resp, err := c.ic.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if err != nil {
return nil, err
}
if !strings.EqualFold(mediaType, ical.MIMEType) {
return nil, fmt.Errorf("caldav: expected Content-Type %q, got %q", ical.MIMEType, mediaType)
}
cal, err := ical.NewDecoder(resp.Body).DecodeCalendar()
if err != nil {
return nil, err
}
co := &CalendarObject{
Path: resp.Request.URL.Path,
Data: cal,
}
if err := populateCalendarObject(co, resp); err != nil {
return nil, err
}
return co, nil
}
func (c *Client) PutCalendarObject(path string, cal *ical.Calendar) (*CalendarObject, error) { func (c *Client) PutCalendarObject(path string, cal *ical.Calendar) (*CalendarObject, error) {
// TODO: add support for If-None-Match and If-Match // TODO: add support for If-None-Match and If-Match