Skip to content

Commit 6e42116

Browse files
Add new GregClient constructor to accept custom http client (#116)
* add new GregClient constructor to accept custom http client * Update src/GregClient/GregClient.cs Co-authored-by: Copilot <[email protected]> * refactor dup code into helper --------- Co-authored-by: Copilot <[email protected]>
1 parent d3ed839 commit 6e42116

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/GregClient/GregClient.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using RestSharp;
55
using System;
66
using System.Net;
7+
using System.Net.Http;
78

89
namespace Greg
910
{
@@ -17,10 +18,8 @@ public IAuthProvider AuthProvider
1718
get { return _authProvider; }
1819
}
1920

20-
public GregClient(IAuthProvider provider, string packageManagerUrl)
21+
private static void SetTLSHelper()
2122
{
22-
23-
2423
// https://stackoverflow.com/questions/2819934/detect-windows-version-in-net
2524
// if the current OS is windows 7 or lower
2625
// set TLS to 1.2.
@@ -29,10 +28,40 @@ public GregClient(IAuthProvider provider, string packageManagerUrl)
2928
{
3029
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
3130
}
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="GregClient"/> class with the specified authentication provider,
35+
/// and package manager URL.
36+
/// </summary>
37+
/// <param name="provider">The authentication provider used for managing authentication tokens.</param>
38+
/// <param name="packageManagerUrl">The base URL of the package manager service.</param>
39+
public GregClient(IAuthProvider provider, string packageManagerUrl)
40+
{
41+
SetTLSHelper();
3242
_authProvider = provider;
3343
_client = new RestClient(packageManagerUrl);
3444
}
3545

46+
/// <summary>
47+
/// Initializes a new instance of the <see cref="GregClient"/> class with the specified authentication provider,
48+
/// package manager URL, and optional HTTP client.
49+
/// </summary>
50+
/// <param name="provider">The authentication provider used for managing authentication tokens.</param>
51+
/// <param name="packageManagerUrl">The base URL of the package manager service.</param>
52+
/// <param name="httpClient">An optional HTTP client to use for making requests. If null, a default client is created.</param>
53+
public GregClient(IAuthProvider provider, string packageManagerUrl, HttpClient httpClient)
54+
{
55+
SetTLSHelper();
56+
_authProvider = provider;
57+
58+
var baseUrl = new Uri(packageManagerUrl);
59+
if (httpClient != null)
60+
_client = new RestClient(httpClient, new RestClientOptions() { BaseUrl = baseUrl });
61+
else
62+
_client = new RestClient(packageManagerUrl);
63+
}
64+
3665
private RestResponse ExecuteInternal(Request m)
3766
{
3867

0 commit comments

Comments
 (0)