From 6fd304a9d1f224d0403bb190c540671561b63098 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Fri, 30 Dec 2022 22:56:27 +0200 Subject: [PATCH 1/3] Feature: Added support for HTTP/2 to improve perf --- src/Commands/AzureAD/RegisterManagementShellAccess.cs | 1 + src/Commands/Base/InvokeSPRestMethod.cs | 7 +------ src/Commands/Base/TokenHandling.cs | 1 + src/Commands/PowerPlatform/PowerAutomate/ExportFlow.cs | 2 ++ src/Commands/Utilities/BrowserHelper.cs | 1 + src/Commands/Utilities/REST/GraphHelper.cs | 1 + src/Commands/Utilities/REST/RestHelper.cs | 10 ++++++---- src/Commands/Utilities/VersionChecker.cs | 5 +++-- 8 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Commands/AzureAD/RegisterManagementShellAccess.cs b/src/Commands/AzureAD/RegisterManagementShellAccess.cs index 621d3114b..2886fe43f 100644 --- a/src/Commands/AzureAD/RegisterManagementShellAccess.cs +++ b/src/Commands/AzureAD/RegisterManagementShellAccess.cs @@ -102,6 +102,7 @@ protected override void ProcessRecord() var httpClient = Framework.Http.PnPHttpClient.Instance.GetHttpClient(); using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"https://{GetGraphEndPoint()}/v1.0/organization")) { + requestMessage.Version = new System.Version(2, 0); requestMessage.Headers.Add("Authorization", $"Bearer {accessToken}"); requestMessage.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var response = httpClient.SendAsync(requestMessage).GetAwaiter().GetResult(); diff --git a/src/Commands/Base/InvokeSPRestMethod.cs b/src/Commands/Base/InvokeSPRestMethod.cs index bfae4b980..6f4459080 100644 --- a/src/Commands/Base/InvokeSPRestMethod.cs +++ b/src/Commands/Base/InvokeSPRestMethod.cs @@ -1,17 +1,11 @@ using Microsoft.SharePoint.Client; - using PnP.Framework.Http; using PnP.Framework.Utilities; - using PnP.PowerShell.Commands.Enums; -using PnP.PowerShell.Commands.Utilities.JSON; - using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Management.Automation; -using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Runtime.InteropServices; @@ -84,6 +78,7 @@ protected override void ExecuteCmdlet() { request.Headers.Add("IF-MATCH", "*"); } + request.Version = new Version(2, 0); PnPHttpClient.AuthenticateRequestAsync(request, ClientContext).GetAwaiter().GetResult(); diff --git a/src/Commands/Base/TokenHandling.cs b/src/Commands/Base/TokenHandling.cs index 2ebe31cb1..4eacc40d7 100644 --- a/src/Commands/Base/TokenHandling.cs +++ b/src/Commands/Base/TokenHandling.cs @@ -148,6 +148,7 @@ internal static async Task GetManagedIdentityTokenAsync(Cmdlet cmdlet, H using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, tokenRequestUrl)) { + requestMessage.Version = new Version(2, 0); requestMessage.Headers.Add("Metadata", "true"); if (!string.IsNullOrEmpty(identityHeader)) { diff --git a/src/Commands/PowerPlatform/PowerAutomate/ExportFlow.cs b/src/Commands/PowerPlatform/PowerAutomate/ExportFlow.cs index 0148a3f93..393342dad 100644 --- a/src/Commands/PowerPlatform/PowerAutomate/ExportFlow.cs +++ b/src/Commands/PowerPlatform/PowerAutomate/ExportFlow.cs @@ -2,6 +2,7 @@ using PnP.PowerShell.Commands.Base; using PnP.PowerShell.Commands.Base.PipeBinds; using PnP.PowerShell.Commands.Utilities.REST; +using System; using System.Management.Automation; using System.Net.Http; using System.Text.Json; @@ -122,6 +123,7 @@ protected override void ExecuteCmdlet() var packageLink = valueElement.GetString(); using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, packageLink)) { + requestMessage.Version = new Version(2, 0); //requestMessage.Headers.Add("Authorization", $"Bearer {AccessToken}"); var response = Connection.HttpClient.SendAsync(requestMessage).GetAwaiter().GetResult(); var byteArray = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult(); diff --git a/src/Commands/Utilities/BrowserHelper.cs b/src/Commands/Utilities/BrowserHelper.cs index 816af621f..968789770 100644 --- a/src/Commands/Utilities/BrowserHelper.cs +++ b/src/Commands/Utilities/BrowserHelper.cs @@ -277,6 +277,7 @@ internal static bool GetWebBrowserPopup(string siteUrl, string title, (string ur string requestUrl = string.Format("{0}/_api/contextinfo", siteUrl.TrimEnd('/')); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); + request.Version = new Version(2, 0); request.Headers.Add("accept", "application/json;odata=nometadata"); HttpResponseMessage response = await httpClient.SendAsync(request); diff --git a/src/Commands/Utilities/REST/GraphHelper.cs b/src/Commands/Utilities/REST/GraphHelper.cs index 36789c25e..26a612b0c 100644 --- a/src/Commands/Utilities/REST/GraphHelper.cs +++ b/src/Commands/Utilities/REST/GraphHelper.cs @@ -55,6 +55,7 @@ private static HttpRequestMessage GetMessage(string url, HttpMethod method, PnPC } var message = new HttpRequestMessage(); + message.Version = new Version(2, 0); message.Method = method; message.Headers.TryAddWithoutValidation("Accept", "application/json"); message.RequestUri = !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? new Uri($"https://{connection.GraphEndPoint}/{url}") : new Uri(url); diff --git a/src/Commands/Utilities/REST/RestHelper.cs b/src/Commands/Utilities/REST/RestHelper.cs index bfe71a9fe..5fcc0aaf9 100644 --- a/src/Commands/Utilities/REST/RestHelper.cs +++ b/src/Commands/Utilities/REST/RestHelper.cs @@ -255,7 +255,7 @@ public static async Task PostAsync(HttpClient httpClient, string url, Cl public static async Task PostAsync(HttpClient httpClient, string url, string accessToken, object payload, string accept = "application/json") { - HttpRequestMessage message = null; + HttpRequestMessage message = null; if (payload != null) { #if NETFRAMEWORK @@ -276,7 +276,7 @@ public static async Task PostAsync(HttpClient httpClient, string url, st public static async Task PostAsync(HttpClient httpClient, string url, ClientContext clientContext, object payload, string accept = "application/json") { - HttpRequestMessage message = null; + HttpRequestMessage message = null; if (payload != null) { #if NETFRAMEWORK @@ -381,7 +381,7 @@ public static async Task PatchAsync(HttpClient httpClient, string url, str public static async Task PatchAsync(HttpClient httpClient, string url, string accessToken, object payload, string accept = "application/json") { - HttpRequestMessage message = null; + HttpRequestMessage message = null; if (payload != null) { #if NETFRAMEWORK @@ -616,7 +616,7 @@ private static HttpResponseMessage ExecuteDeleteRequestInternal(ClientContext co url += $"?{string.Join("&", restparams)}"; } - var client = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient(); + var client = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", context.GetAccessToken()); @@ -642,6 +642,7 @@ private static HttpRequestMessage GetMessage(string url, HttpMethod method, stri } var message = new HttpRequestMessage(); + message.Version = new Version(2, 0); message.Method = method; message.RequestUri = new Uri(url); if (!string.IsNullOrEmpty(accessToken)) @@ -665,6 +666,7 @@ private static HttpRequestMessage GetMessage(string url, HttpMethod method, Clie } var message = new HttpRequestMessage(); + message.Version = new Version(2, 0); message.Method = method; message.RequestUri = new Uri(url); message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(accept)); diff --git a/src/Commands/Utilities/VersionChecker.cs b/src/Commands/Utilities/VersionChecker.cs index ef9dd94e3..185773068 100644 --- a/src/Commands/Utilities/VersionChecker.cs +++ b/src/Commands/Utilities/VersionChecker.cs @@ -132,8 +132,9 @@ internal static string GetAvailableVersion(bool isNightly) // Deliberately lowering timeout as the version check is not critical so in case of a slower or blocked internet connection, this should not block the cmdlet for too long httpClient.Timeout = TimeSpan.FromSeconds(VersionCheckTimeOut); - - var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, isNightly ? NightlyVersionCheckUrl : ReleaseVersionCheckUrl)).GetAwaiter().GetResult(); + var request = new HttpRequestMessage(HttpMethod.Get, isNightly ? NightlyVersionCheckUrl : ReleaseVersionCheckUrl); + request.Version = new Version(2, 0); + var response = httpClient.SendAsync(request).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { var onlineVersion = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); From 138a80ed2e5e123944574aadf42adae7118cfbb9 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Fri, 30 Dec 2022 23:01:14 +0200 Subject: [PATCH 2/3] Remove whitespace --- src/Commands/Utilities/REST/RestHelper.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/Utilities/REST/RestHelper.cs b/src/Commands/Utilities/REST/RestHelper.cs index 5fcc0aaf9..6bc0991ea 100644 --- a/src/Commands/Utilities/REST/RestHelper.cs +++ b/src/Commands/Utilities/REST/RestHelper.cs @@ -255,7 +255,7 @@ public static async Task PostAsync(HttpClient httpClient, string url, Cl public static async Task PostAsync(HttpClient httpClient, string url, string accessToken, object payload, string accept = "application/json") { - HttpRequestMessage message = null; + HttpRequestMessage message = null; if (payload != null) { #if NETFRAMEWORK @@ -276,7 +276,7 @@ public static async Task PostAsync(HttpClient httpClient, string url, st public static async Task PostAsync(HttpClient httpClient, string url, ClientContext clientContext, object payload, string accept = "application/json") { - HttpRequestMessage message = null; + HttpRequestMessage message = null; if (payload != null) { #if NETFRAMEWORK @@ -616,7 +616,7 @@ private static HttpResponseMessage ExecuteDeleteRequestInternal(ClientContext co url += $"?{string.Join("&", restparams)}"; } - var client = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient(); + var client = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", context.GetAccessToken()); From 7669a82cd6253e74801143ee060ec201013bf669 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Fri, 30 Dec 2022 23:03:51 +0200 Subject: [PATCH 3/3] Added changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9be1c5f94..5ca07c2c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `-ShowInFiltersPane` to `Set-PnPField` which allows fields to be shown or hidden in the filters pane [#2623](https://github.com/pnp/powershell/pull/2632) - Added `-KeyColumn` to `Add-PnPDataRowsToSiteTemplate` which allows for overwriting existing list items in a site template [#2616](https://github.com/pnp/powershell/pull/2616) - Added `IsTeamsConnected`, `IsTeamsChannelConnected` and `TeamChannelType` to be returned when `Get-PnPTenantSite` cmdlet is executed. [#2656](https://github.com/pnp/powershell/pull/2656) +- Added `HTTP/2` support for all HTTP requests to improve performance for HTTP requests. [#2687](https://github.com/pnp/powershell/pull/2687) ### Changed