Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow checking statuscodes of batch response without parsing step body #626

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,16 +14,36 @@ public class BatchRequestContentCollection
{
private readonly IBaseClient baseClient;
private readonly List<BatchRequestContent> batchRequests;
private readonly int batchRequestLimit;
private BatchRequestContent currentRequest;
private bool readOnly = false;

/// <summary>
/// Constructs a new <see cref="BatchRequestContentCollection"/>.
/// </summary>
/// <param name="baseClient">The <see cref="IBaseClient"/> for making requests</param>
public BatchRequestContentCollection(IBaseClient baseClient)
public BatchRequestContentCollection(IBaseClient baseClient) : this (baseClient, CoreConstants.BatchRequest.MaxNumberOfRequests)
{

}

/// <summary>
/// Constructs a new <see cref="BatchRequestContentCollection"/>.
/// </summary>
/// <param name="baseClient">The <see cref="IBaseClient"/> for making requests</param>
/// <param name="batchRequestLimit">Number of requests that may be placed in a single batch</param>
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;
svrooij marked this conversation as resolved.
Show resolved Hide resolved
this.batchRequestLimit = batchRequestLimit;
batchRequests = new List<BatchRequestContent>();
currentRequest = new BatchRequestContent(baseClient);
}
Expand All @@ -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);
Expand Down Expand Up @@ -100,5 +121,25 @@ internal IEnumerable<BatchRequestContent> GetBatchRequestsForExecution()

return batchRequests;
}

/// <summary>
/// A BatchRequestSteps property.
/// </summary>
public IReadOnlyDictionary<string, BatchRequestStep> BatchRequestSteps { get
{
if (batchRequests.Count > 0)
{
IEnumerable<KeyValuePair<string, BatchRequestStep>> 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;
}
}
}
}
28 changes: 28 additions & 0 deletions src/Microsoft.Graph.Core/Requests/Content/BatchResponseContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public async Task<Dictionary<string, HttpResponseMessage>> GetResponsesAsync()
return responseMessages;
}

/// <summary>
/// Gets all batch responses statuscodes <see cref="Dictionary{String, HttpStatusCode}"/>.
/// </summary>
/// <returns>A Dictionary of id and <see cref="HttpStatusCode"/> representing batch responses.</returns>
public async Task<Dictionary<string, HttpStatusCode>> GetResponsesStatusCodesAsync()
{
Dictionary<string, HttpStatusCode> statuscodes = new Dictionary<string, HttpStatusCode>();
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;
}

/// <summary>
/// Gets a batch response as <see cref="HttpResponseMessage"/> for the specified batch request id.
/// The returned <see cref="HttpResponseMessage"/> MUST be disposed since it implements an <see cref="IDisposable"/>.
Expand Down Expand Up @@ -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");
}
/// <summary>
/// Gets the <see cref="HttpContent"/> of a batch response as a <see cref="JsonDocument"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -77,10 +79,29 @@ public Task<Stream> GetResponseStreamByIdAsync(string requestId)
/// All <see cref="HttpResponseMessage"/> in the dictionary MUST be disposed since they implement <see cref="IDisposable"/>.
/// </summary>
/// <returns>A Dictionary of id and <see cref="HttpResponseMessage"/> representing batch responses.</returns>
/// <remarks>Not implemented, need help</remarks>
/// <remarks>Not implemented, use GetResponsesStatusCodesAsync and fetch individual responses</remarks>
[Obsolete("use GetResponsesStatusCodesAsync and then GetResponseByIdAsync")]
public Task<Dictionary<string, HttpResponseMessage>> GetResponsesAsync()
{
throw new NotImplementedException("GetResponsesAsync() is not available in the BatchCollection");
}

/// <summary>
/// Gets all batch responses statuscodes <see cref="Dictionary{String, HttpStatusCode}"/>.
/// </summary>
/// <returns>A Dictionary of id and <see cref="HttpStatusCode"/> representing batch responses.</returns>
public async Task<Dictionary<string, HttpStatusCode>> GetResponsesStatusCodesAsync()
{
Dictionary<string, HttpStatusCode> statuscodes = new Dictionary<string, HttpStatusCode>();
foreach(var response in batchResponses)
{
var batchStatusCodes = await response.Response.GetResponsesStatusCodesAsync();
foreach(var result in batchStatusCodes)
{
statuscodes.Add(result.Key, result.Value);
}
}
return statuscodes;
}
}
}