public async Task<bool> GetNoteBook(clsOneNoteNotebook Notebook, StringBuilder sbMessage) { bool result = false; NotebookListResponse myNotebookListResponse; clsOneNoteNotebook temp = new clsOneNoteNotebook {name = NotebookName }; int IndexInList; string strRequest; strRequest = NotebooksEndpoint;// +"?$filter=contains(name,'" + tmpNotebook.name + "')"; var NoteBookListMessage = new HttpRequestMessage(HttpMethod.Get, strRequest); var httpClient = new HttpClient(); _response = null; // Check if Auth token needs to be refreshed await RefreshAuthTokenIfNeeded(); // Add Authorization header httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); // Note: API only supports JSON return type. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //// Get and parse the HTTP response from the service try { HttpResponseMessage response = await httpClient.SendAsync(NoteBookListMessage); myNotebookListResponse = await ParseNotebookListResponse(response); } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } if (myNotebookListResponse.Notebooks == null || myNotebookListResponse.Notebooks.Count == 0) { //we didn't find any notebooks so we need to create ours result = await CreateNoteBook(Notebook,sbMessage); } else { IndexInList = myNotebookListResponse.Notebooks.IndexOf(temp); if (IndexInList >= 0 ) { Notebook.id = myNotebookListResponse.Notebooks[IndexInList].id; Notebook.name = myNotebookListResponse.Notebooks[IndexInList].name; Notebook.sectionsUrl = myNotebookListResponse.Notebooks[IndexInList].sectionsUrl; result = true; } else { //our notebook is not on the list so we need to create it result = await CreateNoteBook(Notebook, sbMessage); } } return result; }
private async Task<bool> SendCreateNotebookRequest(HttpRequestMessage createMessage,clsOneNoteNotebook newNotebook, StringBuilder sbMessage) { bool result = false; var httpClient = new HttpClient(); CreateNotebookResponse successResponse; _response = null; // Check if Auth token needs to be refreshed await RefreshAuthTokenIfNeeded(); // Add Authorization header httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); // Note: API only supports JSON return type. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Get and parse the HTTP response from the service try { HttpResponseMessage response = await httpClient.SendAsync(createMessage); _response = await ParseCreateNotebookResponse(response); // Update the UI accordingly if (UpdateUIAfterCreateRequest(response) == false) { sbMessage.AppendLine("Error: " + response.StatusCode); return false; } } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } if (_response as CreateNotebookResponse != null) { successResponse = (CreateNotebookResponse)_response; } else { return false; } if (successResponse.StatusCode != HttpStatusCode.Created) { return false; } else { newNotebook.id = successResponse.id; newNotebook.name = successResponse.name; newNotebook.sectionsUrl = successResponse.sectionsUrl; newNotebook.oneNoteClientUrl = successResponse.oneNoteClientUrl; newNotebook.oneNoteWebUrl = successResponse.oneNoteWebUrl; result = true; } return result; }
/// <summary> /// Send a create page request /// </summary> /// <param name="createMessage">The HttpRequestMessage which contains the page information</param> private async Task<bool> SendCreatePageRequest(HttpRequestMessage createMessage, StringBuilder sbMessage) { var httpClient = new HttpClient(); _response = null; // Check if Auth token needs to be refreshed await RefreshAuthTokenIfNeeded(); // Add Authorization header httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); // Note: API only supports JSON return type. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Get and parse the HTTP response from the service try { HttpResponseMessage response = await httpClient.SendAsync(createMessage); _response = await ParseResponse(response); // Update the UI accordingly if (UpdateUIAfterCreateRequest(response) == false) { if (response.StatusCode == HttpStatusCode.NotFound) { //our local copy of the notebook and section list are no // longer in sync with the user's account. This could // occur because the user deleted the notebook or section sbMessage.AppendLine("Error: Notebook out of synch. Please exit the app and try again"); } else { sbMessage.AppendLine("Error: " + response.StatusCode); } return false; } } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } return true; }