Files
2024-12-23 15:41:12 +01:00

62 lines
1.6 KiB
C#

using RestSharp;
public class RestCaller
{
/// <summary>
/// Classe gestione chiamate REST
/// </summary>
/// <param name="ApiUrl"></param>
/// <param name="TimeOutSec"></param>
public RestCaller(string ApiUrl, int TimeOutSec)
{
tOut = TimeOutSec;
apiUrl = ApiUrl;
restOptStd = new RestClientOptions
{
Timeout = TimeSpan.FromSeconds(tOut),
BaseUrl = new Uri(apiUrl)
};
}
/// <summary>
/// Esecuzione chiamata Rest tipo GET
/// </summary>
/// <param name="resource"></param>
/// <returns></returns>
public string ExecuteCallGet(string resource)
{
string answ = "";
if (!string.IsNullOrEmpty(resource))
{
// client chiamate rest
using (var client = new RestClient(restOptStd))
{
var currReq = new RestRequest(resource, Method.Get);
// currReq.AddHeader("Content-type", "application/xml");
currReq.AddHeader("Content-type", "application/json");
// effettuo vera chiamata
var currResp = client.Get(currReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
answ = $"{currResp.Content}";
}
}
}
return answ;
}
protected string apiUrl = "";
protected int tOut = 60;
/// <summary>
/// Conf client RestSharp standard:
/// - timeout 1 min
/// </summary>
private RestClientOptions restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(60) };
}