Skip to content

Commit

Permalink
Merge branch 'Enhance_APIRequest_SetHeader' of https://github.com/Mon…
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenbe committed May 22, 2021
2 parents 1753351 + bd26e89 commit 90b9d2a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/sdk/PnP.Core/Model/Base/BaseDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ public async Task<ApiRequestResponse> ExecuteRequestAsync(ApiRequest request)
{
ExecuteRequestApiCall = true,
SkipCollectionClearing = true,
RawRequest = true
RawRequest = true,
Headers= request.Headers
}
, request.HttpMethod).ConfigureAwait(false);

Expand Down
11 changes: 9 additions & 2 deletions src/sdk/PnP.Core/Services/Core/ApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PnP.Core.Services
/// </summary>
internal struct ApiCall
{
internal ApiCall(string request, ApiType apiType, string jsonBody = null, string receivingProperty = null, bool loadPages = false)
internal ApiCall(string request, ApiType apiType, string jsonBody = null, string receivingProperty = null, bool loadPages = false, Dictionary<string, string> headers = null)
{
Type = apiType;
Request = request;
Expand All @@ -29,9 +29,10 @@ internal ApiCall(string request, ApiType apiType, string jsonBody = null, string
LoadPages = loadPages;
SkipCollectionClearing = false;
ExecuteRequestApiCall = false;
Headers = headers;
}

internal ApiCall(List<Core.CSOM.Requests.IRequest<object>> csomRequests, string receivingProperty = null)
internal ApiCall(List<Core.CSOM.Requests.IRequest<object>> csomRequests, string receivingProperty = null, Dictionary<string, string> headers=null)
{
Request = null;
Type = ApiType.CSOM;
Expand All @@ -51,6 +52,7 @@ internal ApiCall(List<Core.CSOM.Requests.IRequest<object>> csomRequests, string
LoadPages = false;
SkipCollectionClearing = false;
ExecuteRequestApiCall = false;
Headers = headers;
}

/// <summary>
Expand Down Expand Up @@ -146,5 +148,10 @@ internal ApiCall(List<Core.CSOM.Requests.IRequest<object>> csomRequests, string
/// Flag that indicates this ApiCall was issued from an ExecuteRequest method
/// </summary>
internal bool ExecuteRequestApiCall { get; set; }

/// <summary>
/// Http-Headers for Request
/// </summary>
internal Dictionary<string, string> Headers { get; set; }
}
}
12 changes: 10 additions & 2 deletions src/sdk/PnP.Core/Services/Core/ApiRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Collections.Generic;
using System.Net.Http;

namespace PnP.Core.Services
{
Expand All @@ -15,12 +16,14 @@ public class ApiRequest
/// <param name="type"><see cref="ApiRequestType"/> of the request</param>
/// <param name="request">Actual API call to issue</param>
/// <param name="body">Optional body of the request</param>
public ApiRequest(HttpMethod httpMethod, ApiRequestType type, string request, string body)
/// <param name="headers">Optional for the request</param>
public ApiRequest(HttpMethod httpMethod, ApiRequestType type, string request, string body, Dictionary<string,string>headers=null)
{
HttpMethod = httpMethod;
Type = type;
Request = request;
Body = body;
Headers = headers;
}

/// <summary>
Expand Down Expand Up @@ -51,5 +54,10 @@ public ApiRequest(ApiRequestType type, string request): this(HttpMethod.Get, typ
/// The optional payload/body of the API call to execute
/// </summary>
public string Body { get; set; }

/// <summary>
/// The optional headers of the API call to execute - for example IF-Match for PATCH Request
/// </summary>
public Dictionary<string, string> Headers { get; }
}
}
16 changes: 16 additions & 0 deletions src/sdk/PnP.Core/Services/Core/BatchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,22 @@ private Tuple<string, string> BuildMicrosoftGraphBatchRequestContent(Batch batch
};
};

if(request.ApiCall.Headers!=null)
{
foreach(var key in request.ApiCall.Headers.Keys)
{
string existingKey = graphRequest.Headers.Keys.FirstOrDefault(k => k.Equals(key, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrWhiteSpace(existingKey))
{
graphRequest.Headers.Add(key, request.ApiCall.Headers[key]);
}
else
{
graphRequest.Headers[existingKey] = request.ApiCall.Headers[key];
}
}
}

graphRequests.Requests.Add(graphRequest);

#if DEBUG
Expand Down

0 comments on commit 90b9d2a

Please sign in to comment.