This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathEventsSnippets.java
More file actions
230 lines (202 loc) · 10.1 KB
/
EventsSnippets.java
File metadata and controls
230 lines (202 loc) · 10.1 KB
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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
package com.microsoft.graph.snippets.snippet;
import com.google.gson.JsonObject;
import com.microsoft.graph.concurrency.ICallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.models.extensions.Attendee;
import com.microsoft.graph.models.extensions.DateTimeTimeZone;
import com.microsoft.graph.models.extensions.EmailAddress;
import com.microsoft.graph.models.extensions.Event;
import com.microsoft.graph.models.extensions.ItemBody;
import com.microsoft.graph.models.extensions.Location;
import com.microsoft.graph.models.generated.AttendeeType;
import com.microsoft.graph.models.generated.BodyType;
import com.microsoft.graph.requests.extensions.IEventCollectionPage;
import org.joda.time.DateTime;
import java.util.Collections;
import static com.microsoft.graph.snippets.R.array.create_event;
import static com.microsoft.graph.snippets.R.array.delete_event;
import static com.microsoft.graph.snippets.R.array.get_user_events;
import static com.microsoft.graph.snippets.R.array.update_event;
public abstract class EventsSnippets<Result> extends AbstractSnippet<Result> {
public EventsSnippets(Integer descriptionArray) {
super(SnippetCategory.eventsSnippetCategory, descriptionArray);
}
static EventsSnippets[] getEventsSnippets() {
return new EventsSnippets[]{
// Marker element
new EventsSnippets(null) {
@Override
public void request(ICallback callback) {
//No implementation
}
},
//Snippets
/*
* Get all events for the signed in user.
* GET https://graph.microsoft.com/{version}/me/events
* @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_list_events
*/
new EventsSnippets<JsonObject>(get_user_events) {
@Override
public void request(final ICallback<JsonObject> callback) {
mGraphServiceClient
.me()
.events()
.buildRequest()
.get(new ICallback<IEventCollectionPage>() {
@Override
public void success(IEventCollectionPage iEventCollectionPage) {
callback.success(iEventCollectionPage.getRawObject());
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
},
/*
* Adds an event to the signed-in user\'s calendar.
* POST https://graph.microsoft.com/{version}/me/events
* @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_post_events
*/
new EventsSnippets<JsonObject>(create_event) {
@Override
public void request(final ICallback<JsonObject> callback) {
Event event = createEventObject();
mGraphServiceClient
.me()
.events()
.buildRequest()
.post(event, new ICallback<Event>() {
@Override
public void success(Event event) {
callback.success(event.getRawObject());
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
},
/*
* Update an event
* PATCH https://graph.microsoft.com/{version}/me/events/{Event.Id}
* @see https://graph.microsoft.io/docs/api-reference/v1.0/api/event_update
*/
new EventsSnippets<JsonObject>(update_event) {
@Override
public void request(final ICallback<JsonObject> callback) {
Event event = createEventObject();
mGraphServiceClient
.me()
.events()
.buildRequest()
.post(event, new ICallback<Event>() {
@Override
public void success(Event event) {
// Update the event object
event.subject = "Updated event";
mGraphServiceClient
.me()
.events()
.byId(event.id)
.buildRequest()
.patch(event, new ICallback<Event>() {
@Override
public void success(Event event) {
callback.success(event.getRawObject());
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
},
/*
* Delete an event
* DELETE https://graph.microsoft.com/{version}/me/events/{Event.Id}
* @see https://graph.microsoft.io/docs/api-reference/v1.0/api/event_delete
*/
new EventsSnippets<JsonObject>(delete_event) {
@Override
public void request(final ICallback<JsonObject> callback) {
Event event = createEventObject();
mGraphServiceClient
.me()
.events()
.buildRequest()
.post(event, new ICallback<Event>() {
@Override
public void success(Event event) {
// Update the event object
event.subject = "Updated event";
mGraphServiceClient
.me()
.events()
.byId(event.id)
.buildRequest()
.delete(new ICallback<Event>() {
@Override
public void success(Event aVoid) {
callback.success(null);
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
}
};
}
public abstract void request(ICallback<Result> callback);
private static Event createEventObject() {
Event event = new Event();
event.subject = "Microsoft Graph SDK Discussion";
// set start time to now
DateTimeTimeZone start = new DateTimeTimeZone();
start.dateTime = DateTime.now().toString();
event.start = start;
// and end in 1 hr
DateTimeTimeZone end = new DateTimeTimeZone();
end.dateTime = DateTime.now().plusHours(1).toString();
event.end = end;
// set the timezone
start.timeZone = end.timeZone = "UTC";
// set a location
Location location = new Location();
location.displayName = "Bill's Office";
event.location = location;
// add attendees
Attendee attendee = new Attendee();
attendee.type = AttendeeType.REQUIRED;
attendee.emailAddress = new EmailAddress();
attendee.emailAddress.address = "mara@fabrikam.com";
event.attendees = Collections.singletonList(attendee);
// add a msg
ItemBody msg = new ItemBody();
msg.content = "Let's discuss the Microsoft Graph SDK.";
msg.contentType = BodyType.TEXT;
event.body = msg;
return event;
}
}