Skip to content

Commit

Permalink
merge of #459. Thanks @czullu
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenbe committed May 22, 2021
1 parent 90b9d2a commit 41e3a99
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Added

- Added support for adding custom HTTP headers when executing a custom apirequest via ExecuteRequest #459 [czullu - Christian Zuellig]
- Added support to load an IFile directly off of an IListItem #463 [DaleyKD - Kyle Daley]
- Added missing properties to IListItem and GetDisplayName methods #452 [DaleyKD - Kyle Daley]
- Added missing properties to IFile #445 [DaleyKD - Kyle Daley]
Expand Down
23 changes: 23 additions & 0 deletions src/sdk/PnP.Core.Test/Base/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Text;
using System.Threading.Tasks;
using PnP.Core.QueryModel;
using System.Collections.Generic;
using System.Net.Http;

namespace PnP.Core.Test.Base
{
Expand Down Expand Up @@ -1118,6 +1120,27 @@ public async Task ExecuteGraphRequest()
}
}

[TestMethod]
public async Task ExecuteGraphRequestWithCustomHeaders()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
Dictionary<string, string> extraHeaders = new()
{
{ "random", "random" },
{ "random2", "random2" }
};

var apiRequest = new ApiRequest(HttpMethod.Get, ApiRequestType.Graph, "me", null, extraHeaders);
var response = context.Team.ExecuteRequest(apiRequest);

Assert.IsTrue(response.ApiRequest == apiRequest);
Assert.IsTrue(!string.IsNullOrEmpty(response.Response));
Assert.IsTrue(response.StatusCode == System.Net.HttpStatusCode.OK);
}
}

#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/sdk/PnP.Core/Model/Base/BaseDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public async Task<ApiRequestResponse> ExecuteRequestAsync(ApiRequest request)
ExecuteRequestApiCall = true,
SkipCollectionClearing = true,
RawRequest = true,
Headers= request.Headers
Headers = request.Headers
}
, request.HttpMethod).ConfigureAwait(false);

Expand Down
10 changes: 5 additions & 5 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, Dictionary<string, string> headers = null)
internal ApiCall(string request, ApiType apiType, string jsonBody = null, string receivingProperty = null, bool loadPages = false)
{
Type = apiType;
Request = request;
Expand All @@ -29,10 +29,10 @@ internal ApiCall(string request, ApiType apiType, string jsonBody = null, string
LoadPages = loadPages;
SkipCollectionClearing = false;
ExecuteRequestApiCall = false;
Headers = headers;
Headers = null;
}

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

/// <summary>
Expand Down Expand Up @@ -150,7 +150,7 @@ internal ApiCall(List<Core.CSOM.Requests.IRequest<object>> csomRequests, string
internal bool ExecuteRequestApiCall { get; set; }

/// <summary>
/// Http-Headers for Request
/// Optional Http-Headers for Request to make
/// </summary>
internal Dictionary<string, string> Headers { get; set; }
}
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/PnP.Core/Services/Core/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ 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>
/// <param name="headers">Optional for the request</param>
public ApiRequest(HttpMethod httpMethod, ApiRequestType type, string request, string body, Dictionary<string,string>headers=null)
/// <param name="headers">Optional headers for the request</param>
public ApiRequest(HttpMethod httpMethod, ApiRequestType type, string request, string body, Dictionary<string, string> headers = null)
{
HttpMethod = httpMethod;
Type = type;
Expand Down Expand Up @@ -56,7 +56,7 @@ public ApiRequest(ApiRequestType type, string request): this(HttpMethod.Get, typ
public string Body { get; set; }

/// <summary>
/// The optional headers of the API call to execute - for example IF-Match for PATCH Request
/// The optional headers of the API call to execute - for example IF-Match for PATCH Request. Currently only supported for Graph calls.
/// </summary>
public Dictionary<string, string> Headers { get; }
}
Expand Down
15 changes: 10 additions & 5 deletions src/sdk/PnP.Core/Services/Core/BatchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -766,15 +766,20 @@ private Tuple<string, string> BuildMicrosoftGraphBatchRequestContent(Batch batch
bodiesToReplace.Add(counter, request.ApiCall.JsonBody);
graphRequest.Body = $"@#|Body{counter}|#@";
graphRequest.Headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json" }
};
{
{ "Content-Type", "application/json" }
};
};

if(request.ApiCall.Headers!=null)
if (request.ApiCall.Headers != null && request.ApiCall.Headers.Count > 0)
{
foreach(var key in request.ApiCall.Headers.Keys)
if (graphRequest.Headers == null)
{
graphRequest.Headers = new Dictionary<string, string>();
}

foreach (var key in request.ApiCall.Headers.Keys)
{
string existingKey = graphRequest.Headers.Keys.FirstOrDefault(k => k.Equals(key, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrWhiteSpace(existingKey))
{
Expand Down

0 comments on commit 41e3a99

Please sign in to comment.