This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOneNoteManager.cs
More file actions
283 lines (228 loc) · 11.8 KB
/
OneNoteManager.cs
File metadata and controls
283 lines (228 loc) · 11.8 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace AutoCADNote
{
public static class OneNoteManager
{
private static readonly Uri NotebookEndPoint = new Uri("https://www.onenote.com/api/v1.0/notebooks");
private static string pageName;
private static string pageEditUrl;
private static string AutoCADNotebook = "AutoCAD_OneNote";
private static string AutoCADNotebookSection = "Drawings";
public static string SectionsEndPoint { get; set; }
public static string PagesEndPoint { get; set; }
public static string PageEditUrl { get { return pageEditUrl; } }
public static string PageName { get { return pageName; } }
private static Dictionary<string, string> PageNameToEditUrls = new Dictionary<string, string>();
public static async Task CreatePageIfNotExists(string name)
{
// Check if a page with this name exists in the AutoCAD notebook, if not create it.
// Store the sectionsUrl and pagesurl of created notebook and section for the page
name = SanitizeName(name);
if (String.IsNullOrEmpty(SectionsEndPoint) || String.IsNullOrEmpty(PagesEndPoint)) // We dont have context of section yet, create or get if it exists
{
// Check if notebook exists, if not create it
await CreateNotebookIfNotExists();
// Check if section exists, if not create it
await CreateSectionIfNotExists();
// Now SectionsUrl and PagesUrl are populated
}
if (!String.IsNullOrEmpty(PageName) && PageName == name)
{
// This page is the one in context, so no need to create anything, PageEditUrl in context is the right one.
}
else // First time run when there is not PageName populated, or if the page name is different
{
// Notebook and section are created and verified, just the pagename is different, do only page level processing
// Check if page exists, if not create it
// First check if in-memory dictionary has this page, if so use it from there
if (PageNameToEditUrls.ContainsKey(name))
{
pageEditUrl = PageNameToEditUrls[name];
pageName = name;
}
else
{
List<PageObject> existingPages = await GetPagesInSection(AuthManager.Token);
var sameNamedPages = existingPages.Where(e => e.Title == name);
if (sameNamedPages.Any())
{
pageEditUrl = sameNamedPages.First().EditUrl;
}
else
{
pageEditUrl = await CreatePageInOneNote(name, AuthManager.Token);
}
pageName = name; // Set the name so that we can detect when it changed(new file opened),
//and decide to open that page, or create a new page
// Add to in-memory dictionary for faster retrieval in case of switching between open documents.
if (!PageNameToEditUrls.ContainsKey(pageName))
{
PageNameToEditUrls.Add(pageName, pageEditUrl);
}
}
}
}
// Create a simple HTML page and send it to the OneNote API
private static async Task<string> CreatePageInOneNote(string name, string token)
{
var client = new HttpClient();
// Note: API only supports JSON return type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
string date = DateTime.Now.ToString("o");
string simpleHtml = "<html>" +
"<head>" +
"<title>" + name + "</title>" +
"<meta name=\"created\" content=\"" + date + "\" />" +
"</head>" +
"</html>";
var createMessage = new HttpRequestMessage(HttpMethod.Post, PagesEndPoint)
{
Content = new StringContent(simpleHtml, System.Text.Encoding.UTF8, "text/html")
};
HttpResponseMessage response = await client.SendAsync(createMessage);
dynamic responseObject = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
var editUrl = responseObject["links"]["oneNoteWebUrl"]["href"].ToString();
return editUrl;
}
private static string SanitizeName(string name)
{
name = name.Replace("&", String.Empty); // Strip unsafe html characters.
var parts = name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
return String.Join(" ", parts);
}
private static async Task<List<PageObject>> GetPagesInSection(string token)
{
var pages = new List<PageObject>();
var client = new HttpClient();
// Note: API only supports JSON return type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await client.GetAsync(PagesEndPoint);
dynamic responseObject = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
foreach (var pageObject in responseObject.value)
{
pages.Add(new PageObject()
{
Id = (string)pageObject["id"],
Title = (string)pageObject["title"],
EditUrl = (string)pageObject["links"]["oneNoteWebUrl"]["href"]
});
}
return pages;
}
private static async Task CreateNotebookIfNotExists()
{
string autoCadNotebookSectionsUrl = await GetAutoCadNotebookSectionsUrlForUser(AuthManager.Token);
if (autoCadNotebookSectionsUrl != null)
{
SectionsEndPoint = autoCadNotebookSectionsUrl;
}
else
{
string createdNotebookSectionsUrl = await CreateNotebookInOneNote(AuthManager.Token);
if (!string.IsNullOrEmpty(createdNotebookSectionsUrl))
{
SectionsEndPoint = createdNotebookSectionsUrl;
}
}
}
private static async Task<string> CreateNotebookInOneNote(string token)
{
var client = new HttpClient();
// Note: API only supports JSON return type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var body = String.Format("{{ name: \"{0}\"}}", AutoCADNotebook);
var createMessage = new HttpRequestMessage(HttpMethod.Post, NotebookEndPoint)
{
Content = new StringContent(body, System.Text.Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(createMessage);
var notebookResponseJson = JToken.Parse(await response.Content.ReadAsStringAsync());
// Get the SectionsUrl property of created notebook that has notebook id within it.
return (string)notebookResponseJson["sectionsUrl"];
}
private static async Task<string> GetAutoCadNotebookSectionsUrlForUser(string token)
{
var client = new HttpClient();
// Note: API only supports JSON return type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await client.GetAsync(NotebookEndPoint);
dynamic responseObject = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
string autoCadNotebookSectionsUrl = null;
foreach (var notebookObject in responseObject.value)
{
if (((string)notebookObject["name"]).Equals(AutoCADNotebook, StringComparison.InvariantCultureIgnoreCase))
{
autoCadNotebookSectionsUrl = (string)notebookObject["sectionsUrl"];
}
}
return autoCadNotebookSectionsUrl;
}
public static async Task CreateSectionIfNotExists()
{
string autocadSectionPagesUrl = await GetAutoCadSectionPagesUrlForNotebook(AuthManager.Token);
if (!string.IsNullOrEmpty(autocadSectionPagesUrl))
{
PagesEndPoint = autocadSectionPagesUrl;
}
else
{
string createdSectionPagesUrl = await CreateSectionInOneNote(AuthManager.Token);
if (!string.IsNullOrEmpty(createdSectionPagesUrl))
{
PagesEndPoint = createdSectionPagesUrl;
}
}
}
private static async Task<string> CreateSectionInOneNote(string token)
{
var client = new HttpClient();
// Note: API only supports JSON return type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var body = String.Format("{{ name: \"{0}\"}}", AutoCADNotebookSection);
var createMessage = new HttpRequestMessage(HttpMethod.Post, SectionsEndPoint)
{
Content = new StringContent(body, System.Text.Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(createMessage);
var notebookResponseJson = JToken.Parse(await response.Content.ReadAsStringAsync());
// Get the PagesUrl property of created section that has section id within it.
return (string)notebookResponseJson["pagesUrl"];
}
public static async Task<string> GetAutoCadSectionPagesUrlForNotebook(string token)
{
var client = new HttpClient();
// Note: API only supports JSON return type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await client.GetAsync(SectionsEndPoint);
dynamic responseObject = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
string autoCadSectionPagesUrl = null;
foreach (var sectionObject in responseObject.value)
{
if (((string)sectionObject["name"]).Equals(AutoCADNotebookSection, StringComparison.InvariantCultureIgnoreCase))
{
autoCadSectionPagesUrl = (string)sectionObject["pagesUrl"];
}
}
return autoCadSectionPagesUrl;
}
}
public class PageObject
{
public string Id { get; set; }
public string Title { get; set; }
public string EditUrl { get; set; }
}
}