microsoftgraph / msgraph-sdk-dotnet-core

The core Microsoft Graph client library for .Net. (Microsoft.Graph.Core)
Other
150 stars 48 forks source link

Add access to Headers in BatchRequestContentCollection #862

Open Dethras opened 3 months ago

Dethras commented 3 months ago

Is your feature request related to a problem? Please describe the problem.

I have a specific case where I need to pass something through a Header to be accessed in a DelegatingHandler, with the addition of BatchRequestContentCollection, BatchRequestContent has become obsolete, and there is no way to access the Headers via BatchRequestContentCollection or via graphClient.Batch.PostAsync(BatchRequestContentCollection)

Describe the solution you'd like.

BatchRequestContentCollection or graphClient.Batch.PostAsync(BatchRequestContentCollection) should accept RequestHeaders/RequestInformation, and they should be added with every batch call made with the graphClient.Batch.PostAsync(BatchRequestContentCollection).

Additional context?

No response

andrueastman commented 3 months ago

Thanks for raising this @Dethras

To clarify,

Dethras commented 3 months ago

Hi @andrueastman

  1. This is for the entire batch request (For the https://graph.microsoft.com/v1.0/$batch call) [Note: For each individual request it is already supported through BatchRequestStep]
  2. It is possible through BatchRequestContent since I can use graphClient.Batch.ToPostRequestInformationAsync and then access the RequestHeaders through the RequestInformation
andrueastman commented 3 months ago

Thanks for the extra information here @Dethras

In my head, BatchRequestContentCollection still ends up splitting the requests into BatchRequestContent instances as the API only accepts 20 items at a time. So, this may still be possible.

Any chance you can also share a code example of how you send the request once you get the RequestInformation object?

Dethras commented 2 months ago

@andrueastman Apologies for the late reply.

Here is how I am currently doing it using the BatchRequestContent and it's ToPostRequestInformationAsync

var batchToProcess = new BatchRequestContent(graphClient, <Insert max 20 BatchRequest Steps Here>)
var requestInfo = await graphClient.Batch.ToPostRequestInformationAsync(batchToProcess);

requestInfo.Headers.Add(<Header Name>,<Header Value>);

var locationResponseHandler = new NativeResponseHandler();

requestInfo.SetResponseHandler(locationResponseHandler);

await graphClient.RequestAdapter.SendNoContentAsync(requestInfo, cancellationToken: cancellationToken);

var batchResponse = new BatchResponseContent(locationResponseHandler.Value as HttpResponseMessage);
var responses = await batchResponse.GetResponsesStatusCodesAsync();