Extracted EWS code into separate file

This commit is contained in:
Andreas Schneider 2018-04-01 11:29:56 +02:00
parent e8cf553e9a
commit 5d62c932d5
2 changed files with 196 additions and 190 deletions

View File

@ -1,155 +1,11 @@
package main package main
import ( import (
"bytes"
"encoding/xml"
"fmt" "fmt"
"io"
"log" "log"
"net/http"
"net/http/cookiejar"
"strings"
"text/template"
"time" "time"
) )
type FolderId struct {
Id string `xml:",attr"`
ChangeKey string `xml:",attr"`
}
type CalendarItem struct {
Subject string
UID string
Start time.Time
End time.Time
}
type EWSCalendar struct {
httpClient *http.Client
url string
username string
password string
}
func NewEWSCalendar(url, username, password string) *EWSCalendar {
// Prepare a cookie jar, since EWS uses some.
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
return &EWSCalendar{
httpClient: &http.Client{
Jar: jar,
},
url: url,
username: username,
password: password,
}
}
func (e *EWSCalendar) prepareRequest(body io.Reader) (req *http.Request, err error) {
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
}
func (e *EWSCalendar) getCalendarFolderID() (id *FolderId, err error) {
req, err := e.prepareRequest(strings.NewReader(folderIdRequest))
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 folderID: %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 == "FolderId" {
id = &FolderId{}
err = d.DecodeElement(id, &t)
// The first one is enough
return
}
}
}
}
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() { func main() {
s := LoadSettings() s := LoadSettings()
@ -181,49 +37,3 @@ func main() {
item.HRef, item.UID(), item.Summary(), item.Start(), item.End()) item.HRef, item.UID(), item.Summary(), item.Start(), item.End())
} }
} }
const folderIdRequest = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:GetFolder>
<m:FolderShape>
<t:BaseShape>AllProperties</t:BaseShape>
</m:FolderShape>
<m:FolderIds>
<t:DistinguishedFolderId Id="calendar" />
</m:FolderIds>
</m:GetFolder>
</soap:Body>
</soap:Envelope>`
var calendarQuery = template.Must(template.New("calendarQuery").Parse(`<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<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 }}" />
<m:ParentFolderIds>
<t:FolderId Id="{{ .FolderId.Id }}" ChangeKey="{{ .FolderId.ChangeKey }}" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>`))

196
src/calanonsync/ews.go Normal file
View File

@ -0,0 +1,196 @@
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"strings"
"text/template"
"time"
)
type FolderId struct {
Id string `xml:",attr"`
ChangeKey string `xml:",attr"`
}
type CalendarItem struct {
Subject string
UID string
Start time.Time
End time.Time
}
type EWSCalendar struct {
httpClient *http.Client
url string
username string
password string
}
func NewEWSCalendar(url, username, password string) *EWSCalendar {
// Prepare a cookie jar, since EWS uses some.
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
return &EWSCalendar{
httpClient: &http.Client{
Jar: jar,
},
url: url,
username: username,
password: password,
}
}
func (e *EWSCalendar) prepareRequest(body io.Reader) (req *http.Request, err error) {
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
}
func (e *EWSCalendar) getCalendarFolderID() (id *FolderId, err error) {
req, err := e.prepareRequest(strings.NewReader(folderIdRequest))
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 folderID: %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 == "FolderId" {
id = &FolderId{}
err = d.DecodeElement(id, &t)
// The first one is enough
return
}
}
}
}
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
}
const folderIdRequest = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:GetFolder>
<m:FolderShape>
<t:BaseShape>AllProperties</t:BaseShape>
</m:FolderShape>
<m:FolderIds>
<t:DistinguishedFolderId Id="calendar" />
</m:FolderIds>
</m:GetFolder>
</soap:Body>
</soap:Envelope>`
var calendarQuery = template.Must(template.New("calendarQuery").Parse(`<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<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 }}" />
<m:ParentFolderIds>
<t:FolderId Id="{{ .FolderId.Id }}" ChangeKey="{{ .FolderId.ChangeKey }}" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>`))