wrapper - replaced http client methods with nuget package

master
Tudor Stanciu 2020-12-21 00:48:41 +02:00
parent d2730bc123
commit 4df05ce914
2 changed files with 4 additions and 45 deletions

View File

@ -8,7 +8,7 @@
<PackageReference Include="IdentityServer.PublishedLanguage" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NDB.Extensions.Http" Version="1.0.0" />
</ItemGroup>
</Project>

View File

@ -1,11 +1,10 @@
using IdentityServer.PublishedLanguage.Dto;
using IdentityServer.Wrapper.Constants;
using IdentityServer.Wrapper.Models;
using Newtonsoft.Json;
using NDB.Extensions.Http;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace IdentityServer.Wrapper.Services
@ -26,55 +25,15 @@ namespace IdentityServer.Wrapper.Services
public async Task<Token> Authenticate(string userName, string password)
{
var route = string.Format(ApiRoutes.Authentication, userName, password);
var result = await ExecutePostRequest<Token>(route);
var result = await _httpClient.ExecutePostRequest<Token>(route);
return result;
}
public async Task<User> Authorize(string token)
{
var route = string.Format(ApiRoutes.Authorization, token);
var result = await ExecutePostRequest<User>(route);
var result = await _httpClient.ExecutePostRequest<User>(route);
return result;
}
#region Private methods
private async Task<T> ExecuteGetRequest<T>(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;
}
}
private async Task<R> ExecutePostRequest<R>(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;
}
}
private async Task<R> ExecutePostRequest<R, B>(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;
}
}
#endregion
}
}