Files

198 lines
6.5 KiB
C#
Raw Permalink Normal View History

2024-01-19 17:25:56 +01:00
using Newtonsoft.Json;
using SecureBank.Common;
using System;
2024-01-23 15:41:59 +01:00
using System.Collections;
2024-01-19 17:25:56 +01:00
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
2024-01-23 15:41:59 +01:00
public async Task<APIResponse<TResponse>> SendAsync<TResponse>(APIMethodType type, string url, Dictionary<string, string>? query = null)
2024-01-19 17:25:56 +01:00
{
2024-01-23 15:41:59 +01:00
url = AddQuery(url, query);
return await SendRequestAndParseBodyAsync<TResponse>(type, url, null);
2024-01-19 17:25:56 +01:00
}
2024-01-23 15:41:59 +01:00
public async Task<APIResponse> SendAsync(APIMethodType type, string url, Dictionary<string, string>? query = null)
2024-01-19 17:25:56 +01:00
{
2024-01-23 15:41:59 +01:00
url = AddQuery(url, query);
return await SendRequestAndParseBodyAsync(type, url, null);
2024-01-19 17:25:56 +01:00
}
2024-01-23 15:41:59 +01:00
public async Task<APIResponse<TResponse>> SendAsync<TResponse, TBody>(APIMethodType type, string url, TBody body, Dictionary<string, string>? query = null)
2024-01-19 17:25:56 +01:00
{
2024-01-23 15:41:59 +01:00
url = AddQuery(url, query);
2024-01-19 17:25:56 +01:00
HttpContent content = PrepareBody(body);
2024-01-23 15:41:59 +01:00
return await SendRequestAndParseBodyAsync<TResponse>(type, url, content);
2024-01-19 17:25:56 +01:00
}
2024-01-23 15:41:59 +01:00
public async Task<APIResponse> SendAsync<TBody>(APIMethodType type, string url, TBody body, Dictionary<string, string>? query = null)
2024-01-19 17:25:56 +01:00
{
2024-01-23 15:41:59 +01:00
url = AddQuery(url, query);
2024-01-19 17:25:56 +01:00
HttpContent content = PrepareBody(body);
2024-01-23 15:41:59 +01:00
return await SendRequestAndParseBodyAsync(type, url, content);
2024-01-19 17:25:56 +01:00
}
#endregion
#region PRIVATE METHODS
2024-01-23 15:41:59 +01:00
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;
}
2024-01-19 17:25:56 +01:00
private HttpContent PrepareBody<T>(T body)
{
string json = JsonConvert.SerializeObject(body);
HttpContent content = new StringContent(json);
content.Headers.ContentType.MediaType = "application/json";
return content;
}
2024-01-23 15:41:59 +01:00
private async Task<APIResponse> SendRequestAndParseBodyAsync(APIMethodType type, string url, HttpContent? content)
2024-01-19 17:25:56 +01:00
{
try
{
2024-01-23 15:41:59 +01:00
HttpResponseMessage response = await SendRequestAsync(type, url, content);
string stringResponse = await response.Content.ReadAsStringAsync();
APIResponse? responseBodyObject = JsonConvert.DeserializeObject<APIResponse>(stringResponse);
if (responseBodyObject is null)
{
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)
{
return new APIResponse
2024-01-19 17:25:56 +01:00
{
2024-01-23 15:41:59 +01:00
Status = ResponseStatus.BadRequest,
Message = ex.Message
2024-01-19 17:25:56 +01:00
};
2024-01-23 15:41:59 +01:00
}
}
2024-01-19 17:25:56 +01:00
2024-01-23 15:41:59 +01:00
private async Task<APIResponse<T>> SendRequestAndParseBodyAsync<T>(APIMethodType type, string url, HttpContent? content)
{
try
{
HttpResponseMessage response = await SendRequestAsync(type, url, content);
2024-01-19 17:25:56 +01:00
2024-01-23 15:41:59 +01:00
string stringResponse = await response.Content.ReadAsStringAsync();
APIResponse<T>? responseBodyObject = JsonConvert.DeserializeObject<APIResponse<T>>(stringResponse);
2024-01-19 17:25:56 +01:00
if (responseBodyObject is null)
{
2024-01-23 15:41:59 +01:00
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}"
};
}
2024-01-19 17:25:56 +01:00
}
return responseBodyObject;
}
catch (Exception ex)
{
2024-01-23 15:41:59 +01:00
return new APIResponse<T>
{
Status = ResponseStatus.BadRequest,
Message = ex.Message
};
2024-01-19 17:25:56 +01:00
}
}
2024-01-23 15:41:59 +01:00
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()
};
}
2024-01-19 17:25:56 +01:00
#endregion
}
}