From c84553a7bc9c7df3eb5a766538c2fabb4645a802 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 21 Dec 2020 00:44:37 +0200 Subject: [PATCH] NDB.Extensions.Http package --- NDB.Extensions.Http/HttpClientExtensions.cs | 51 +++++++++++++++++++ .../NDB.Extensions.Http.csproj | 17 +++++++ NDB.sln | 12 ++++- Notes.txt | 2 + 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 NDB.Extensions.Http/HttpClientExtensions.cs create mode 100644 NDB.Extensions.Http/NDB.Extensions.Http.csproj diff --git a/NDB.Extensions.Http/HttpClientExtensions.cs b/NDB.Extensions.Http/HttpClientExtensions.cs new file mode 100644 index 0000000..9518a38 --- /dev/null +++ b/NDB.Extensions.Http/HttpClientExtensions.cs @@ -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 ExecuteGetRequest(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(await response.Content.ReadAsStringAsync()); + return result; + } + } + + public static async Task ExecutePostRequest(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(await response.Content.ReadAsStringAsync()); + return result; + } + } + + public static async Task ExecutePostRequest(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(await response.Content.ReadAsStringAsync()); + return result; + } + } + } +} diff --git a/NDB.Extensions.Http/NDB.Extensions.Http.csproj b/NDB.Extensions.Http/NDB.Extensions.Http.csproj new file mode 100644 index 0000000..b35bf8e --- /dev/null +++ b/NDB.Extensions.Http/NDB.Extensions.Http.csproj @@ -0,0 +1,17 @@ + + + + netstandard2.0 + NDB http + Http extensions + Http extensions + https://dev.azure.com/tstanciu94/NDB + Git + https://dev.azure.com/tstanciu94/NDB + + + + + + + diff --git a/NDB.sln b/NDB.sln index 59dac51..d167a60 100644 --- a/NDB.sln +++ b/NDB.sln @@ -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} diff --git a/Notes.txt b/Notes.txt index dd109f2..c3e994d 100644 --- a/Notes.txt +++ b/Notes.txt @@ -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 ####################################################################################################################################################### \ No newline at end of file