59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using MauiApp.Core.RestClient.AuthenticationApi.Contracts;
|
|
using MauiApp.Core.RestClient.IntegryApi.Dto;
|
|
using MauiApp.Core.RestClient.IntegryApi.Exceptions;
|
|
using MauiApp.Core.System.Device.Contracts;
|
|
|
|
namespace MauiApp.Core.RestClient.AuthenticationApi;
|
|
|
|
public class AuthenticationApiRestClient : ApiRestClient, IAuthenticationApiRestClient
|
|
{
|
|
|
|
private readonly IDeviceService _deviceService;
|
|
|
|
public AuthenticationApiRestClient(IDeviceService deviceService)
|
|
{
|
|
_deviceService = deviceService;
|
|
}
|
|
public async Task<T> Post<T>(string url, object body, IDictionary<string, object> queryParams = null, HttpClient httpClient = null)
|
|
{
|
|
var result = await base.Post<BaseRestResponse<T>>(url, body, queryParams, httpClient);
|
|
|
|
if (result?.Esito == -1)
|
|
{
|
|
throw new RestException(result.ErrorMessage);
|
|
}
|
|
|
|
return result.Dto ?? result.Entity;
|
|
}
|
|
|
|
public async Task<T> Get<T>(string url, IDictionary<string, object> queryParams = null, HttpClient httpClient = null)
|
|
{
|
|
var result = await base.Get<BaseRestResponse<T>>(url, queryParams, httpClient);
|
|
|
|
if (result?.Esito == -1)
|
|
{
|
|
throw new RestException(result.ErrorMessage);
|
|
}
|
|
|
|
return result.Dto ?? result.Entity;
|
|
}
|
|
|
|
|
|
public async Task<LoginResponseDto> RefreshToken(string refreshToken, string profileDb)
|
|
{
|
|
IDictionary<string, object> queryParams = new Dictionary<string, object>
|
|
{
|
|
{ "profileDb", profileDb }
|
|
};
|
|
|
|
var refreshTokenRequest = new AuthRefreshTokenRequestDto()
|
|
{
|
|
DeviceId = await _deviceService.GetDeviceId(),
|
|
RefreshToken = refreshToken
|
|
};
|
|
|
|
var refreshTokenResponse = await Post<LoginResponseDto>("auth/refresh", refreshTokenRequest, queryParams);
|
|
|
|
return refreshTokenResponse;
|
|
}
|
|
} |