Exemplo n.º 1
0
        /// <summary>
        /// Make a GraphAPI batch request, optionally passing in a function to process the returned Dictionary<string, HttpResponseMessage>. This method will dispose all HttpResponseMessage
        /// before returning
        /// </summary>
        /// <param name="httpClient"></param>
        /// <param name="content"></param>
        /// <param name="asyncresponsehandler"></param>
        /// <returns></returns>
        public async Task <BatchResponseContent> MakeBatchRequest(HttpClient httpClient, BatchRequestContent content, Func <Dictionary <string, HttpResponseMessage>, Task> asyncresponsehandler)
        {
            //make the batch request
            string requesturl = Connector.Instance.RootUrl + "/$batch";
            BatchResponseContent batchResponseContent = null;

            while (string.IsNullOrWhiteSpace(requesturl) == false)
            {
                HttpResponseMessage response = await this.PostAsync(httpClient, requesturl, content);

                batchResponseContent = new BatchResponseContent(response);

                Dictionary <string, HttpResponseMessage> responses = await batchResponseContent.GetResponsesAsync();

                if (asyncresponsehandler != null)
                {
                    await asyncresponsehandler(responses);
                }

                foreach (HttpResponseMessage message in responses.Values)
                {
                    message.Dispose();
                }
                requesturl = await batchResponseContent.GetNextLinkAsync();
            }

            return(batchResponseContent);
        }
Exemplo n.º 2
0
        private static async Task TestBatch(HttpClient httpClient)
        {
            Console.WriteLine("Fetching Batch");
            // Create http GET request.
            HttpRequestMessage httpRequestMessage1 = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/");

            // Create http POST request.
            String jsonContent = "{" +
                                 "\"displayName\": \"My Notebook2\"" +
                                 "}";
            HttpRequestMessage httpRequestMessage2 = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/me/onenote/notebooks")
            {
                Content = new StringContent(jsonContent, Encoding.UTF8, "application/json")
            };

            // Create batch request steps with request ids.
            BatchRequestStep requestStep1 = new BatchRequestStep("1", httpRequestMessage1, null);
            BatchRequestStep requestStep2 = new BatchRequestStep("2", httpRequestMessage2, new List <string> {
                "1"
            });

            // Add batch request steps to BatchRequestContent.
            BatchRequestContent batchRequestContent = new BatchRequestContent();

            batchRequestContent.AddBatchRequestStep(requestStep1);
            batchRequestContent.AddBatchRequestStep(requestStep2);

            // Send batch request with BatchRequestContent.
            HttpResponseMessage response = await httpClient.PostAsync("https://graph.microsoft.com/v1.0/$batch", batchRequestContent);

            // Handle http responses using BatchResponseContent.
            BatchResponseContent batchResponseContent          = new BatchResponseContent(response);
            Dictionary <string, HttpResponseMessage> responses = await batchResponseContent.GetResponsesAsync();

            HttpResponseMessage httpResponse = await batchResponseContent.GetResponseByIdAsync("1");

            string responseString = await httpResponse.Content.ReadAsStringAsync();

            Console.WriteLine(responseString);

            HttpResponseMessage httpResponse2 = await batchResponseContent.GetResponseByIdAsync("2");

            responseString = await httpResponse2.Content.ReadAsStringAsync();

            Console.WriteLine(responseString);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            string nextLink = await batchResponseContent.GetNextLinkAsync();

            Console.WriteLine(nextLink);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
        }
Exemplo n.º 3
0
        public async Task JsonBatchRequest()
        {
            string token = await GetAccessTokenUsingPasswordGrant();

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

            HttpRequestMessage httpRequestMessage1 = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/");

            String body = "{" +
                          "\"displayName\": \"My Notebook\"" +
                          "}";
            HttpRequestMessage httpRequestMessage2 = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/me/onenote/notebooks");

            httpRequestMessage2.Content = new StringContent(body, Encoding.UTF8, "application/json");

            BatchRequestStep requestStep1 = new BatchRequestStep("1", httpRequestMessage1, null);
            BatchRequestStep requestStep2 = new BatchRequestStep("2", httpRequestMessage2, new List <string> {
                "1"
            });

            BatchRequestContent batchRequestContent = new BatchRequestContent();

            batchRequestContent.AddBatchRequestStep(requestStep1);
            batchRequestContent.AddBatchRequestStep(requestStep2);

            HttpResponseMessage response = await httpClient.PostAsync("https://graph.microsoft.com/v1.0/$batch", batchRequestContent);

            BatchResponseContent batchResponseContent          = new BatchResponseContent(response);
            Dictionary <string, HttpResponseMessage> responses = await batchResponseContent.GetResponsesAsync();

            HttpResponseMessage httpResponse = await batchResponseContent.GetResponseByIdAsync("1");

            string nextLink = await batchResponseContent.GetNextLinkAsync();

            Assert.True(responses.Count.Equals(2));
        }