This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
EventsSnippets.java
203 lines (173 loc) · 8.91 KB
/
EventsSnippets.java
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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
package com.microsoft.office365.msgraphsnippetapp.snippet;
import com.microsoft.office365.microsoftgraphvos.Attendee;
import com.microsoft.office365.microsoftgraphvos.DateTimeTimeZone;
import com.microsoft.office365.microsoftgraphvos.EmailAddress;
import com.microsoft.office365.microsoftgraphvos.Event;
import com.microsoft.office365.microsoftgraphvos.ItemBody;
import com.microsoft.office365.microsoftgraphvos.Location;
import com.microsoft.office365.msgraphapiservices.MSGraphEventsService;
import org.joda.time.DateTime;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static com.microsoft.office365.msgraphsnippetapp.R.array.create_event;
import static com.microsoft.office365.msgraphsnippetapp.R.array.delete_event;
import static com.microsoft.office365.msgraphsnippetapp.R.array.get_user_events;
import static com.microsoft.office365.msgraphsnippetapp.R.array.update_event;
public abstract class EventsSnippets<Result> extends AbstractSnippet<MSGraphEventsService, Result> {
public EventsSnippets(Integer descriptionArray) {
super(SnippetCategory.eventsSnippetCategory, descriptionArray);
}
static EventsSnippets[] getEventsSnippets() {
return new EventsSnippets[]{
// Marker element
new EventsSnippets(null) {
@Override
public void request(MSGraphEventsService o, Callback 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<ResponseBody>(get_user_events) {
@Override
public void request(
MSGraphEventsService MSGraphEventsService,
Callback<ResponseBody> callback) {
MSGraphEventsService.getEvents(getVersion()).enqueue(callback);
}
},
/*
* 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<ResponseBody>(create_event) {
@Override
public void request(
MSGraphEventsService MSGraphEventsService,
Callback<ResponseBody> callback) {
MSGraphEventsService.createNewEvent(getVersion(), createEvent()).enqueue(callback);
}
},
/*
* 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<ResponseBody>(update_event) {
@Override
public void request(
final MSGraphEventsService MSGraphEventsService,
final Callback<ResponseBody> callback) {
// create a new event to update
MSGraphEventsService.createNewEvent(
getVersion(),
createEvent()).enqueue(
new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String eventId = new JSONObject(response.body().string()).getString("id");
// now that the event has been created,
// let's change the subject
Event amended = new Event();
amended.subject = "Weekly Sync Meeting";
MSGraphEventsService.updateEvent(
getVersion(),
eventId,
amended).enqueue(callback);
} catch(JSONException | IOException e) {
callback.onFailure(call, e);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
callback.onFailure(call, t);
}
}
);
}
},
/*
* 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<ResponseBody>(delete_event) {
@Override
public void request(
final MSGraphEventsService MSGraphEventsService,
final Callback<ResponseBody> callback) {
// create a new event to delete
Event event = createEvent();
MSGraphEventsService.createNewEvent(
getVersion(),
event).enqueue(
new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String eventId = new JSONObject(response.body().string()).getString("id");
// event created, now let's delete it
MSGraphEventsService.deleteEvent(
getVersion(),
eventId).enqueue(callback);
} catch(JSONException | IOException e) {
callback.onFailure(call, e);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
callback.onFailure(call, t);
}
}
);
}
}
};
}
public abstract void request(MSGraphEventsService service, Callback<Result> callback);
public static Event createEvent() {
Event event = new Event();
event.subject = "Microsoft Graph API 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 = Attendee.TYPE_REQUIRED;
attendee.emailAddress = new EmailAddress();
attendee.emailAddress.address = "mara@fabrikam.com";
event.attendees = new Attendee[]{attendee};
// add a msg
ItemBody msg = new ItemBody();
msg.content = "Let's discuss the power of the Office 365 unified API.";
msg.contentType = ItemBody.CONTENT_TYPE_TEXT;
event.body = msg;
return event;
}
}