64 lines
2.3 KiB
C#
64 lines
2.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MP.INVE.Data;
|
|
using NLog;
|
|
using System.Text.Json;
|
|
|
|
namespace MP.INVE.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
|
|
{
|
|
private readonly IHttpClientFactory _clientFactory;
|
|
private static ILogger<MiDataService> _logger = null!;
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private static string MpIoBaseUrl = "";
|
|
|
|
public IOApiService(IHttpClientFactory clientFactory, IConfiguration configuration, ILogger<MiDataService> logger)
|
|
{
|
|
_logger = logger;
|
|
_logger.LogInformation("Starting IOApiService INIT");
|
|
_configuration = configuration;
|
|
_clientFactory = clientFactory;
|
|
// conf url x chiamate REST
|
|
MpIoBaseUrl = _configuration.GetValue<string>("ServerConf:MpIoBaseUrl");
|
|
}
|
|
/// <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($"Richiesta call per {MpIoBaseUrl}{relUrl}");
|
|
}
|
|
else
|
|
{
|
|
result = "NO";
|
|
Log.Error($"Errore in chaimata | code {response.StatusCode} | {response.Content}");
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|