From 678578dcac7dc22311599fec7cf72abf4b86f8fe Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 31 Mar 2018 15:57:06 +0200 Subject: [PATCH] * Added item query * Request more properties --- src/calanonsync/calanonsync.go | 76 +++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/src/calanonsync/calanonsync.go b/src/calanonsync/calanonsync.go index 6cf6918..df0f53f 100644 --- a/src/calanonsync/calanonsync.go +++ b/src/calanonsync/calanonsync.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/xml" "fmt" "io" @@ -29,11 +30,10 @@ type EWSCalendar struct { url string username string password string - items []CalendarItem } func NewEWSCalendar(url, username, password string) *EWSCalendar { - // Prepare a cookie jar so we don't have to provide basic auth credentials multiple times. + // Prepare a cookie jar, since EWS uses some. jar, err := cookiejar.New(nil) if err != nil { panic(err) @@ -52,6 +52,7 @@ func (e *EWSCalendar) prepareRequest(body io.Reader) (req *http.Request, err err req, err = http.NewRequest(http.MethodPost, e.url, body) req.Header.Set("Content-Type", "text/xml") req.Header.Set("Accept", "text/xml") + req.SetBasicAuth(e.username, e.password) return } @@ -60,8 +61,6 @@ func (e *EWSCalendar) getCalendarFolderID() (id *FolderId, err error) { if err != nil { return } - // For the first request we need an authentication header - req.SetBasicAuth(e.username, e.password) resp, err := e.httpClient.Do(req) if err != nil { @@ -98,6 +97,59 @@ func (e *EWSCalendar) getCalendarFolderID() (id *FolderId, err error) { } } +func (e *EWSCalendar) getCalendarItems(folder *FolderId, start, end time.Time) (items []CalendarItem, err error) { + b := &bytes.Buffer{} + model := struct { + FolderId *FolderId + StartDate string + EndDate string + }{folder, start.Format(time.RFC3339), end.Format(time.RFC3339)} + + err = calendarQuery.Execute(b, &model) + if err != nil { + return + } + + req, err := e.prepareRequest(b) + if err != nil { + return + } + + resp, err := e.httpClient.Do(req) + if err != nil { + return + } + + if resp.StatusCode != http.StatusOK { + err = fmt.Errorf("unexpected status when querying calendar items: %d", resp.StatusCode) + return + } + + d := xml.NewDecoder(resp.Body) + defer resp.Body.Close() + + for { + var t xml.Token + t, err = d.Token() + if err == io.EOF { + err = nil + return + } else if err != nil { + return + } + + switch t := t.(type) { + case xml.StartElement: + if t.Name.Local == "CalendarItem" { + item := CalendarItem{} + err = d.DecodeElement(&item, &t) + items = append(items, item) + } + } + } + return +} + func main() { e := NewEWSCalendar("https://outlook.live.com/EWS/Exchange.asmx", "", "") id, err := e.getCalendarFolderID() @@ -105,7 +157,16 @@ func main() { log.Println(err) return } - fmt.Println(id) + + items, err := e.getCalendarItems(id, time.Now().AddDate(0, -1, 0), time.Now().AddDate(0, 2, 0)) + if err != nil { + log.Println(err) + return + } + + for _, item := range items { + fmt.Printf("%#v\n", item) + } } const folderIdRequest = ` @@ -136,9 +197,14 @@ var calendarQuery = template.Must(template.New("calendarQuery").Parse(`IdOnly + + + + +