Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subindo os arquivos em um novo repositório e atualizado o objeto de r…
…etorno (ResultHttp) para exibir mensagem de erro ao realizar a conversão do obejto de retorno.
- Loading branch information
1 parent
e484420
commit a5962be
Showing
7 changed files
with
564 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,38 @@ | ||
name: Developer.HttpCore | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Setup .NET Core | ||
uses: actions/setup-dotnet@v1 | ||
# with: | ||
# dotnet-version: 7.0.x | ||
|
||
- name: Restore dependencies | ||
working-directory: Developer.HttpCore/Project | ||
run: dotnet restore | ||
|
||
- name: Build | ||
working-directory: Developer.HttpCore/Project | ||
run: dotnet build --configuration Release --no-restore | ||
#- uses: actions/upload-artifact@main | ||
# with: | ||
# name: dropArtifacts | ||
# path: **/src/HttpCore/bin/Release/*.nupkg | ||
|
||
#- name: Test | ||
# working-directory: Developer.HttpCore/Project | ||
# run: dotnet test --no-restore --verbosity normal | ||
|
||
- name: Publish Nuget | ||
run: dotnet nuget push **/*.nupkg -k ${{secrets.NUGETTOKEN}} -s https://api.nuget.org/v3/index.json --skip-duplicate |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.5.33414.496 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Developer.HttpCore", "Developer.HttpCore\Developer.HttpCore.csproj", "{BDA53E24-6A8D-4B8B-8E31-A65638B874BA}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{BDA53E24-6A8D-4B8B-8E31-A65638B874BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{BDA53E24-6A8D-4B8B-8E31-A65638B874BA}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{BDA53E24-6A8D-4B8B-8E31-A65638B874BA}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{BDA53E24-6A8D-4B8B-8E31-A65638B874BA}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {BFC960A4-4F8A-4329-9087-64E777DB7F50} | ||
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,199 @@ | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System; | ||
|
||
namespace Developer.HttpCore | ||
{ | ||
public class BaseHttpFactory | ||
{ | ||
public string _phraseIdentifyRejection; | ||
public readonly HttpClient _client; | ||
private readonly bool _configureAwait; | ||
|
||
public BaseHttpFactory(IHttpClientFactory factory, string factoryName, bool configureAwait = false, string phraseIdentifyRejection = null) | ||
{ | ||
_client = factory.CreateClient(factoryName); | ||
_configureAwait = configureAwait; | ||
_phraseIdentifyRejection = phraseIdentifyRejection; | ||
} | ||
|
||
#region Get | ||
|
||
public async Task<ResultHttp> GetAsync(string requestUri, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.GetAsync(requestUri, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> GetAsync(Uri requestUri, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.GetAsync(requestUri, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> GetAsync(string requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.GetAsync(requestUri, completionOption, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> GetAsync(Uri requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.GetAsync(requestUri, completionOption, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
#endregion Get | ||
|
||
#region Post | ||
|
||
public async Task<ResultHttp> PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PostAsync(requestUri, content, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> PostAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PostAsync(requestUri, content, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> PostStringContentAsync(string requestUri, string json, Encoding encoding, string mediaType = "application/json", CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PostAsync(requestUri, new StringContent(json, encoding, mediaType), cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> PostStringContentAsync(Uri requestUri, string json, Encoding encoding, string mediaType = "application/json", CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PostAsync(requestUri, new StringContent(json, encoding, mediaType), cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
#endregion Post | ||
|
||
#region Put | ||
|
||
public async Task<ResultHttp> PutAsync(string requestUri, HttpContent content, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PutAsync(requestUri, content, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> PutAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PutAsync(requestUri, content, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> PutStringContentAsync(string requestUri, string json, Encoding encoding, string mediaType = "application/json", CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PutAsync(requestUri, new StringContent(json, encoding, mediaType), cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> PutStringContentAsync(Uri requestUri, string json, Encoding encoding, string mediaType = "application/json", CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.PutAsync(requestUri, new StringContent(json, encoding, mediaType), cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
#endregion Put | ||
|
||
#region Delete | ||
|
||
public async Task<ResultHttp> DeleteAsync(string requestUri, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.DeleteAsync(requestUri, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
public async Task<ResultHttp> DeleteAsync(Uri requestUri, CancellationToken cancellationToken = default) | ||
{ | ||
using (HttpResponseMessage response = await _client.DeleteAsync(requestUri, cancellationToken).ConfigureAwait(_configureAwait)) | ||
{ | ||
return await GetResultHttp(response); | ||
} | ||
} | ||
|
||
#endregion Delete | ||
|
||
private async Task<ResultHttp> GetResultHttp(HttpResponseMessage response) | ||
{ | ||
ResultHttp resultHttp = new ResultHttp() { Response = response, HttpStatusCode = response.StatusCode }; | ||
|
||
try | ||
{ | ||
resultHttp.DataString = await response.Content.ReadAsStringAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
resultHttp.Success = false; | ||
resultHttp.Message = ex.Message; | ||
} | ||
|
||
try | ||
{ | ||
resultHttp.DataBytes = await response.Content.ReadAsByteArrayAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
resultHttp.Success = false; | ||
resultHttp.Message = ex.Message; | ||
} | ||
|
||
try | ||
{ | ||
resultHttp.DataStream = await response.Content.ReadAsStreamAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
resultHttp.Success = false; | ||
resultHttp.Message = ex.Message; | ||
} | ||
|
||
if (resultHttp.DataString != null || resultHttp.DataBytes != null || resultHttp.DataStream != null) | ||
{ | ||
resultHttp.Success = true; | ||
resultHttp.Message = null; | ||
} | ||
|
||
if (_phraseIdentifyRejection != null) | ||
{ | ||
resultHttp.RequestRejected = resultHttp.DataString.ToUpper().Contains(_phraseIdentifyRejection.ToUpper()); | ||
} | ||
|
||
return resultHttp; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.