800cbaaec4
- ok force sync - ok chiusura ODL
99 lines
3.5 KiB
C#
99 lines
3.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MP.SPEC.Data;
|
|
using NLog;
|
|
using System.Text.Json;
|
|
|
|
namespace MP.SPEC.Services
|
|
{
|
|
/// <summary>
|
|
/// Classe per chiamare metodi da MP-IO
|
|
///
|
|
/// rif: https://www.ezzylearning.net/tutorial/making-http-requests-in-blazor-server-apps https://wellsb.com/csharp/aspnet/blazor-httpclientfactory-and-web-api/
|
|
/// </summary>
|
|
public class IOApiService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public IOApiService(IHttpClientFactory clientFactory, IConfiguration configuration, ILogger<MpDataService> logger)
|
|
{
|
|
_logger = logger;
|
|
_logger.LogInformation("Starting IOApiService INIT");
|
|
_configuration = configuration;
|
|
_clientFactory = clientFactory;
|
|
// conf url x chiamate REST
|
|
MpIoBaseUrl = _configuration.GetValue<string>("ServerConf:MpIoBaseUrl");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Chiama metodo x registrare esecuzione task da IOB
|
|
/// </summary>
|
|
/// <param name="selRec"></param>
|
|
/// <returns></returns>
|
|
public async Task addTask2Exe(string idxMacc, string taskName, string taskVal)
|
|
{
|
|
// compongo URL e chiamo
|
|
string restUrl = $"IOB/addTask2Exe/{idxMacc}?taskName={taskName}&taskVal={taskVal}";
|
|
try
|
|
{
|
|
var response = await callMpIoUrlGet(restUrl);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore durante chiamata addTask2Exe:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Chiama metodo x chiedere sync DB
|
|
/// </summary>
|
|
/// <param name="IdxMacc"></param>
|
|
/// <returns></returns>
|
|
public async Task callSyncDb(string IdxMacc)
|
|
{
|
|
// chiamo aggiunta task SyncDb...
|
|
await addTask2Exe(IdxMacc, "syncDbData", "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua chiamata ad MP-IO
|
|
/// </summary>
|
|
/// <param name="relUrl">URL metodo relativo alla base path di MP-IO</param>
|
|
/// <returns></returns>
|
|
public async Task<string> callMpIoUrlGet(string relUrl)
|
|
{
|
|
string result = "";
|
|
var request = new HttpRequestMessage(HttpMethod.Get, $"{MpIoBaseUrl}{relUrl}");
|
|
request.Headers.Add("Accept", "application/vnd.github.v3+json");
|
|
var client = _clientFactory.CreateClient();
|
|
Log.Info($"Richiesta call per {MpIoBaseUrl}{relUrl}");
|
|
var response = await client.SendAsync(request);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var stringResponse = await response.Content.ReadAsStringAsync();
|
|
result = stringResponse;
|
|
Log.Info($"Effettuata coin successo call per {MpIoBaseUrl}{relUrl}");
|
|
}
|
|
else
|
|
{
|
|
result = "NO";
|
|
Log.Error($"Errore in chaimata | code {response.StatusCode} | {response.Content}");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
private static ILogger<MpDataService> _logger = null!;
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
private static string MpIoBaseUrl = "";
|
|
private readonly IHttpClientFactory _clientFactory;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |