From f8a8d50ce8c372a49f8de86e6e8b876e5c4c52f0 Mon Sep 17 00:00:00 2001 From: Christian Zuellig Date: Thu, 20 May 2021 23:19:18 +0200 Subject: [PATCH 1/2] Allow to set headers to APIRequest for PATCH if needs IF-MATCH for BuildSharePointRestBatchRequestContent the IF-MATCH is general set to * --- src/sdk/PnP.Core/Model/Base/BaseDataModel.cs | 3 ++- src/sdk/PnP.Core/Services/Core/ApiCall.cs | 11 +++++++++-- src/sdk/PnP.Core/Services/Core/ApiRequest.cs | 12 ++++++++++-- src/sdk/PnP.Core/Services/Core/BatchClient.cs | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/sdk/PnP.Core/Model/Base/BaseDataModel.cs b/src/sdk/PnP.Core/Model/Base/BaseDataModel.cs index cb1bd956f4..6916a26a32 100644 --- a/src/sdk/PnP.Core/Model/Base/BaseDataModel.cs +++ b/src/sdk/PnP.Core/Model/Base/BaseDataModel.cs @@ -211,7 +211,8 @@ public async Task ExecuteRequestAsync(ApiRequest request) { ExecuteRequestApiCall = true, SkipCollectionClearing = true, - RawRequest = true + RawRequest = true, + Headers= request.Headers } , request.HttpMethod).ConfigureAwait(false); diff --git a/src/sdk/PnP.Core/Services/Core/ApiCall.cs b/src/sdk/PnP.Core/Services/Core/ApiCall.cs index c45f5aff4a..8132e90dea 100644 --- a/src/sdk/PnP.Core/Services/Core/ApiCall.cs +++ b/src/sdk/PnP.Core/Services/Core/ApiCall.cs @@ -9,7 +9,7 @@ namespace PnP.Core.Services /// 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 headers = null) { Type = apiType; Request = request; @@ -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> csomRequests, string receivingProperty = null) + internal ApiCall(List> csomRequests, string receivingProperty = null, Dictionary headers=null) { Request = null; Type = ApiType.CSOM; @@ -51,6 +52,7 @@ internal ApiCall(List> csomRequests, string LoadPages = false; SkipCollectionClearing = false; ExecuteRequestApiCall = false; + Headers = headers; } /// @@ -146,5 +148,10 @@ internal ApiCall(List> csomRequests, string /// Flag that indicates this ApiCall was issued from an ExecuteRequest method /// internal bool ExecuteRequestApiCall { get; set; } + + /// + /// Http-Headers for Request + /// + internal Dictionary Headers { get; set; } } } diff --git a/src/sdk/PnP.Core/Services/Core/ApiRequest.cs b/src/sdk/PnP.Core/Services/Core/ApiRequest.cs index 6ecd3c08d9..472ac4acd6 100644 --- a/src/sdk/PnP.Core/Services/Core/ApiRequest.cs +++ b/src/sdk/PnP.Core/Services/Core/ApiRequest.cs @@ -1,4 +1,5 @@ -using System.Net.Http; +using System.Collections.Generic; +using System.Net.Http; namespace PnP.Core.Services { @@ -15,12 +16,14 @@ public class ApiRequest /// of the request /// Actual API call to issue /// Optional body of the request - public ApiRequest(HttpMethod httpMethod, ApiRequestType type, string request, string body) + /// Optional for the request + public ApiRequest(HttpMethod httpMethod, ApiRequestType type, string request, string body, Dictionaryheaders=null) { HttpMethod = httpMethod; Type = type; Request = request; Body = body; + Headers = headers; } /// @@ -51,5 +54,10 @@ public ApiRequest(ApiRequestType type, string request): this(HttpMethod.Get, typ /// The optional payload/body of the API call to execute /// public string Body { get; set; } + + /// + /// The optional headers of the API call to execute - for example IF-Match for PATCH Request + /// + public Dictionary Headers { get; } } } diff --git a/src/sdk/PnP.Core/Services/Core/BatchClient.cs b/src/sdk/PnP.Core/Services/Core/BatchClient.cs index f0fa3d9bd0..ebf80fefcb 100644 --- a/src/sdk/PnP.Core/Services/Core/BatchClient.cs +++ b/src/sdk/PnP.Core/Services/Core/BatchClient.cs @@ -771,6 +771,21 @@ private Tuple BuildMicrosoftGraphBatchRequestContent(Batch batch }; }; + if(request.ApiCall.Headers!=null) + { + foreach(var key in request.ApiCall.Headers.Keys) + { + if(!graphRequest.Headers.ContainsKey(key)) + { + graphRequest.Headers.Add(key, request.ApiCall.Headers[key]); + } + else + { + graphRequest.Headers[key] = request.ApiCall.Headers[key]; + } + } + } + graphRequests.Requests.Add(graphRequest); #if DEBUG From bd26e899640d65df9133906acb0edef2e881fc43 Mon Sep 17 00:00:00 2001 From: Christian Zuellig Date: Fri, 21 May 2021 07:37:00 +0200 Subject: [PATCH 2/2] respect that http header keys are most of the times not case sensitiv for compare with existing --- src/sdk/PnP.Core/Services/Core/BatchClient.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sdk/PnP.Core/Services/Core/BatchClient.cs b/src/sdk/PnP.Core/Services/Core/BatchClient.cs index ebf80fefcb..77604a2ef7 100644 --- a/src/sdk/PnP.Core/Services/Core/BatchClient.cs +++ b/src/sdk/PnP.Core/Services/Core/BatchClient.cs @@ -775,13 +775,14 @@ private Tuple BuildMicrosoftGraphBatchRequestContent(Batch batch { foreach(var key in request.ApiCall.Headers.Keys) { - if(!graphRequest.Headers.ContainsKey(key)) + 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[key] = request.ApiCall.Headers[key]; + graphRequest.Headers[existingKey] = request.ApiCall.Headers[key]; } } }