NDB.Extensions.Http package

messaging
Tudor Stanciu 2020-12-21 00:44:37 +02:00
parent e531f347c2
commit c84553a7bc
4 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,51 @@
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace NDB.Extensions.Http
{
public static class HttpClientExtensions
{
private const string _contentType = "application/json";
public static async Task<T> ExecuteGetRequest<T>(this HttpClient httpClient, string route) where T : class
{
using (var response = await httpClient.GetAsync(route))
{
if (!response.IsSuccessStatusCode)
throw new Exception($"Error: StatusCode: {response.StatusCode}; Reason: {response.ReasonPhrase}");
var result = JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
return result;
}
}
public static async Task<R> ExecutePostRequest<R>(this HttpClient httpClient, string route) where R : class
{
HttpContent content = new StringContent(JsonConvert.SerializeObject(string.Empty), Encoding.UTF8, _contentType);
using (var response = await httpClient.PostAsync(route, content))
{
if (!response.IsSuccessStatusCode)
throw new Exception($"Error: StatusCode: {response.StatusCode}; Reason: {response.ReasonPhrase}");
var result = JsonConvert.DeserializeObject<R>(await response.Content.ReadAsStringAsync());
return result;
}
}
public static async Task<R> ExecutePostRequest<R, B>(this HttpClient httpClient, string route, B body) where R : class where B : class
{
HttpContent content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, _contentType);
using (var response = await httpClient.PostAsync(route, content))
{
if (!response.IsSuccessStatusCode)
throw new Exception($"Error: StatusCode: {response.StatusCode}; Reason: {response.ReasonPhrase}");
var result = JsonConvert.DeserializeObject<R>(await response.Content.ReadAsStringAsync());
return result;
}
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageTags>NDB http</PackageTags>
<Description>Http extensions</Description>
<PackageReleaseNotes>Http extensions</PackageReleaseNotes>
<PackageProjectUrl>https://dev.azure.com/tstanciu94/NDB</PackageProjectUrl>
<RepositoryType>Git</RepositoryType>
<RepositoryUrl>https://dev.azure.com/tstanciu94/NDB</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

12
NDB.sln
View File

@ -32,7 +32,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Extensions.Swagger", "N
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "application", "application", "{564B5266-6F7E-4AFA-A7D4-B0E8F2E7107E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Application.DataContracts", "NDB.Application.DataContracts\NDB.Application.DataContracts.csproj", "{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Application.DataContracts", "NDB.Application.DataContracts\NDB.Application.DataContracts.csproj", "{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "http", "http", "{C1301480-5C4C-4F73-8D26-DD3E798FAFD5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Extensions.Http", "NDB.Extensions.Http\NDB.Extensions.Http.csproj", "{28D5CE9E-D975-4842-8B30-5063B82979C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -68,6 +72,10 @@ Global
{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Release|Any CPU.Build.0 = Release|Any CPU
{28D5CE9E-D975-4842-8B30-5063B82979C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28D5CE9E-D975-4842-8B30-5063B82979C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28D5CE9E-D975-4842-8B30-5063B82979C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28D5CE9E-D975-4842-8B30-5063B82979C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -78,6 +86,8 @@ Global
{CB9BDFCC-DB01-4A13-8091-9A241BE85CCD} = {FB4E71FD-9E32-4B5C-8019-1E8EBB9DDE9F}
{564B5266-6F7E-4AFA-A7D4-B0E8F2E7107E} = {E0202271-4E92-4DB8-900D-B5FD745B9278}
{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3} = {564B5266-6F7E-4AFA-A7D4-B0E8F2E7107E}
{C1301480-5C4C-4F73-8D26-DD3E798FAFD5} = {B50B55F0-9E6E-4061-9100-E2329A44E76B}
{28D5CE9E-D975-4842-8B30-5063B82979C6} = {C1301480-5C4C-4F73-8D26-DD3E798FAFD5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {87541BAB-3FAC-4ADB-A7FB-8228DA87843D}

View File

@ -19,4 +19,6 @@ Push packages:
dotnet nuget push NDB.Application.DataContracts.1.0.0.nupkg -k ***REMOVED*** -s http://stawebsrv:8081/NuGetServer/nuget
dotnet nuget push NDB.Extensions.Swagger.1.0.0.nupkg -k ***REMOVED*** -s http://stawebsrv:8081/NuGetServer/nuget
dotnet nuget push NDB.Extensions.Http.1.0.0.nupkg -k ***REMOVED*** -s http://stawebsrv:8081/NuGetServer/nuget
#######################################################################################################################################################