Skip to content

Commit afee564

Browse files
authored
Merge pull request #47 from deadmann/feature/46-securityprotocoltype
Feature/46 securityprotocoltype
2 parents 30bfafd + 2896da6 commit afee564

14 files changed

+164
-36
lines changed

AdvancedRestHandler.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.29806.167
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Docs", "_Docs", "{7EDD1602-7EC9-4D23-A7CE-4762973CC1F3}"
77
ProjectSection(SolutionItems) = preProject
8-
_Docs\push nuget github package.txt = _Docs\push nuget github package.txt
8+
_Docs\push nuget github package.md = _Docs\push nuget github package.md
99
EndProjectSection
1010
EndProject
1111
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdvancedRestHandler", "AdvancedRestHandler\AdvancedRestHandler.csproj", "{0B5EB03D-57EA-4762-B11E-0B886A1C51E2}"

AdvancedRestHandler/AdvancedRestHandler.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Net;
99
using System.Net.Http;
1010
using System.Reflection;
11+
using System.Security.Authentication;
1112
using System.Text;
1213
using System.Threading;
1314
using System.Threading.Tasks;
@@ -38,6 +39,11 @@ public class AdvancedRestHandler
3839
/// If set, will be used globally for requests
3940
/// </summary>
4041
public TimeSpan? GlobalTimeout { get; set; }
42+
43+
/// <summary>
44+
/// If set, will be used globally for requests
45+
/// </summary>
46+
public SslProtocols? GlobalSslProtocols { get; set; }
4147

4248
#region Initialization
4349

@@ -125,8 +131,9 @@ private void SetGlobalConfigurationVariables(RestHandlerInitializerOptions optio
125131
}
126132

127133
GlobalTimeout = options.Timeout;
134+
GlobalSslProtocols = options.SslProtocols;
128135
}
129-
136+
130137
#endregion Initialization
131138

132139
#region GET
@@ -1906,14 +1913,14 @@ public async Task<TResponse> DeleteDataAsync<TResponse, TRequest>(string partial
19061913
/// <returns></returns>
19071914
private HttpClient GetAndConfigureHttpClient(RestHandlerRequestOptions requestOptions)
19081915
{
1909-
HttpClient httpClient = GetHttpClient();
1916+
HttpClient httpClient = GetHttpClient(requestOptions);
19101917

19111918
ConfigureHttpClient(httpClient, requestOptions);
19121919

19131920
return httpClient;
19141921
}
19151922

1916-
private HttpClient GetHttpClient()
1923+
private HttpClient GetHttpClient(RestHandlerRequestOptions requestOptions)
19171924
{
19181925
switch (_arhHttpClientType)
19191926
{
@@ -1927,28 +1934,42 @@ private HttpClient GetHttpClient()
19271934
return _httpClient;
19281935
case ArhHttpClientType.Manual:
19291936
default:
1930-
return MakeNewHttpClient();
1937+
return MakeNewHttpClient(requestOptions);
19311938
}
19321939
}
19331940

1934-
private HttpClient MakeNewHttpClient()
1941+
private HttpClient MakeNewHttpClient(RestHandlerRequestOptions requestOptions)
19351942
{
1943+
HttpMessageHandler httpMessageHandler = MakeNewHttpClientHandler(requestOptions);
1944+
19361945
HttpClient httpClient;
19371946
if (!string.IsNullOrWhiteSpace(_baseUrl))
19381947
{
1939-
httpClient = new HttpClient
1948+
httpClient = new HttpClient(httpMessageHandler)
19401949
{
19411950
BaseAddress = new Uri(_baseUrl)
19421951
};
19431952
}
19441953
else
19451954
{
1946-
httpClient = new HttpClient();
1955+
httpClient = new HttpClient(httpMessageHandler);
19471956
}
19481957

19491958
return httpClient;
19501959
}
19511960

1961+
private HttpClientHandler MakeNewHttpClientHandler(RestHandlerRequestOptions requestOptions)
1962+
{
1963+
var result = new HttpClientHandler();
1964+
1965+
if (GlobalSslProtocols is not null)
1966+
result.SslProtocols = GlobalSslProtocols.Value;
1967+
if (requestOptions.SslProtocols is not null)
1968+
result.SslProtocols = requestOptions.SslProtocols.Value;
1969+
1970+
return result;
1971+
}
1972+
19521973
private void ConfigureHttpClient(HttpClient httpClient, RestHandlerRequestOptions requestOptions)
19531974
{
19541975
if (requestOptions.Timeout.HasValue)
@@ -2124,9 +2145,9 @@ private TResponse MakeResponse<TResponse>(string requestString, string responseS
21242145
{
21252146
fallbackResult = Convert.ChangeType(responseString, typeof(string));
21262147
}
2127-
else if (requestString.StartsWith("[") || responseString.StartsWith("{"))
2148+
else if (responseString!.StartsWith("[") || responseString!.StartsWith("{"))
21282149
{
2129-
fallbackResult = DeserializeToType(responseString, fallbackType);
2150+
fallbackResult = DeserializeToType(responseString!, fallbackType);
21302151
}
21312152

21322153
SetValueOnProperty(result, nameof(ArhResponse.FallbackModel), fallbackResult);
@@ -2257,7 +2278,7 @@ protected virtual object DeserializeToType(string jsonString, Type genericTypeAr
22572278
return typeof(JsonConvert)
22582279
.GetMethod(nameof(JsonConvert.DeserializeObject), new[] { typeof(string), typeof(Type) })
22592280
//?.MakeGenericMethod(genericTypeArgument)
2260-
.Invoke(null, new object[] { jsonString, genericTypeArgument });
2281+
!.Invoke(null, new object[] { jsonString, genericTypeArgument });
22612282
}
22622283

22632284
/// <summary>

AdvancedRestHandler/AdvancedRestHandler.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
99
<RootNamespace>Arh</RootNamespace>
10-
<AssemblyVersion>1.5.0.0</AssemblyVersion>
11-
<FileVersion>1.5.0.0</FileVersion>
12-
<Version>1.5.0</Version>
10+
<AssemblyVersion>1.6.0.0</AssemblyVersion>
11+
<FileVersion>1.6.0.0</FileVersion>
12+
<Version>1.6.0</Version>
1313
<RepositoryUrl>https://github.com/deadmann/AdvancedRestHandler</RepositoryUrl>
1414
<PackageReleaseNotes>
15+
1.6.0
16+
- Add support for using custom SSL/TSL protocols
1517
1.5.0
1618
- Supports external HttpClient
1719
- Supports IHttpClientFactory
@@ -35,7 +37,8 @@
3537
<Product>AdvancedRestHandler</Product>
3638
<PackageId>AdvancedRestHandler</PackageId>
3739
<AssemblyName>AdvancedRestHandler</AssemblyName>
38-
<PackageVersion>1.5.0</PackageVersion>
40+
<PackageVersion>1.6.0</PackageVersion>
41+
<LangVersion>11</LangVersion>
3942
</PropertyGroup>
4043

4144
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

AdvancedRestHandler/AdvancedRestHandler.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.ComponentModel;
2+
3+
namespace System.Runtime.CompilerServices
4+
{
5+
#if !NET5_0_OR_GREATER
6+
7+
[EditorBrowsable(EditorBrowsableState.Never)]
8+
internal static class IsExternalInit {}
9+
10+
#endif // !NET5_0_OR_GREATER
11+
12+
#if !NET7_0_OR_GREATER
13+
14+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
15+
internal sealed class RequiredMemberAttribute : Attribute {}
16+
17+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
18+
internal sealed class CompilerFeatureRequiredAttribute : Attribute
19+
{
20+
public CompilerFeatureRequiredAttribute(string featureName)
21+
{
22+
FeatureName = featureName;
23+
}
24+
25+
public string FeatureName { get; }
26+
public bool IsOptional { get; init; }
27+
28+
public const string RefStructs = nameof(RefStructs);
29+
public const string RequiredMembers = nameof(RequiredMembers);
30+
}
31+
32+
#endif // !NET7_0_OR_GREATER
33+
}
34+
35+
namespace System.Diagnostics.CodeAnalysis
36+
{
37+
#if !NET7_0_OR_GREATER
38+
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
39+
internal sealed class SetsRequiredMembersAttribute : Attribute {}
40+
#endif
41+
}

AdvancedRestHandler/RestHandlerInitializerOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Security.Authentication;
34

45
namespace Arh
56
{
@@ -26,5 +27,10 @@ public class RestHandlerInitializerOptions
2627
/// Set global Timeout of requests
2728
/// </summary>
2829
public TimeSpan Timeout { get; set; }
30+
31+
/// <summary>
32+
/// Set global SslProtocols
33+
/// </summary>
34+
public SslProtocols? SslProtocols { get; set; } = null;
2935
}
3036
}

AdvancedRestHandler/RestHandlerRequestOptions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
4+
using System.Security.Authentication;
45
using System.Text;
56

67
namespace Arh
@@ -11,6 +12,7 @@ namespace Arh
1112
[SuppressMessage("ReSharper", "RedundantDefaultMemberInitializer")]
1213
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")]
1314
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
15+
[SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")]
1416
public class RestHandlerRequestOptions
1517
{
1618
/// <summary>
@@ -37,7 +39,7 @@ public class RestHandlerRequestOptions
3739
/// <summary>
3840
/// If the request is not convert-able to the provided type, then we can try to convert it to this type instead
3941
/// </summary>
40-
public List<Type> FallbackModels { get; set; } = new List<Type>();
42+
public List<Type> FallbackModels { get; set; } = new();
4143

4244
/// <summary>
4345
/// Use another encoding for the content passed to StringContent Object used by JSON serializer
@@ -53,5 +55,10 @@ public class RestHandlerRequestOptions
5355
/// Remove default "; charset=utf-8" from Content-Type in header
5456
/// </summary>
5557
public bool OmitContentTypeCharSet { get; set; } = false;
58+
59+
/// <summary>
60+
/// Allow support for different version of SSL/TSL protocols
61+
/// </summary>
62+
public SslProtocols? SslProtocols { get; set; } = null;
5663
}
5764
}

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ A utility code that can be used to request Data from services...
33

44
This library is based on my experience and need of working in the past company, taking many services in. I could handle any request so far using it, and every time I felt it doesn't support my need, i tried to expand it.
55

6-
[![NuGet](https://img.shields.io/badge/nuget-v1.5.0-blue)](https://www.nuget.org/packages/AdvancedRestHandler/)
6+
[![NuGet](https://img.shields.io/badge/nuget-v1.6.0-blue)](https://www.nuget.org/packages/AdvancedRestHandler/)
77

88
First install the package using following commands, or download it manually from release section...
99

1010
```powershell
11-
Install-Package AdvancedRestHandler -Version 1.5.0
11+
Install-Package AdvancedRestHandler -Version 1.6.0
1212
```
1313
or
1414
```sh
15-
dotnet add package AdvancedRestHandler --version 1.5.0
15+
dotnet add package AdvancedRestHandler --version 1.6.0
1616
```
1717

1818
Then you can use the library.
@@ -31,13 +31,30 @@ in the above code, in the `AdvancedRestHandler` constructor:
3131
in the `GetData`:
3232

3333
- `TResponse` is the type of data you are receiving and the incoming json or (i'm not sure if i pars XML, but if supported XML) should be deserialized to
34-
- `url` depending on the existance of baseUrl can be either the full url to a service, or the versitile part of the path in the service url
34+
- `url` depending on the existence of baseUrl can be either the full url to a service, or the versitile part of the path in the service url
3535
- `options` are modifier that can affect the behaviour of the service
3636

3737
Also note that request can be:
3838

39-
- A premitive
39+
- A primitive
4040
- A Type
4141
- A Type inherited from ArhResponse
4242
- An object of type ArhResponse<[Your Type]>
4343
- An object of type ArhStringResponse
44+
45+
## Exceptions
46+
47+
-------------
48+
49+
There are option exceptions for external `HttpClient` and `IHttpClientFactory` that are consumed by ArhRestHandler.
50+
51+
### RestHandlerInitializerOptions:
52+
The Arh `RestHandlerInitializerOptions` is a model that provide options that you will set in the beginning of Arh life-cycle
53+
54+
- `FixEndOfUrl`: (Throw exceptions if set to true) This option fix the slash `/` at the end of your URLs, so you don't need to think about your partial url passed every time
55+
- `SslProtocols`: (Does nothing if is set) This option allow you to change your request SSL/TSL protocols
56+
57+
### RestHandlerRequestOptions:
58+
The Arh `RestHandlerRequestOptions` is a model that provide options per request of ARH
59+
60+
- `SslProtocols`: (Does nothing if is set) This option allow you to change your request SSL/TSL protocols

Tests/AdvancedRestHandler.Test_Net/AdvancedRestHandler.Test_Net.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<Nullable>enable</Nullable>
66

77
<IsPackable>false</IsPackable>
8+
9+
<LangVersion>11</LangVersion>
810
</PropertyGroup>
911

1012
<ItemGroup>

Tests/AdvancedRestHandler.Test_NetCore/AdvancedRestHandler.Test_NetCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<IsPackable>false</IsPackable>
77

88
<RootNamespace>AdvancedRestHandler.Tests</RootNamespace>
9+
10+
<LangVersion>11</LangVersion>
911
</PropertyGroup>
1012

1113
<ItemGroup>

Tests/AdvancedRestHandler.Test_NetFramework/AdvancedRestHandler.Test_NetFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<AssemblyName>AdvancedRestHandler.Test_NetFramework</AssemblyName>
1313
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1414
<FileAlignment>512</FileAlignment>
15+
<LangVersion>11</LangVersion>
1516
</PropertyGroup>
1617
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1718
<PlatformTarget>AnyCPU</PlatformTarget>

_Docs/push nuget github package.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- ### Step 1:
2+
- - Authenticate (if this is the first time) Note you must also pass --store-password-in-clear-text on non-Windows systems.
3+
```bash
4+
$ dotnet nuget add source https://nuget.pkg.github.com/deadmann/index.json -n github -u deadmann -p GH_TOKEN [--store-password-in-clear-text]
5+
```
6+
- - if already exists....
7+
8+
```bash
9+
$ dotnet nuget remove source github
10+
```
11+
- - // if key update is required: https://github.com/settings/tokens
12+
13+
- ### Step 2: Build
14+
```bash
15+
$ dotnet build --configuration Release
16+
```
17+
18+
- ### Step 3: Pack
19+
```bash
20+
$ dotnet pack --configuration Release
21+
```
22+
23+
- ### Step 4: Publish
24+
```bash
25+
$ dotnet nuget push "bin/Release/AdvancedRestHandler.1.0.0.nupkg" --source "github"
26+
```
27+
// OR (Based on nuget.config Configuration)
28+
```bash
29+
$ dotnet nuget push "bin/Release/AdvancedRestHandler.1.6.0.nupkg" --source "github" --api-key=[GH_AccessToken]
30+
```
31+
// OR (With %Key%)
32+
```bash
33+
set GITHUB_USERNAME=your_username
34+
set GITHUB_TOKEN=your_token
35+
$ dotnet nuget push "bin/Release/AdvancedRestHandler.1.6.0.nupkg" --source "github"
36+
```

_Docs/push nuget github package.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)