exciting-aftermath/Portfolio.Application/Services/BaseService.cs

91 lines
3.1 KiB
C#

using Newtonsoft.Json;
using Portfolio.Domain.Features.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using static Portfolio.Domain.Utility.StaticDetails;
namespace Portfolio.Application.Services
{
public class BaseService : IBaseService
{
private readonly IHttpClientFactory _httpClientFactory;
public BaseService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<ResponseDto?> SendAsync(RequestDto requestDto)
{
try
{
HttpClient client = _httpClientFactory.CreateClient("PokemonSleepAPI");
HttpRequestMessage message = new();
message.Headers.Add("Accept", "application/json");
//token here
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;
}
}
}
}