1 module office365;
2 
3 import vibe.d;
4 
5 ///
6 struct Events
7 {
8 	///
9 	Event[] value;
10 }
11 
12 ///
13 enum DefaultFolder
14 {
15 	Inbox = "inbox",
16 	SentItems = "sentitems",
17 	Drafts = "drafts",
18 }
19 
20 ///
21 struct Messages
22 {
23 	///
24 	Message[] value;
25 	///
26 	@optional
27 	@name("@odata.count")
28 	int count;
29 }
30 
31 ///
32 struct EventDate
33 {
34 	///
35 	@name("DateTime")
36 	string _DateTime;
37 	///
38 	string TimeZone;
39 }
40 
41 ///
42 struct EventLocation
43 {
44 	///
45 	string DisplayName;
46 }
47 
48 /++ 
49  + Microsoft.OutlookServices.Event
50  + see https://msdn.microsoft.com/en-us/office/office365/api/complex-types-for-mail-contacts-calendar#EventResource
51  +/
52 struct Event
53 {
54 	///
55 	string[] Categories;
56 	///
57 	bool IsReminderOn;
58 	///
59 	bool HasAttachments;
60 	///
61 	string Subject;
62 	///
63 	bool IsAllDay;
64 	///
65 	bool IsCancelled;
66 	///
67 	bool IsOrganizer;
68 	///
69 	@byName EventImportance Importance;
70 	///
71 	EventDate Start;
72 	///
73 	EventDate End;
74 	///
75 	EventLocation Location;
76 	///
77 	EventResponseStatus ResponseStatus;
78 	///
79 	EventAttendees[] Attendees;
80 	///
81 	@optional
82 	string OnlineMeetingUrl;
83 
84 }
85 
86 ///
87 struct EventAttendees
88 {
89 	///
90 	EventAttendeesType Type;
91 	///
92 	EventResponseStatus Status;
93 	///
94 	@name("EmailAddress") 
95 	EmailAddress _EmailAddress;
96 }
97 
98 ///
99 enum EventAttendeesType
100 {
101 	Required,
102 	Optional
103 }
104 
105 ///
106 enum EventImportance
107 {
108 	Normal,
109 	High,
110 	Low
111 }
112 
113 ///
114 enum ResponseState
115 {
116 	Organizer,
117 	None,
118 	NotResponded,
119 	Accepted,
120 	Declined
121 }
122 
123 ///
124 struct EventResponseStatus 
125 {
126 	///
127 	@byName ResponseState Response;
128 	///
129 	string Time;
130 }
131 
132 
133 ///
134 struct MessageBody
135 {
136 	///
137 	string ContentType;
138 	///
139 	string Content;
140 }
141 
142 ///
143 enum Select
144 {
145 	Subject,
146 	BodyPreview,
147 	SentDateTime,
148 	Body,
149 	Sender,
150 	From,
151 }
152 
153 ///
154 struct SelectBuilder
155 {
156 	private string content;
157 
158 	///
159 	auto add(Select selector)
160 	{
161 		import std.conv:to;
162 
163 		if(content.length!=0)
164 			content ~= ",";
165 
166 		content ~= to!string(selector);
167 
168 		return this;
169 	}
170 
171 	///
172 	string toString() const
173 	{
174 		return content;
175 	}
176 }
177 
178 ///
179 struct EmailAddress
180 {
181 	///
182 	struct NameAdressTuple
183 	{
184 		///
185 		string Name;
186 		///
187 		string Address;
188 	}
189 
190 	///
191 	NameAdressTuple EmailAddress;
192 }
193 
194 ///
195 struct Message
196 {
197 	///
198 	string Id;
199 	///
200 	@optional
201 	string Subject;
202 	///
203 	@optional
204 	MessageBody Body;
205 	///
206 	@optional
207 	string BodyPreview;
208 	///
209 	@optional
210 	string SentDateTime;
211 	///
212 	@optional
213 	EmailAddress Sender;
214 	///
215 	@optional
216 	EmailAddress From;
217 	///
218 	@optional
219 	bool IsRead;
220 }
221 
222 ///
223 @path("/api/v2.0/me")
224 interface Office365Api
225 {
226 	/// uses https://outlook.office.com/api/v2.0/me/calendarview
227 	@method(HTTPMethod.GET)
228 	Events calendarview(DateTime startdatetime, DateTime enddatetime);
229 
230 	@queryParam("_filter", "$filter")
231 	@queryParam("_top", "$top")
232 	@queryParam("_select", "$select")
233 	@path("/mailfolders/:folder/messages")
234 	@method(HTTPMethod.GET)
235 	{
236 		@queryParam("_count", "$count")
237 		Messages messages(string _folder, bool _count, string _filter, string _select="", int _top=10);
238 		Messages messages(string _folder, string _filter="", string _select="", int _top=10);
239 	}
240 
241 	@path("/messages/:id")
242 	@method(HTTPMethod.DELETE)
243 	void deleteMessage(string _id);
244 
245 	@path("/messages/:id")
246 	@method(HTTPMethod.PATCH)
247 	{
248 		Message updateRead(string _id, bool IsRead);
249 		Message updateCategories(string _id, string[] Categories);
250 	}
251 }
252 
253 ///
254 static Office365Api createOfficeApi(string access_token)
255 {
256 	import vibe.web.rest:RestInterfaceClient;
257 	import vibe.http.client:HTTPClientRequest;
258 	auto res = new RestInterfaceClient!Office365Api("https://outlook.office.com");
259 
260 	res.requestFilter = (HTTPClientRequest req){
261 		req.headers["Authorization"] = "Bearer "~access_token;
262 	};
263 
264 	return res;
265 }