From ec8057e825f6b5706994470bc8594d6fa0e52842 Mon Sep 17 00:00:00 2001 From: Aparajit Pratap Date: Wed, 21 May 2025 22:42:55 -0400 Subject: [PATCH 1/3] add new GregClient constructor to accept custom http client --- src/GregClient/GregClient.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/GregClient/GregClient.cs b/src/GregClient/GregClient.cs index 32ec3f5..f223565 100644 --- a/src/GregClient/GregClient.cs +++ b/src/GregClient/GregClient.cs @@ -4,6 +4,7 @@ using RestSharp; using System; using System.Net; +using System.Net.Http; namespace Greg { @@ -19,8 +20,6 @@ public IAuthProvider AuthProvider public GregClient(IAuthProvider provider, string packageManagerUrl) { - - // https://stackoverflow.com/questions/2819934/detect-windows-version-in-net // if the current OS is windows 7 or lower // set TLS to 1.2. @@ -33,6 +32,25 @@ public GregClient(IAuthProvider provider, string packageManagerUrl) _client = new RestClient(packageManagerUrl); } + public GregClient(IAuthProvider provider, string packageManagerUrl, HttpClient httpClient) + { + // https://stackoverflow.com/questions/2819934/detect-windows-version-in-net + // if the current OS is windows 7 or lower + // set TLS to 1.2. + // else do nothing and let the OS decide the version of TLS to support. (.net 4.7 required) + if (System.Environment.OSVersion.Version.Major <= 6 && System.Environment.OSVersion.Version.Minor <= 1) + { + ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; + } + _authProvider = provider; + + var baseUrl = new Uri(packageManagerUrl); + if (httpClient != null) + _client = new RestClient(httpClient, new RestClientOptions() { BaseUrl = baseUrl }); + else + _client = new RestClient(packageManagerUrl); + } + private RestResponse ExecuteInternal(Request m) { From 31641ae2e9face312c16a0c56c0eeb25970a06bb Mon Sep 17 00:00:00 2001 From: aparajit-pratap Date: Thu, 22 May 2025 09:53:20 -0400 Subject: [PATCH 2/3] Update src/GregClient/GregClient.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/GregClient/GregClient.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/GregClient/GregClient.cs b/src/GregClient/GregClient.cs index f223565..0fcd5c4 100644 --- a/src/GregClient/GregClient.cs +++ b/src/GregClient/GregClient.cs @@ -32,6 +32,13 @@ public GregClient(IAuthProvider provider, string packageManagerUrl) _client = new RestClient(packageManagerUrl); } + /// + /// Initializes a new instance of the class with the specified authentication provider, + /// package manager URL, and optional HTTP client. + /// + /// The authentication provider used for managing authentication tokens. + /// The base URL of the package manager service. + /// An optional HTTP client to use for making requests. If null, a default client is created. public GregClient(IAuthProvider provider, string packageManagerUrl, HttpClient httpClient) { // https://stackoverflow.com/questions/2819934/detect-windows-version-in-net From 8ffc5d952c56508e53715598c6f4e8805217eaff Mon Sep 17 00:00:00 2001 From: Aparajit Pratap Date: Thu, 22 May 2025 10:36:31 -0400 Subject: [PATCH 3/3] refactor dup code into helper --- src/GregClient/GregClient.cs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/GregClient/GregClient.cs b/src/GregClient/GregClient.cs index 0fcd5c4..26f19af 100644 --- a/src/GregClient/GregClient.cs +++ b/src/GregClient/GregClient.cs @@ -18,7 +18,7 @@ public IAuthProvider AuthProvider get { return _authProvider; } } - public GregClient(IAuthProvider provider, string packageManagerUrl) + private static void SetTLSHelper() { // https://stackoverflow.com/questions/2819934/detect-windows-version-in-net // if the current OS is windows 7 or lower @@ -28,6 +28,17 @@ public GregClient(IAuthProvider provider, string packageManagerUrl) { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; } + } + + /// + /// Initializes a new instance of the class with the specified authentication provider, + /// and package manager URL. + /// + /// The authentication provider used for managing authentication tokens. + /// The base URL of the package manager service. + public GregClient(IAuthProvider provider, string packageManagerUrl) + { + SetTLSHelper(); _authProvider = provider; _client = new RestClient(packageManagerUrl); } @@ -41,14 +52,7 @@ public GregClient(IAuthProvider provider, string packageManagerUrl) /// An optional HTTP client to use for making requests. If null, a default client is created. public GregClient(IAuthProvider provider, string packageManagerUrl, HttpClient httpClient) { - // https://stackoverflow.com/questions/2819934/detect-windows-version-in-net - // if the current OS is windows 7 or lower - // set TLS to 1.2. - // else do nothing and let the OS decide the version of TLS to support. (.net 4.7 required) - if (System.Environment.OSVersion.Version.Major <= 6 && System.Environment.OSVersion.Version.Minor <= 1) - { - ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; - } + SetTLSHelper(); _authProvider = provider; var baseUrl = new Uri(packageManagerUrl);