ffec555e85
- Completato aggiunta progetto - porting classi servizi/controlli/db a progetto - compilazione OK - manca gestione connString ottimizzata x il DB di origine CORRETTO (LAND/PROG/STATS)
111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace MP.TaskMan.Services
|
|
{
|
|
public class RestCallService
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public RestCallService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
// verifico la url base
|
|
apiUrl = _configuration.GetValue<string>("ServerConf:Prog.ApiUrl");
|
|
// fix opzioni base del RestClient
|
|
restOptStd = new RestClientOptions
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(60),
|
|
BaseUrl = new Uri(apiUrl)
|
|
};
|
|
}
|
|
|
|
private string apiUrl = "";
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Stato server da chiamare x verifiche preliminari stato API REST
|
|
/// </summary>
|
|
/// <param name="ApiUrl"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> CheckServer()
|
|
{
|
|
string answ = "ND";
|
|
// cerco online
|
|
using (RestClient client = new RestClient(restOptStd))
|
|
{
|
|
var request = new RestRequest($"api/health", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
// verifico risposta
|
|
if (response.Content != null)
|
|
{
|
|
answ = response.Content.Replace("\"", "");
|
|
}
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua una generica chiamata ApiRest
|
|
/// </summary>
|
|
/// <param name="ApiUrl">URL Api di base</param>
|
|
/// <param name="ApiRequest">Richiesta completa</param>
|
|
/// <returns></returns>
|
|
public async Task<RestResponse> CallRestGet(string ApiUrl, string ApiRequest)
|
|
{
|
|
RestResponse response;
|
|
// cerco online
|
|
using (RestClient client = new RestClient(ApiUrl))
|
|
{
|
|
var request = new RestRequest(ApiRequest, Method.Get);
|
|
response = await client.GetAsync(request);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration? _configuration;
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#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
|
|
}
|
|
} |