init
This commit is contained in:
107
SecureBank.Website/SecureBank.Website.API/APIClient.cs
Normal file
107
SecureBank.Website/SecureBank.Website.API/APIClient.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using Newtonsoft.Json;
|
||||
using SecureBank.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Website.API
|
||||
{
|
||||
public class APIClient
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public APIClient(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<APIResponse<TResponse>> SendAsync<TResponse>(APIMethodType type, string url)
|
||||
{
|
||||
return await SendRequestAsync<APIResponse<TResponse>>(type, url, null);
|
||||
}
|
||||
|
||||
public async Task<APIResponse> SendAsync(APIMethodType type, string url)
|
||||
{
|
||||
return await SendRequestAsync<APIResponse>(type, url, null);
|
||||
}
|
||||
|
||||
public async Task<APIResponse<TResponse>> SendAsync<TResponse, TBody>(APIMethodType type, string url, TBody body)
|
||||
{
|
||||
HttpContent content = PrepareBody(body);
|
||||
|
||||
return await SendRequestAsync<APIResponse<TResponse>>(type, url, content);
|
||||
}
|
||||
|
||||
public async Task<APIResponse> SendAsync<TBody>(APIMethodType type, string url, TBody body)
|
||||
{
|
||||
HttpContent content = PrepareBody(body);
|
||||
|
||||
return await SendRequestAsync<APIResponse>(type, url, content);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private HttpContent PrepareBody<T>(T body)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(body);
|
||||
|
||||
HttpContent content = new StringContent(json);
|
||||
content.Headers.ContentType.MediaType = "application/json";
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
private async Task<T> SendRequestAsync<T>(APIMethodType type, string url, HttpContent? content)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = type switch
|
||||
{
|
||||
APIMethodType.GET => await _httpClient.GetAsync(url),
|
||||
APIMethodType.POST => await _httpClient.PostAsync(url, content),
|
||||
APIMethodType.PUT => await _httpClient.PutAsync(url, content),
|
||||
APIMethodType.DELETE => await _httpClient.DeleteAsync(url),
|
||||
_ => throw new NotImplementedException()
|
||||
};
|
||||
|
||||
string responseBodyString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
T? responseBodyObject = JsonConvert.DeserializeObject<T>(responseBodyString);
|
||||
|
||||
if (responseBodyObject is null)
|
||||
{
|
||||
throw new Exception($"Wrong response type. Response: {responseBodyString}; {response.StatusCode}");
|
||||
}
|
||||
|
||||
return responseBodyObject;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Website.API
|
||||
{
|
||||
public class APIEndpointsConfiguration
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public string Base { get; private set; }
|
||||
|
||||
// Accounts
|
||||
public string AccountsBase { get; private set; }
|
||||
public string AccountsCreateAccount { get; private set; }
|
||||
public string AccountsGetPasswordVariant { get; private set; }
|
||||
public string AccountsAuthentication { get; private set; }
|
||||
public string AccountsAuthenticationRefresh { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public APIEndpointsConfiguration(IConfiguration configuration)
|
||||
{
|
||||
Base = configuration.GetSection("Endpoints")["Base"];
|
||||
|
||||
AccountsBase = $"{Base}{configuration.GetSection("Endpoints").GetSection("Accounts")["Base"]}";
|
||||
AccountsCreateAccount = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["CreateAccount"]}";
|
||||
AccountsGetPasswordVariant = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["GetPasswordVariant"]}";
|
||||
AccountsAuthentication = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["Authentication"]}";
|
||||
AccountsAuthenticationRefresh = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["AuthenticationRefresh"]}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
16
SecureBank.Website/SecureBank.Website.API/APIMethodType.cs
Normal file
16
SecureBank.Website/SecureBank.Website.API/APIMethodType.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Website.API
|
||||
{
|
||||
public enum APIMethodType
|
||||
{
|
||||
GET,
|
||||
POST,
|
||||
PUT,
|
||||
DELETE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\SecureBank.Common\SecureBank.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user