diff --git a/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContentCollection.cs b/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContentCollection.cs index 52a4bbcae..934f408bd 100644 --- a/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContentCollection.cs +++ b/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContentCollection.cs @@ -3,6 +3,7 @@ using Microsoft.Kiota.Abstractions; using System; using System.Collections.Generic; + using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -13,6 +14,7 @@ public class BatchRequestContentCollection { private readonly IBaseClient baseClient; private readonly List batchRequests; + private readonly int batchRequestLimit; private BatchRequestContent currentRequest; private bool readOnly = false; @@ -20,9 +22,28 @@ public class BatchRequestContentCollection /// Constructs a new . /// /// The for making requests - public BatchRequestContentCollection(IBaseClient baseClient) + public BatchRequestContentCollection(IBaseClient baseClient) : this (baseClient, CoreConstants.BatchRequest.MaxNumberOfRequests) { + + } + + /// + /// Constructs a new . + /// + /// The for making requests + /// Number of requests that may be placed in a single batch + public BatchRequestContentCollection(IBaseClient baseClient, int batchRequestLimit) + { + if(baseClient == null) + { + throw new ArgumentNullException(nameof(baseClient)); + } + if (batchRequestLimit < 2 || batchRequestLimit > CoreConstants.BatchRequest.MaxNumberOfRequests) + { + throw new ArgumentOutOfRangeException(nameof(batchRequestLimit)); + } this.baseClient = baseClient; + this.batchRequestLimit = batchRequestLimit; batchRequests = new List(); currentRequest = new BatchRequestContent(baseClient); } @@ -38,7 +59,7 @@ private void ValidateReadOnly() private void SetupCurrentRequest() { ValidateReadOnly(); - if (currentRequest.BatchRequestSteps.Count >= CoreConstants.BatchRequest.MaxNumberOfRequests) + if (currentRequest.BatchRequestSteps.Count >= batchRequestLimit) { batchRequests.Add(currentRequest); currentRequest = new BatchRequestContent(baseClient); @@ -100,5 +121,25 @@ internal IEnumerable GetBatchRequestsForExecution() return batchRequests; } + + /// + /// A BatchRequestSteps property. + /// + public IReadOnlyDictionary BatchRequestSteps { get + { + if (batchRequests.Count > 0) + { + IEnumerable> result = currentRequest.BatchRequestSteps; + foreach ( var request in batchRequests) + { + result = result.Concat(request.BatchRequestSteps); + } + + return result.ToDictionary(x => x.Key, x => x.Value); + } + + return currentRequest.BatchRequestSteps; + } + } } } \ No newline at end of file diff --git a/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContent.cs b/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContent.cs index 3233f592d..18ecdc2e3 100644 --- a/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContent.cs +++ b/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContent.cs @@ -55,6 +55,25 @@ public async Task> GetResponsesAsync() return responseMessages; } + /// + /// Gets all batch responses statuscodes . + /// + /// A Dictionary of id and representing batch responses. + public async Task> GetResponsesStatusCodesAsync() + { + Dictionary statuscodes = new Dictionary(); + jBatchResponseObject = jBatchResponseObject ?? await GetBatchResponseContentAsync().ConfigureAwait(false); + if (jBatchResponseObject == null) + return statuscodes; + + if (jBatchResponseObject.RootElement.TryGetProperty(CoreConstants.BatchRequest.Responses, out JsonElement jResponses) && jResponses.ValueKind == JsonValueKind.Array) + { + foreach (JsonElement jResponseItem in jResponses.EnumerateArray()) + statuscodes.Add(jResponseItem.GetProperty(CoreConstants.BatchRequest.Id).ToString(), GetStatusCodeFromJObject(jResponseItem)); + } + return statuscodes; + } + /// /// Gets a batch response as for the specified batch request id. /// The returned MUST be disposed since it implements an . @@ -170,6 +189,15 @@ private HttpResponseMessage GetResponseMessageFromJObject(JsonElement jResponseI return responseMessage; } + + private HttpStatusCode GetStatusCodeFromJObject(JsonElement jResponseItem) + { + if (jResponseItem.TryGetProperty(CoreConstants.BatchRequest.Status, out JsonElement status)) + { + return (HttpStatusCode)int.Parse(status.ToString()); + } + throw new ArgumentException("Response does not contain statuscode"); + } /// /// Gets the of a batch response as a . /// diff --git a/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContentCollection.cs b/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContentCollection.cs index 94db00c01..953a3bfb8 100644 --- a/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContentCollection.cs +++ b/src/Microsoft.Graph.Core/Requests/Content/BatchResponseContentCollection.cs @@ -6,7 +6,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; + using System.Net; using System.Net.Http; + using System.Text.Json; using System.Threading.Tasks; /// @@ -77,10 +79,29 @@ public Task GetResponseStreamByIdAsync(string requestId) /// All in the dictionary MUST be disposed since they implement . /// /// A Dictionary of id and representing batch responses. - /// Not implemented, need help + /// Not implemented, use GetResponsesStatusCodesAsync and fetch individual responses + [Obsolete("use GetResponsesStatusCodesAsync and then GetResponseByIdAsync")] public Task> GetResponsesAsync() { throw new NotImplementedException("GetResponsesAsync() is not available in the BatchCollection"); } + + /// + /// Gets all batch responses statuscodes . + /// + /// A Dictionary of id and representing batch responses. + public async Task> GetResponsesStatusCodesAsync() + { + Dictionary statuscodes = new Dictionary(); + foreach(var response in batchResponses) + { + var batchStatusCodes = await response.Response.GetResponsesStatusCodesAsync(); + foreach(var result in batchStatusCodes) + { + statuscodes.Add(result.Key, result.Value); + } + } + return statuscodes; + } } } \ No newline at end of file