-
Notifications
You must be signed in to change notification settings - Fork 62
Proper Batching support #613
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
Merged
andrueastman
merged 3 commits into
microsoftgraph:dev
from
svrooij:feature/batching-support
Mar 7, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Microsoft.Graph.Core/Requests/Content/BatchRequestContentCollection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| namespace Microsoft.Graph | ||
| { | ||
| using Microsoft.Kiota.Abstractions; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// A collection of batch requests that are automatically managed. | ||
| /// </summary> | ||
| public class BatchRequestContentCollection | ||
| { | ||
| private readonly IBaseClient baseClient; | ||
| private readonly List<BatchRequestContent> batchRequests; | ||
| 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) | ||
| { | ||
| this.baseClient = baseClient; | ||
| batchRequests = new List<BatchRequestContent>(); | ||
| currentRequest = new BatchRequestContent(baseClient); | ||
| } | ||
|
|
||
| private void ValidateReadOnly() | ||
| { | ||
| if (readOnly) | ||
| { | ||
| throw new InvalidOperationException("Batch request collection is already executed"); | ||
| } | ||
| } | ||
|
|
||
| private void SetupCurrentRequest() | ||
| { | ||
| ValidateReadOnly(); | ||
| if (currentRequest.BatchRequestSteps.Count >= CoreConstants.BatchRequest.MaxNumberOfRequests) | ||
| { | ||
| batchRequests.Add(currentRequest); | ||
| currentRequest = new BatchRequestContent(baseClient); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a <see cref="HttpRequestMessage"/> to batch request content. | ||
| /// </summary> | ||
| /// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to use to build a <see cref="BatchRequestStep"/> to add.</param> | ||
| /// <returns>The requestId of the newly created <see cref="BatchRequestStep"/></returns> | ||
| public string AddBatchRequestStep(HttpRequestMessage httpRequestMessage) | ||
| { | ||
| SetupCurrentRequest(); | ||
| return currentRequest.AddBatchRequestStep(httpRequestMessage); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a <see cref="RequestInformation"/> to batch request content | ||
| /// </summary> | ||
| /// <param name="requestInformation">A <see cref="RequestInformation"/> to use to build a <see cref="BatchRequestStep"/> to add.</param> | ||
| /// <returns>The requestId of the newly created <see cref="BatchRequestStep"/></returns> | ||
| public Task<string> AddBatchRequestStepAsync(RequestInformation requestInformation) | ||
| { | ||
| SetupCurrentRequest(); | ||
| return currentRequest.AddBatchRequestStepAsync(requestInformation); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Removes a <see cref="BatchRequestStep"/> from batch request content for the specified id. | ||
| /// </summary> | ||
| /// <param name="requestId">A unique batch request id to remove.</param> | ||
| /// <returns>True or false based on removal or not removal of a <see cref="BatchRequestStep"/>.</returns> | ||
| public bool RemoveBatchRequestStepWithId(string requestId) | ||
| { | ||
| ValidateReadOnly(); | ||
| var removed = currentRequest.RemoveBatchRequestStepWithId(requestId); | ||
| if (!removed && batchRequests.Count > 0) | ||
| { | ||
| for (int i = 0; i < batchRequests.Count; i++) | ||
| { | ||
| removed = batchRequests[i].RemoveBatchRequestStepWithId(requestId); | ||
| if (removed) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return removed; | ||
| } | ||
|
|
||
| internal IEnumerable<BatchRequestContent> GetBatchRequestsForExecution() | ||
| { | ||
| readOnly = true; | ||
| if (currentRequest.BatchRequestSteps.Count > 0) | ||
| { | ||
| batchRequests.Add(currentRequest); | ||
| } | ||
|
|
||
| return batchRequests; | ||
| } | ||
| } | ||
| } | ||
86 changes: 86 additions & 0 deletions
86
src/Microsoft.Graph.Core/Requests/Content/BatchResponseContentCollection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| namespace Microsoft.Graph | ||
| { | ||
| using Microsoft.Kiota.Abstractions; | ||
| using Microsoft.Kiota.Abstractions.Serialization; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// Handles batch request responses. | ||
| /// </summary> | ||
| public class BatchResponseContentCollection | ||
| { | ||
| private readonly List<KeyedBatchResponseContent> batchResponses; | ||
|
|
||
| internal BatchResponseContentCollection() | ||
| { | ||
| batchResponses = new List<KeyedBatchResponseContent>(); | ||
| } | ||
|
|
||
| internal void AddResponse(IEnumerable<string> keys, BatchResponseContent content) | ||
| { | ||
| batchResponses.Add(new KeyedBatchResponseContent(new HashSet<string>(keys), content)); | ||
| } | ||
|
|
||
| private BatchResponseContent GetBatchResponseContaining(string requestId) | ||
| { | ||
| return batchResponses.FirstOrDefault(b => b.Keys.Contains(requestId))?.Response; | ||
| } | ||
|
|
||
| /// <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"/>. | ||
| /// </summary> | ||
| /// <param name="requestId">A batch request id.</param> | ||
| /// <returns>A <see cref="HttpResponseMessage"/> response object for a batch request.</returns> | ||
| public Task<HttpResponseMessage> GetResponseByIdAsync(string requestId) | ||
| { | ||
| return GetBatchResponseContaining(requestId)?.GetResponseByIdAsync(requestId) | ||
| ?? Task.FromResult<HttpResponseMessage>(null); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a batch response as a requested type for the specified batch request id. | ||
| /// </summary> | ||
| /// <param name="requestId">A batch request id.</param> | ||
| /// <param name="responseHandler">ResponseHandler to use for the response</param> | ||
| /// <returns>A deserialized object of type T<see cref="HttpResponseMessage"/>.</returns> | ||
| public Task<T> GetResponseByIdAsync<T>(string requestId, IResponseHandler responseHandler = null) where T : IParsable, new() | ||
| { | ||
| return GetBatchResponseContaining(requestId)?.GetResponseByIdAsync<T>(requestId, responseHandler) | ||
| ?? Task.FromResult<T>(default); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a batch response content as a stream | ||
| /// </summary> | ||
| /// <param name="requestId">A batch request id.</param> | ||
| /// <returns>The response stream of the batch response object</returns> | ||
| /// <remarks> Stream should be dispose once done with.</remarks> | ||
| public Task<Stream> GetResponseStreamByIdAsync(string requestId) | ||
| { | ||
| var batch = GetBatchResponseContaining(requestId); | ||
| if (batch is null) | ||
| { | ||
| return Task.FromResult<Stream>(default); | ||
| } | ||
|
|
||
| return batch.GetResponseStreamByIdAsync(requestId); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets all batch responses <see cref="Dictionary{String, HttpResponseMessage}"/>. | ||
| /// 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> | ||
| public Task<Dictionary<string, HttpResponseMessage>> GetResponsesAsync() | ||
| { | ||
| throw new NotImplementedException("GetResponsesAsync() is not available in the BatchCollection"); | ||
| } | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Microsoft.Graph.Core/Requests/Content/KeyedBatchResponseContent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace Microsoft.Graph | ||
| { | ||
| using System.Collections.Generic; | ||
|
|
||
| internal class KeyedBatchResponseContent | ||
| { | ||
| internal readonly HashSet<string> Keys; | ||
| internal readonly BatchResponseContent Response; | ||
|
|
||
| public KeyedBatchResponseContent(HashSet<string> keys, BatchResponseContent response) | ||
| { | ||
| Keys = keys; | ||
| Response = response; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.