final1
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user