92 lines
2.7 KiB
C#
92 lines
2.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using RestSharp;
|
|
|
|
namespace EgwCoreLib.Lux.Data.Services.General
|
|
{
|
|
public class CalcRequestService : ICalcRequestService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public CalcRequestService(IConfiguration config)
|
|
{
|
|
_config = config;
|
|
// verifico la url base
|
|
apiUrl = _config.GetValue<string>("ServerConf:Prog.ApiUrl") ?? "https://iis01.egalware.com/lux/srv/api";
|
|
routeBasePath = _config.GetValue<string>("ServerConf:RouteBaseUrl") ?? "window";
|
|
// fix opzioni base del RestClient
|
|
restOptStd = new RestClientOptions
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(60),
|
|
BaseUrl = new Uri($"{apiUrl}/{routeBasePath}")
|
|
};
|
|
Log.Info("ImageCache Service Started");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Effettua una generica chiamata ApiRest
|
|
/// </summary>
|
|
/// <param name="ApiUrl">URL Api di base</param>
|
|
/// <param name="ApiRequest">Richiesta completa</param>
|
|
/// <param name="rawBody">Corpo Json trasmesso con richiesta</param>
|
|
/// <returns></returns>
|
|
public async Task<RestResponse> CallRestPost(string ApiUrl, string ApiRequest, object rawBody)
|
|
{
|
|
using var client = CreateClient(ApiUrl);
|
|
|
|
var request = new RestRequest(ApiRequest, Method.Post);
|
|
request.AddJsonBody(rawBody);
|
|
|
|
return await client.ExecuteAsync(request);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly string apiUrl = "";
|
|
|
|
private readonly string routeBasePath = "";
|
|
|
|
private IConfiguration _config;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Conf client RestSharp standard:
|
|
/// - base URI al sito
|
|
/// - timeout 1 min
|
|
/// </summary>
|
|
private RestClientOptions restOptStd { get; set; } = new RestClientOptions();
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Helper creazione client Rest x RestSharp
|
|
/// </summary>
|
|
/// <param name="baseUrl"></param>
|
|
/// <returns></returns>
|
|
private RestClient CreateClient(string baseUrl)
|
|
{
|
|
var opt = new RestClientOptions
|
|
{
|
|
Timeout = restOptStd.Timeout, // eredita timeout
|
|
BaseUrl = new Uri(baseUrl) // sovrascrive BaseUrl
|
|
};
|
|
|
|
return new RestClient(opt);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |