83 lines
3.0 KiB
C#
83 lines
3.0 KiB
C#
using Newtonsoft.Json;
|
|
using PokemonSleepInterface.Models;
|
|
using PokemonSleepInterface.Service.IService;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using static PokemonSleepInterface.Utility.StaticDetails;
|
|
|
|
namespace PokemonSleepInterface.Service
|
|
{
|
|
public class BaseService : IBaseService
|
|
{
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
public BaseService(IHttpClientFactory httpClientFactory)
|
|
{
|
|
_httpClientFactory = httpClientFactory;
|
|
}
|
|
public async Task<ResponseDto?> SendAsync(RequestDto requestDto, bool withBearer = true)
|
|
{
|
|
try
|
|
{
|
|
HttpClient client = _httpClientFactory.CreateClient("PokemonSleepAPI");
|
|
HttpRequestMessage message = new();
|
|
message.Headers.Add("Accept", "application/json");
|
|
|
|
message.RequestUri = new Uri(requestDto.Url);
|
|
if (requestDto.Data != null)
|
|
{
|
|
message.Content = new StringContent(JsonConvert.SerializeObject(requestDto.Data), Encoding.UTF8, "application/json");
|
|
}
|
|
|
|
HttpResponseMessage? apiResponse = null;
|
|
|
|
switch (requestDto.ApiType)
|
|
{
|
|
case ApiType.POST:
|
|
message.Method = HttpMethod.Post;
|
|
break;
|
|
case ApiType.DELETE:
|
|
message.Method = HttpMethod.Delete;
|
|
break;
|
|
case ApiType.PUT:
|
|
message.Method = HttpMethod.Put;
|
|
break;
|
|
default:
|
|
message.Method = HttpMethod.Get;
|
|
break;
|
|
|
|
}
|
|
|
|
apiResponse = await client.SendAsync(message);
|
|
|
|
switch (apiResponse.StatusCode)
|
|
{
|
|
case HttpStatusCode.NotFound:
|
|
return new() { IsSuccess = false, Message = "Not Found" };
|
|
case HttpStatusCode.Forbidden:
|
|
return new() { IsSuccess = false, Message = "Access Denied" };
|
|
case HttpStatusCode.Unauthorized:
|
|
return new() { IsSuccess = false, Message = "Unauthorized" };
|
|
case HttpStatusCode.InternalServerError:
|
|
return new() { IsSuccess = false, Message = "Internal Server Error" };
|
|
default:
|
|
var apiContent = await apiResponse.Content.ReadAsStringAsync();
|
|
var apiResponseDto = JsonConvert.DeserializeObject<ResponseDto>(apiContent);
|
|
return apiResponseDto;
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var dto = new ResponseDto
|
|
{
|
|
IsSuccess = false,
|
|
Message = ex.Message.ToString(),
|
|
};
|
|
return dto;
|
|
}
|
|
}
|
|
}
|
|
}
|