This commit is contained in:
2024-01-23 15:41:59 +01:00
Unverified
parent 5d5a69ccf7
commit 3b2b4c9b7e
76 changed files with 4100 additions and 888 deletions

View File

@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using SecureBank.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -31,28 +32,32 @@ namespace SecureBank.Website.API
#region PUBLIC METHODS
public async Task<APIResponse<TResponse>> SendAsync<TResponse>(APIMethodType type, string url)
public async Task<APIResponse<TResponse>> SendAsync<TResponse>(APIMethodType type, string url, Dictionary<string, string>? query = null)
{
return await SendRequestAsync<APIResponse<TResponse>>(type, url, null);
url = AddQuery(url, query);
return await SendRequestAndParseBodyAsync<TResponse>(type, url, null);
}
public async Task<APIResponse> SendAsync(APIMethodType type, string url)
public async Task<APIResponse> SendAsync(APIMethodType type, string url, Dictionary<string, string>? query = null)
{
return await SendRequestAsync<APIResponse>(type, url, null);
url = AddQuery(url, query);
return await SendRequestAndParseBodyAsync(type, url, null);
}
public async Task<APIResponse<TResponse>> SendAsync<TResponse, TBody>(APIMethodType type, string url, TBody body)
public async Task<APIResponse<TResponse>> SendAsync<TResponse, TBody>(APIMethodType type, string url, TBody body, Dictionary<string, string>? query = null)
{
url = AddQuery(url, query);
HttpContent content = PrepareBody(body);
return await SendRequestAsync<APIResponse<TResponse>>(type, url, content);
return await SendRequestAndParseBodyAsync<TResponse>(type, url, content);
}
public async Task<APIResponse> SendAsync<TBody>(APIMethodType type, string url, TBody body)
public async Task<APIResponse> SendAsync<TBody>(APIMethodType type, string url, TBody body, Dictionary<string, string>? query = null)
{
url = AddQuery(url, query);
HttpContent content = PrepareBody(body);
return await SendRequestAsync<APIResponse>(type, url, content);
return await SendRequestAndParseBodyAsync(type, url, content);
}
#endregion
@@ -61,6 +66,25 @@ namespace SecureBank.Website.API
#region PRIVATE METHODS
private string AddQuery(string url, Dictionary<string, string>? query)
{
if (query is not null && query.Count > 0)
{
Dictionary<string, string> queryNew = query.ToDictionary();
StringBuilder sb = new StringBuilder(url);
KeyValuePair<string, string> item = queryNew.ElementAt(0);
queryNew.Remove(item.Key);
sb.Append($"?{item.Key}={item.Value}");
foreach (KeyValuePair<string, string> item2 in queryNew)
{
sb.Append($"&{item2.Key}={item2.Value}");
}
return sb.ToString();
}
return url;
}
private HttpContent PrepareBody<T>(T body)
{
string json = JsonConvert.SerializeObject(body);
@@ -71,37 +95,103 @@ namespace SecureBank.Website.API
return content;
}
private async Task<T> SendRequestAsync<T>(APIMethodType type, string url, HttpContent? content)
private async Task<APIResponse> SendRequestAndParseBodyAsync(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()
};
HttpResponseMessage response = await SendRequestAsync(type, url, content);
string stringResponse = await response.Content.ReadAsStringAsync();
string responseBodyString = await response.Content.ReadAsStringAsync();
T? responseBodyObject = JsonConvert.DeserializeObject<T>(responseBodyString);
APIResponse? responseBodyObject = JsonConvert.DeserializeObject<APIResponse>(stringResponse);
if (responseBodyObject is null)
{
throw new Exception($"Wrong response type. Response: {responseBodyString}; {response.StatusCode}");
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
return new APIResponse
{
Status = ResponseStatus.Unauthorized,
Message = $"You do not have permission"
};
}
else
{
return new APIResponse
{
Status = ResponseStatus.BadRequest,
Message = $"Wrong response type. Response: {stringResponse}; {response.StatusCode}"
};
}
}
return responseBodyObject;
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
return new APIResponse
{
Status = ResponseStatus.BadRequest,
Message = ex.Message
};
}
}
private async Task<APIResponse<T>> SendRequestAndParseBodyAsync<T>(APIMethodType type, string url, HttpContent? content)
{
try
{
HttpResponseMessage response = await SendRequestAsync(type, url, content);
string stringResponse = await response.Content.ReadAsStringAsync();
APIResponse<T>? responseBodyObject = JsonConvert.DeserializeObject<APIResponse<T>>(stringResponse);
if (responseBodyObject is null)
{
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
return new APIResponse<T>
{
Status = ResponseStatus.Unauthorized,
Message = $"You do not have permission"
};
}
else
{
return new APIResponse<T>
{
Status = ResponseStatus.BadRequest,
Message = $"Wrong response type. Response: {stringResponse}; {response.StatusCode}"
};
}
}
return responseBodyObject;
}
catch (Exception ex)
{
return new APIResponse<T>
{
Status = ResponseStatus.BadRequest,
Message = ex.Message
};
}
}
private async Task<HttpResponseMessage> SendRequestAsync(APIMethodType type, string url, HttpContent? content)
{
return type switch
{
APIMethodType.GET => await _httpClient.GetAsync(url),
APIMethodType.POST => await _httpClient.PostAsync(url, content),
APIMethodType.PUT => await _httpClient.PutAsync(url, content),
APIMethodType.PATCH => await _httpClient.PatchAsync(url, content),
APIMethodType.DELETE => await _httpClient.DeleteAsync(url),
_ => throw new NotImplementedException()
};
}
#endregion
}
}

View File

@@ -16,9 +16,25 @@ namespace SecureBank.Website.API
// Accounts
public string AccountsBase { get; private set; }
public string AccountsCreateAccount { get; private set; }
public string AccountsChangePassword { get; private set; }
public string AccountsGetPasswordVariant { get; private set; }
public string AccountsAuthentication { get; private set; }
public string AccountsAuthenticationRefresh { get; private set; }
public string AccountsGetAccounts { get; private set; }
public string AccountsResetPassword { get; private set; }
public string AccountsUnlockAccount { get; private set; }
// Balance
public string BalanceBase { get; private set; }
public string BalanceGetAccountBalance { get; private set; }
public string BalanceGetBalance { get; private set; }
// Transfers
public string TransfersBase { get; private set; }
public string TransfersGetUserTransfers { get; private set; }
public string TransfersGetTransfers { get; private set; }
public string TransfersCreateAdminTransfer { get; private set; }
public string TransfersCreateUserTransfer { get; private set; }
#endregion
@@ -32,9 +48,23 @@ namespace SecureBank.Website.API
AccountsBase = $"{Base}{configuration.GetSection("Endpoints").GetSection("Accounts")["Base"]}";
AccountsCreateAccount = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["CreateAccount"]}";
AccountsChangePassword = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["ChangePassword"]}";
AccountsGetPasswordVariant = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["GetPasswordVariant"]}";
AccountsAuthentication = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["Authentication"]}";
AccountsAuthenticationRefresh = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["AuthenticationRefresh"]}";
AccountsGetAccounts = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["GetAccounts"]}";
AccountsResetPassword = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["ResetPassword"]}";
AccountsUnlockAccount = $"{AccountsBase}{configuration.GetSection("Endpoints").GetSection("Accounts")["UnlockAccount"]}";
BalanceBase = $"{Base}{configuration.GetSection("Endpoints").GetSection("Balance")["Base"]}";
BalanceGetAccountBalance = $"{BalanceBase}{configuration.GetSection("Endpoints").GetSection("Balance")["GetAccountBalance"]}";
BalanceGetBalance = $"{BalanceBase}{configuration.GetSection("Endpoints").GetSection("Balance")["GetBalance"]}";
TransfersBase = $"{Base}{configuration.GetSection("Endpoints").GetSection("Transfers")["Base"]}";
TransfersGetTransfers = $"{TransfersBase}{configuration.GetSection("Endpoints").GetSection("Transfers")["GetTransfers"]}";
TransfersGetUserTransfers = $"{TransfersBase}{configuration.GetSection("Endpoints").GetSection("Transfers")["GetUserTransfers"]}";
TransfersCreateAdminTransfer = $"{TransfersBase}{configuration.GetSection("Endpoints").GetSection("Transfers")["CreateAdminTransfer"]}";
TransfersCreateUserTransfer = $"{TransfersBase}{configuration.GetSection("Endpoints").GetSection("Transfers")["CreateUserTransfer"]}";
}
#endregion

View File

@@ -11,6 +11,7 @@ namespace SecureBank.Website.API
GET,
POST,
PUT,
PATCH,
DELETE
}
}