Skip to content

Commit ec8057e

Browse files
add new GregClient constructor to accept custom http client
1 parent d3ed839 commit ec8057e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/GregClient/GregClient.cs

Lines changed: 20 additions & 2 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
{
@@ -19,8 +20,6 @@ public IAuthProvider AuthProvider
1920

2021
public GregClient(IAuthProvider provider, string packageManagerUrl)
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.
@@ -33,6 +32,25 @@ public GregClient(IAuthProvider provider, string packageManagerUrl)
3332
_client = new RestClient(packageManagerUrl);
3433
}
3534

35+
public GregClient(IAuthProvider provider, string packageManagerUrl, HttpClient httpClient)
36+
{
37+
// https://stackoverflow.com/questions/2819934/detect-windows-version-in-net
38+
// if the current OS is windows 7 or lower
39+
// set TLS to 1.2.
40+
// else do nothing and let the OS decide the version of TLS to support. (.net 4.7 required)
41+
if (System.Environment.OSVersion.Version.Major <= 6 && System.Environment.OSVersion.Version.Minor <= 1)
42+
{
43+
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
44+
}
45+
_authProvider = provider;
46+
47+
var baseUrl = new Uri(packageManagerUrl);
48+
if (httpClient != null)
49+
_client = new RestClient(httpClient, new RestClientOptions() { BaseUrl = baseUrl });
50+
else
51+
_client = new RestClient(packageManagerUrl);
52+
}
53+
3654
private RestResponse ExecuteInternal(Request m)
3755
{
3856

0 commit comments

Comments
 (0)