forked from andlabs/ews
-
Notifications
You must be signed in to change notification settings - Fork 28
/
create_item.go
155 lines (126 loc) · 4.15 KB
/
create_item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package ews
import (
"encoding/xml"
"errors"
"time"
)
type CreateItem struct {
XMLName struct{} `xml:"m:CreateItem"`
MessageDisposition string `xml:"MessageDisposition,attr"`
SendMeetingInvitations string `xml:"SendMeetingInvitations,attr"`
SavedItemFolderId SavedItemFolderId `xml:"m:SavedItemFolderId"`
Items Items `xml:"m:Items"`
}
type Items struct {
Message []Message `xml:"t:Message"`
CalendarItem []CalendarItem `xml:"t:CalendarItem"`
}
type SavedItemFolderId struct {
DistinguishedFolderId DistinguishedFolderId `xml:"t:DistinguishedFolderId"`
}
type Message struct {
ItemClass string `xml:"t:ItemClass"`
Subject string `xml:"t:Subject"`
Body Body `xml:"t:Body"`
Sender OneMailbox `xml:"t:Sender"`
ToRecipients XMailbox `xml:"t:ToRecipients"`
}
type CalendarItem struct {
Subject string `xml:"t:Subject"`
Body Body `xml:"t:Body"`
ReminderIsSet bool `xml:"t:ReminderIsSet"`
ReminderMinutesBeforeStart int `xml:"t:ReminderMinutesBeforeStart"`
Start time.Time `xml:"t:Start"`
End time.Time `xml:"t:End"`
IsAllDayEvent bool `xml:"t:IsAllDayEvent"`
LegacyFreeBusyStatus string `xml:"t:LegacyFreeBusyStatus"`
Location string `xml:"t:Location"`
RequiredAttendees []Attendees `xml:"t:RequiredAttendees"`
OptionalAttendees []Attendees `xml:"t:OptionalAttendees"`
Resources []Attendees `xml:"t:Resources"`
}
type Body struct {
BodyType string `xml:"BodyType,attr"`
Body []byte `xml:",chardata"`
}
type OneMailbox struct {
Mailbox Mailbox `xml:"t:Mailbox"`
}
type XMailbox struct {
Mailbox []Mailbox `xml:"t:Mailbox"`
}
type Mailbox struct {
EmailAddress string `xml:"t:EmailAddress"`
}
type Attendee struct {
Mailbox Mailbox `xml:"t:Mailbox"`
}
type Attendees struct {
Attendee []Attendee `xml:"t:Attendee"`
}
type createItemResponseBodyEnvelop struct {
XMLName struct{} `xml:"Envelope"`
Body createItemResponseBody `xml:"Body"`
}
type createItemResponseBody struct {
CreateItemResponse CreateItemResponse `xml:"CreateItemResponse"`
}
type CreateItemResponse struct {
ResponseMessages ResponseMessages `xml:"ResponseMessages"`
}
type ResponseMessages struct {
CreateItemResponseMessage Response `xml:"CreateItemResponseMessage"`
}
// CreateMessageItem
// https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-email-message
func CreateMessageItem(c Client, m ...Message) error {
item := &CreateItem{
MessageDisposition: "SendAndSaveCopy",
SavedItemFolderId: SavedItemFolderId{DistinguishedFolderId{Id: "sentitems"}},
}
item.Items.Message = append(item.Items.Message, m...)
xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
}
bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
}
if err := checkCreateItemResponseForErrors(bb); err != nil {
return err
}
return nil
}
// CreateCalendarItem
// https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-calendar-item
func CreateCalendarItem(c Client, ci ...CalendarItem) error {
item := &CreateItem{
SendMeetingInvitations: "SendToAllAndSaveCopy",
SavedItemFolderId: SavedItemFolderId{DistinguishedFolderId{Id: "calendar"}},
}
item.Items.CalendarItem = append(item.Items.CalendarItem, ci...)
xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
}
bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
}
if err := checkCreateItemResponseForErrors(bb); err != nil {
return err
}
return nil
}
func checkCreateItemResponseForErrors(bb []byte) error {
var soapResp createItemResponseBodyEnvelop
if err := xml.Unmarshal(bb, &soapResp); err != nil {
return err
}
resp := soapResp.Body.CreateItemResponse.ResponseMessages.CreateItemResponseMessage
if resp.ResponseClass == ResponseClassError {
return errors.New(resp.MessageText)
}
return nil
}