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> SendAsync(APIMethodType type, string url) { return await SendRequestAsync>(type, url, null); } public async Task SendAsync(APIMethodType type, string url) { return await SendRequestAsync(type, url, null); } public async Task> SendAsync(APIMethodType type, string url, TBody body) { HttpContent content = PrepareBody(body); return await SendRequestAsync>(type, url, content); } public async Task SendAsync(APIMethodType type, string url, TBody body) { HttpContent content = PrepareBody(body); return await SendRequestAsync(type, url, content); } #endregion #region PRIVATE METHODS private HttpContent PrepareBody(T body) { string json = JsonConvert.SerializeObject(body); HttpContent content = new StringContent(json); content.Headers.ContentType.MediaType = "application/json"; return content; } private async Task SendRequestAsync(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(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 } }