|
| 1 | +//********************************************************* |
| 2 | +// Copyright (c) Microsoft Corporation |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the ""License""); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS |
| 12 | +// OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 13 | +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. |
| 15 | +// |
| 16 | +// See the Apache Version 2.0 License for specific language |
| 17 | +// governing permissions and limitations under the License. |
| 18 | +//********************************************************* |
| 19 | + |
| 20 | +using Newtonsoft.Json; |
| 21 | +using System.Collections.Generic; |
| 22 | +using System.Diagnostics; |
| 23 | +using System.Linq; |
| 24 | +using System.Net; |
| 25 | +using System.Net.Http; |
| 26 | +using System.Net.Http.Headers; |
| 27 | +using System.Text; |
| 28 | +using System.Threading.Tasks; |
| 29 | + |
| 30 | +namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.SectionGroups |
| 31 | +{ |
| 32 | + /// <summary> |
| 33 | + /// Class to show a selection of examples creating section groups via HTTP POST to the OneNote API |
| 34 | + /// - Creating a new section group is represented via the POST HTTP verb. |
| 35 | + /// - Creating a new section group under a given notebook is represented by the Uri: https://www.onenote.com/api/beta/me/notes/notebooks/{notebookId}/sectiongroups |
| 36 | + /// - Creating a new section group under a given section group is represented by the Uri: https://www.onenote.com/api/beta/me/notes/sectiongroups/{sectionGroupId}/sectiongroups |
| 37 | + /// For more info, see http://dev.onenote.com/docs |
| 38 | + /// </summary> |
| 39 | + /// <remarks> |
| 40 | + /// NOTE: All create-sectiongroups operations require a parent notebook or parent sectiongroup. |
| 41 | + /// The section group name is specified in the request body. |
| 42 | + /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. |
| 43 | + /// You may notice code blocks being duplicated in various places in this project. |
| 44 | + /// We have deliberately added these code blocks to allow anyone browsing the sample |
| 45 | + /// to easily view all related functionality in near proximity. |
| 46 | + /// </remarks> |
| 47 | + /// <code> |
| 48 | + /// var client = new HttpClient(); |
| 49 | + /// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 50 | + /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); |
| 51 | + /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/me/notes/notebooks/{notebookId}/sectiongroups") |
| 52 | + /// { |
| 53 | + /// Content = new StringContent("{name: NewSectionGroupName }", System.Text.Encoding.UTF8, "application/json") |
| 54 | + /// }; |
| 55 | + /// HttpResponseMessage response = await client.SendAsync(createMessage); |
| 56 | + /// </code> |
| 57 | + public static class PostSectionGroupsExample |
| 58 | + { |
| 59 | + #region Examples of POST https://www.onenote.com/api/beta/me/notes/notebooks/{notebookId}/sectiongroups |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// BETA Create a section group with a given name under a given notebookId |
| 63 | + /// </summary> |
| 64 | + /// <param name="debug">Run the code under the debugger</param> |
| 65 | + /// <param name="notebookId">parent notebook's Id</param> |
| 66 | + /// <param name="sectionName">name of the section group to create</param> |
| 67 | + /// <param name="provider"></param> |
| 68 | + /// <param name="apiRoute"></param> |
| 69 | + /// <remarks>Create section group using a application/json content type</remarks> |
| 70 | + /// <returns>The converted HTTP response message</returns> |
| 71 | + public static async Task<ApiBaseResponse> CreateSectionGroupInNotebook(bool debug, string notebookId, string sectionGroupName, AuthProvider provider, string apiRoute) |
| 72 | + { |
| 73 | + if (debug) |
| 74 | + { |
| 75 | + Debugger.Launch(); |
| 76 | + Debugger.Break(); |
| 77 | + } |
| 78 | + |
| 79 | + var client = new HttpClient(); |
| 80 | + |
| 81 | + // Note: API only supports JSON response. |
| 82 | + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 83 | + |
| 84 | + // Not adding the Authentication header would produce an unauthorized call and the API will return a 401 |
| 85 | + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", |
| 86 | + await Auth.GetAuthToken(provider)); |
| 87 | + |
| 88 | + // Prepare an HTTP POST request to the SectionGroups endpoint |
| 89 | + // The request body content type is application/json and requires a name property |
| 90 | + var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + @"notebooks/" + notebookId + "/sectiongroups") |
| 91 | + { |
| 92 | + Content = new StringContent("{ name : '" + WebUtility.UrlEncode(sectionGroupName) + "' }", Encoding.UTF8, "application/json") |
| 93 | + }; |
| 94 | + |
| 95 | + HttpResponseMessage response = await client.SendAsync(createMessage); |
| 96 | + |
| 97 | + return await HttpUtils.TranslateResponse(response); |
| 98 | + } |
| 99 | + |
| 100 | + #endregion |
| 101 | + |
| 102 | + |
| 103 | + #region Examples of POST https://www.onenote.com/api/beta/me/notes/sectiongroups/{sectionGroupId}/sectiongroups |
| 104 | + /// <summary> |
| 105 | + /// BETA Create a section group with a given name under a given sectionGroupId |
| 106 | + /// </summary> |
| 107 | + /// <param name="debug">Run the code under the debugger</param> |
| 108 | + /// <param name="notebookId">parent section group's Id</param> |
| 109 | + /// <param name="sectionName">name of the section group to create</param> |
| 110 | + /// <param name="provider"></param> |
| 111 | + /// <param name="apiRoute"></param> |
| 112 | + /// <remarks>Create section group using a application/json content type</remarks> |
| 113 | + /// <returns>The converted HTTP response message</returns> |
| 114 | + public static async Task<ApiBaseResponse> CreateSectionGroupInSectionGroup(bool debug, string sectionGroupId, string sectionGroupName, AuthProvider provider, string apiRoute) |
| 115 | + { |
| 116 | + if (debug) |
| 117 | + { |
| 118 | + Debugger.Launch(); |
| 119 | + Debugger.Break(); |
| 120 | + } |
| 121 | + |
| 122 | + var client = new HttpClient(); |
| 123 | + |
| 124 | + // Note: API only supports JSON response. |
| 125 | + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 126 | + |
| 127 | + // Not adding the Authentication header would produce an unauthorized call and the API will return a 401 |
| 128 | + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", |
| 129 | + await Auth.GetAuthToken(provider)); |
| 130 | + |
| 131 | + // Prepare an HTTP POST request to the SectionGroups endpoint |
| 132 | + // The request body content type is application/json and requires a name property |
| 133 | + var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + @"sectiongroups/" + sectionGroupId + "/sectiongroups") |
| 134 | + { |
| 135 | + Content = new StringContent("{ name : '" + WebUtility.UrlEncode(sectionGroupName) + "' }", Encoding.UTF8, "application/json") |
| 136 | + }; |
| 137 | + |
| 138 | + HttpResponseMessage response = await client.SendAsync(createMessage); |
| 139 | + |
| 140 | + return await HttpUtils.TranslateResponse(response); |
| 141 | + } |
| 142 | + |
| 143 | + #endregion |
| 144 | + } |
| 145 | +} |
0 commit comments