forked from amhester/go-outlook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
246 lines (218 loc) · 6.66 KB
/
event.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package outlook
import (
"context"
"fmt"
"strings"
"time"
)
var (
// DefaultEventFields the default set of fields that will be requested from microsoft's graph api when fecthing events
DefaultEventFields = strings.Join([]string{
"id",
"start",
"end",
"createdDateTime",
"lastModifiedDateTime",
"iCalUId",
"subject",
"isAllDay",
"isCancelled",
"isOrganizer",
"showAs",
"onlineMeetingUrl",
"recurrence",
"responseStatus",
"location",
"attendees",
"organizer",
"categories",
"seriesMasterId",
}, ",")
)
// EventService manages communication with microsofts graph for event resources.
type EventService struct {
session *Session
basePath string
}
// NewEventService returns a new instance of a EventService.
func NewEventService(session *Session) *EventService {
return &EventService{
session: session,
basePath: "/events",
}
}
// EventListCall struct allowing for fluent style configuration of calls to the event list endpoint.
type EventListCall struct {
service *EventService
calendarID string
nextLink string
maxResults int64
startTime time.Time
endTime time.Time
}
// List returns a EventListCall struct
func (es *EventService) List(calendarID string) *EventListCall {
return &EventListCall{
service: es,
maxResults: 10,
calendarID: calendarID,
}
}
// MaxResults sets the $top query parameter for the event list call.
func (elc *EventListCall) MaxResults(pageSize int64) *EventListCall {
elc.maxResults = pageSize
return elc
}
// NextLink uses the link provided to set the $skip query parameter for the event list call.
func (elc *EventListCall) NextLink(link string) *EventListCall {
elc.nextLink = link
return elc
}
// StartTime sets the startDateTime query parameter for the event list call.
func (elc *EventListCall) StartTime(start time.Time) *EventListCall {
elc.startTime = start
return elc
}
// EndTime sets the endDateTime query parameter for the event list call.
func (elc *EventListCall) EndTime(end time.Time) *EventListCall {
elc.endTime = end
return elc
}
// Do executes the event list call, returning the event list result.
func (elc *EventListCall) Do(ctx context.Context) (*EventListResult, error) {
params := map[string]interface{}{
"$top": elc.maxResults,
"$count": true,
"startDateTime": elc.startTime.Format(DefaultQueryDateTimeFormat),
"endDateTime": elc.endTime.Format(DefaultQueryDateTimeFormat),
"$select": DefaultEventFields,
}
if elc.nextLink != "" {
params["$skip"] = parsePageLink(elc.nextLink, "$skip")
}
var path string
if elc.calendarID == "primary" {
path = "/calendarView"
} else {
path = fmt.Sprintf("/calendars/%s%s", elc.calendarID, "/calendarView")
}
var result EventListResult
if _, err := elc.service.session.Get(ctx, path, params, &result); err != nil {
return nil, err
}
return &result, nil
}
// EventGetCall struct allowing for fluent style configuration of calls to the event get endpoint.
type EventGetCall struct {
service *EventService
calendarID string
eventID string
}
// Get returns an instance of an EventGetCall with the given calendarID and eventID.
func (es *EventService) Get(calendarID string, eventID string) *EventGetCall {
return &EventGetCall{
service: es,
calendarID: calendarID,
eventID: eventID,
}
}
// Do executes the http get to microsoft's graph api to get the call's event.
func (egc *EventGetCall) Do(ctx context.Context) (*Event, error) {
var path string
if egc.calendarID == "primary" {
path = fmt.Sprintf("/events/%s", egc.eventID)
} else {
path = fmt.Sprintf("/calendars/%s%s/%s", egc.calendarID, egc.service.basePath, egc.eventID)
}
event := Event{}
if _, err := egc.service.session.Get(ctx, path, nil, &event); err != nil {
return nil, err
}
return &event, nil
}
// EventCreateCall struct allowing for fluent style configuration of calls to the event create endpoint.
type EventCreateCall struct {
service *EventService
calendarID string
event *Event
}
// Create returns an instance of en EventCreateCall with the given calendarID.
func (es *EventService) Create(calendarID string) *EventCreateCall {
return &EventCreateCall{
service: es,
calendarID: calendarID,
event: &Event{},
}
}
// Event sets the event on the EventCreateCall.
func (ecc *EventCreateCall) Event(event *Event) *EventCreateCall {
ecc.event = event
return ecc
}
// Do executes the http post to microsoft's graph api to create the call's event.
func (ecc *EventCreateCall) Do(ctx context.Context) (*Event, error) {
path := fmt.Sprintf("/calendars/%s%s", ecc.calendarID, ecc.service.basePath)
if _, err := ecc.service.session.Post(ctx, path, ecc.event, ecc.event); err != nil {
return nil, err
}
return ecc.event, nil
}
// EventUpdateCall struct allowing for fluent style configuration of calls to the event update endpoint.
type EventUpdateCall struct {
service *EventService
calendarID string
event *Event
}
// Update returns an instance of an EventUpdateCall with the given calendarID.
func (es *EventService) Update(calendarID string) *EventUpdateCall {
return &EventUpdateCall{
service: es,
calendarID: calendarID,
event: &Event{},
}
}
// Event sets the event on the EventUpdateCall.
func (euc *EventUpdateCall) Event(event *Event) *EventUpdateCall {
euc.event = event
return euc
}
// Do executes the http patch to microsoft's graph api to update the call's event.
func (euc *EventUpdateCall) Do(ctx context.Context) (*Event, error) {
var path string
if euc.calendarID == "primary" {
path = fmt.Sprintf("/events/%s", euc.event.ID)
} else {
path = fmt.Sprintf("/calendars/%s%s/%s", euc.calendarID, euc.service.basePath, euc.event.ID)
}
if _, err := euc.service.session.Patch(ctx, path, euc.event, euc.event); err != nil {
return nil, err
}
return euc.event, nil
}
// EventDeleteCall struct allowing for fluent style configuration of calls to the event delete endpoint.
type EventDeleteCall struct {
service *EventService
calendarID string
eventID string
}
// Delete returns an instance of an EventDeleteCall with the given calendarID and eventID.
func (es *EventService) Delete(calendarID, eventID string) *EventDeleteCall {
return &EventDeleteCall{
service: es,
calendarID: calendarID,
eventID: eventID,
}
}
// Do executes the http delete to microsoft's graph api to delete the call's event.
func (edc *EventDeleteCall) Do(ctx context.Context) error {
var path string
if edc.calendarID == "primary" {
path = fmt.Sprintf("/events/%s", edc.eventID)
} else {
path = fmt.Sprintf("/calendars/%s%s/%s", edc.calendarID, edc.service.basePath, edc.eventID)
}
if _, err := edc.service.session.Delete(ctx, path, nil, nil); err != nil {
return err
}
return nil
}