119 lines
4.0 KiB
C#
119 lines
4.0 KiB
C#
using System.Net.Http.Headers;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using MauiApp.Core.Business.Contracts;
|
|
using MauiApp.Core.RestClient.IntegryApi.Contracts;
|
|
using MauiApp.Core.RestClient.IntegryApi.Dto;
|
|
using MauiApp.Core.RestClient.IntegryApi.Exceptions;
|
|
|
|
namespace MauiApp.Core.RestClient.IntegryApi;
|
|
|
|
public class IntegryApiRestClient : ApiRestClient, IIntegryApiRestClient
|
|
{
|
|
|
|
private readonly IMessenger _messenger;
|
|
private readonly IUserSessionService _userSessionService;
|
|
|
|
public IntegryApiRestClient(IUserSessionService userSessionService, IMessenger messenger)
|
|
{
|
|
_userSessionService = userSessionService;
|
|
_messenger = messenger;
|
|
}
|
|
|
|
public async Task<T> Post<T>(string url, object body, IDictionary<string, object> queryParams = null, HttpClient httpClient = null)
|
|
{
|
|
queryParams ??= new Dictionary<string, object>();
|
|
|
|
if(_userSessionService?.Session?.ProfileDb != null)
|
|
queryParams.TryAdd("profileDb", _userSessionService.Session.ProfileDb);
|
|
|
|
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)
|
|
{
|
|
queryParams ??= new Dictionary<string, object>();
|
|
|
|
if (_userSessionService?.Session?.ProfileDb != null)
|
|
queryParams.TryAdd("profileDb", _userSessionService.Session.ProfileDb);
|
|
|
|
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<T> AuthorizedPost<T>(string url, object body, IDictionary<string, object> queryParams = null, HttpClient httpClient = null)
|
|
{
|
|
//if (!_userDataSession.IsAuthorized())
|
|
//{
|
|
// if (!_userDataSession.IsRefreshTokenValid())
|
|
// {
|
|
// _messenger.Send(new NewLoginNeededMessage());
|
|
// return default;
|
|
// }
|
|
// else
|
|
// {
|
|
// await _appAuthenticationStateProvider.RefreshTokens();
|
|
// }
|
|
//}
|
|
|
|
httpClient ??= new HttpClient();
|
|
httpClient.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", _userSessionService.Session.AccessToken);
|
|
|
|
queryParams ??= new Dictionary<string, object>();
|
|
queryParams.TryAdd("profileDb", _userSessionService.Session.ProfileDb);
|
|
|
|
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> AuthorizedGet<T>(string url, IDictionary<string, object> queryParams = null, HttpClient httpClient = null)
|
|
{
|
|
//if (!_userDataSession.IsAuthorized())
|
|
//{
|
|
// if (!_userDataSession.IsRefreshTokenValid())
|
|
// {
|
|
// _messenger.Send(new NewLoginNeededMessage());
|
|
// return default;
|
|
// }
|
|
// else
|
|
// {
|
|
// await _appAuthenticationStateProvider.RefreshTokens();
|
|
// }
|
|
//}
|
|
|
|
httpClient ??= new HttpClient();
|
|
httpClient.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", _userSessionService.Session.AccessToken);
|
|
|
|
queryParams ??= new Dictionary<string, object>();
|
|
queryParams.TryAdd("profileDb", _userSessionService.Session.ProfileDb);
|
|
|
|
var result = await base.Get<BaseRestResponse<T>>(url, queryParams, httpClient);
|
|
|
|
if (result?.Esito == -1)
|
|
{
|
|
throw new RestException(result.ErrorMessage);
|
|
}
|
|
|
|
return result.Dto ?? result.Entity;
|
|
}
|
|
} |