-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name-template: 'v$RESOLVED_VERSION' | ||
tag-template: 'v$RESOLVED_VERSION' | ||
categories: | ||
- title: '🚀 Features' | ||
labels: | ||
- 'feature' | ||
- 'enhancement' | ||
- title: '🐛 Bug Fixes' | ||
labels: | ||
- 'fix' | ||
- 'bugfix' | ||
- 'bug' | ||
- 'hotfix' | ||
- 'invalid' | ||
- title: '🧰 Maintenance' | ||
labels: | ||
- 'edits' | ||
- 'chore' | ||
- 'refactoring' | ||
change-template: '- $TITLE @$AUTHOR (#$NUMBER)' | ||
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. | ||
version-resolver: | ||
major: | ||
labels: | ||
- 'major' | ||
minor: | ||
labels: | ||
- 'minor' | ||
- 'feature' | ||
patch: | ||
labels: | ||
- 'patch' | ||
- 'bug' | ||
- 'bugfix' | ||
- 'hotfix' | ||
- 'fix' | ||
default: patch | ||
template: | | ||
## Changes | ||
$CHANGES |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Release Drafter | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
types: [opened, reopened, synchronize] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
update_release_draft: | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: release-drafter/release-drafter@v5 | ||
with: | ||
config-name: release-drafter.yml | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.10.35027.167 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{43F47EAD-C0B5-4EB7-8B05-B722629743DC}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClient.Core", "src\HttpClient.Core\HttpClient.Core.csproj", "{536A2A51-FBBA-4240-B7EE-2986787453FE}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{536A2A51-FBBA-4240-B7EE-2986787453FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{536A2A51-FBBA-4240-B7EE-2986787453FE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{536A2A51-FBBA-4240-B7EE-2986787453FE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{536A2A51-FBBA-4240-B7EE-2986787453FE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{536A2A51-FBBA-4240-B7EE-2986787453FE} = {43F47EAD-C0B5-4EB7-8B05-B722629743DC} | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {B53CA9C3-ADD2-43D1-BFE6-9D1C7B898CA9} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using HttpClient.Core.Web.Http.Extensions; | ||
using Newtonsoft.Json; | ||
using System.Net; | ||
|
||
namespace HttpClient.Core.Web.Http | ||
{ | ||
public class ApiResponse<TResult> | ||
{ | ||
[JsonProperty("isError")] | ||
public bool IsError => Code.IsErrorStatusCode() || Exception != null; | ||
|
||
[JsonProperty("message")] | ||
public string? Message { get; } | ||
|
||
[JsonProperty("result")] | ||
public TResult? Result { get; } | ||
|
||
[JsonProperty("code")] | ||
public HttpStatusCode? Code { get; } | ||
|
||
[JsonProperty("exception")] | ||
public Exception? Exception { get; } | ||
|
||
[JsonConstructor] | ||
public ApiResponse() | ||
{ | ||
Message = null; | ||
Result = default(TResult); | ||
Code = null; | ||
Exception = null; | ||
} | ||
|
||
public ApiResponse(TResult? result, HttpStatusCode? code, string? message, | ||
Exception? exception = null) | ||
{ | ||
Message = message; | ||
Code = code; | ||
Result = result; | ||
Exception = exception; | ||
} | ||
|
||
public ApiResponse(TResult? result, HttpStatusCode? code, Exception? exception = null) : | ||
this(result, code, code?.ToString(), exception) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using HttpClient.Core.Web.Http.Builders; | ||
using HttpClient.Core.Web.Http.Extensions; | ||
using HttpClient.Core.Web.Http.RequestHandlers; | ||
|
||
namespace HttpClient.Core.Web.Http | ||
{ | ||
public class BaseHttpClient | ||
{ | ||
private readonly System.Net.Http.HttpClient _client; | ||
|
||
protected virtual IRequestHandler RequestHandler => new RequestHandler(); | ||
|
||
public BaseHttpClient(Uri? uri = null, HttpClientHandlerBuilder? builder = null) | ||
{ | ||
var httpClientHanlder = builder?.Build() | ||
?? new HttpClientHandlerBuilder().WithAllowAutoRedirect().WithAutomaticDecompression() | ||
.UseCertificateCustomValidation().UseSslProtocols().Build(); | ||
|
||
_client = new System.Net.Http.HttpClient(httpClientHanlder); | ||
_client.BaseAddress = uri; | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> GetAsync<TResult>(string uri, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (uri == null) throw new ArgumentNullException(nameof(uri)); | ||
|
||
return await GetAsync<TResult>(new Uri(uri, UriKind.Relative), cancellationToken); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> GetAsync<TResult>(Uri relativeUri, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await RequestHandler.HandleAsync<TResult>(() => | ||
_client.GetAsync(relativeUri, cancellationToken)); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> PostAsync<TResult>(string uri, object content, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (uri == null) throw new ArgumentNullException(nameof(uri)); | ||
|
||
return await PostAsync<TResult>(new Uri(uri, UriKind.Relative), content, cancellationToken); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> PostAsync<TResult>(Uri relativeUri, object content, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await RequestHandler.HandleAsync<TResult>(() => | ||
_client.PostAsync(relativeUri, content.ToStringContent(), cancellationToken)); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> PutAsync<TResult>(string uri, object content, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (uri == null) throw new ArgumentNullException(nameof(uri)); | ||
|
||
return await PutAsync<TResult>(new Uri(uri, UriKind.Relative), content, cancellationToken); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> PutAsync<TResult>(Uri relativeUri, object content, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await RequestHandler.HandleAsync<TResult>(() => | ||
_client.PutAsync(relativeUri, content.ToStringContent(), cancellationToken)); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> DeleteAsync<TResult>(string uri, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (uri == null) throw new ArgumentNullException(nameof(uri)); | ||
|
||
return await DeleteAsync<TResult>(uri, cancellationToken); | ||
} | ||
|
||
public virtual async Task<ApiResponse<TResult>> DeleteAsync<TResult>(Uri relativeUri, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await RequestHandler.HandleAsync<TResult>(() => | ||
_client.DeleteAsync(relativeUri, cancellationToken)); | ||
} | ||
|
||
public void UseHeaders(IDictionary<string, string> headers) | ||
{ | ||
if (headers == null) throw new ArgumentNullException(nameof(headers)); | ||
|
||
_client.DefaultRequestHeaders.Clear(); | ||
|
||
foreach (var header in headers) | ||
{ | ||
_client.DefaultRequestHeaders.Add(header.Key, header.Value); | ||
} | ||
} | ||
|
||
public void OverrideHeaders(Dictionary<string, string> headers) | ||
{ | ||
if (headers == null) throw new ArgumentNullException(nameof(headers)); | ||
|
||
foreach (var header in headers) | ||
{ | ||
_client.DefaultRequestHeaders.Remove(header.Key); | ||
_client.DefaultRequestHeaders.Add(header.Key, header.Value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Net; | ||
|
||
namespace HttpClient.Core.Web.Http.Builders | ||
{ | ||
public class HttpClientHandlerBuilder | ||
{ | ||
private readonly HttpClientHandler _httpClientHandler; | ||
|
||
public HttpClientHandlerBuilder() | ||
{ | ||
_httpClientHandler = new HttpClientHandler(); | ||
} | ||
|
||
public HttpClientHandlerBuilder UseSslProtocols() | ||
{ | ||
ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls | ||
| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13; | ||
|
||
return this; | ||
} | ||
|
||
public HttpClientHandlerBuilder UseCertificateCustomValidation() | ||
{ | ||
_httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; | ||
|
||
return this; | ||
} | ||
|
||
public HttpClientHandlerBuilder WithAllowAutoRedirect() | ||
{ | ||
_httpClientHandler.AllowAutoRedirect = true; | ||
|
||
return this; | ||
} | ||
|
||
public HttpClientHandlerBuilder WithAutomaticDecompression() | ||
{ | ||
_httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | | ||
DecompressionMethods.Brotli; | ||
|
||
return this; | ||
} | ||
|
||
public HttpClientHandlerBuilder WithProxy(string address) | ||
{ | ||
_httpClientHandler.Proxy = new WebProxy(address); | ||
|
||
_httpClientHandler.UseProxy = true; | ||
|
||
return this; | ||
} | ||
|
||
public HttpClientHandler Build() | ||
{ | ||
return _httpClientHandler; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/HttpClient.Core/Extensions/HttpStatusCodeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Net; | ||
|
||
namespace HttpClient.Core.Web.Http.Extensions | ||
{ | ||
public static class HttpStatusCodeExtensions | ||
{ | ||
public static bool IsErrorStatusCode(this HttpStatusCode? code) | ||
{ | ||
return code >= HttpStatusCode.BadRequest; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Newtonsoft.Json; | ||
using System.Text; | ||
|
||
namespace HttpClient.Core.Web.Http.Extensions | ||
{ | ||
internal static class ObjectExtensions | ||
{ | ||
public static StringContent ToStringContent(this object obj) | ||
{ | ||
return new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using HttpClient.Core.Web.Http.ResponseMapers; | ||
using HttpClient.Core.Web.Http.ResponseReaders; | ||
|
||
namespace HttpClient.Core.Web.Http.RequestHandlers | ||
{ | ||
public interface IRequestHandler | ||
{ | ||
IResponseMaper Maper { get; } | ||
|
||
IResponseReader Reader { get; } | ||
|
||
Task<ApiResponse<TResult>> HandleAsync<TResult>(Func<Task<HttpResponseMessage>> request); | ||
} | ||
} |
Oops, something went wrong.