forked from aksdb/CalAnonSync
parent
e1e0afd6b6
commit
678578dcac
|
@ -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 = `<?xml version="1.0" encoding="utf-8"?>
|
||||
|
@ -136,9 +197,14 @@ var calendarQuery = template.Must(template.New("calendarQuery").Parse(`<?xml ver
|
|||
<t:BaseShape>IdOnly</t:BaseShape>
|
||||
<t:AdditionalProperties>
|
||||
<t:FieldURI FieldURI="item:Subject" />
|
||||
<t:FieldURI FieldURI="item:Sensitivity" />
|
||||
<t:FieldURI FieldURI="calendar:Start" />
|
||||
<t:FieldURI FieldURI="calendar:End" />
|
||||
<t:FieldURI FieldURI="calendar:UID" />
|
||||
<t:FieldURI FieldURI="calendar:RecurrenceId" />
|
||||
<t:FieldURI FieldURI="calendar:IsCancelled" />
|
||||
<t:FieldURI FieldURI="calendar:CalendarItemType" />
|
||||
<t:FieldURI FieldURI="calendar:CalendarItemType" />
|
||||
</t:AdditionalProperties>
|
||||
</m:ItemShape>
|
||||
<m:CalendarView StartDate="{{ .StartDate }}" EndDate="{{ .EndDate }}" />
|
||||
|
|
Loading…
Reference in New Issue