91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using IOB_UT_NEXT.Config;
|
|
using IOB_UT_NEXT.Services.Cache;
|
|
using IOB_UT_NEXT.Services.Networking;
|
|
using NLog;
|
|
|
|
namespace IOB_UT_NEXT.Services.Networking
|
|
{
|
|
/// <summary>
|
|
/// Orchestratore delle comunicazioni.
|
|
/// Coordina HttpService e RedisIobCache per eseguire workflow di business.
|
|
/// Riduce la profondità dello stack in Generic.cs.
|
|
/// </summary>
|
|
public class CommunicationService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public CommunicationService(IobConfTree config, RedisIobCache redisMan)
|
|
{
|
|
_config = config;
|
|
_redisMan = redisMan;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Esegue una chiamata HTTP e salva il risultato direttamente su Redis (Workflow orchestrato).
|
|
/// </summary>
|
|
public async Task<string> CallAndSaveToRedisAsync(string url, string redisKey)
|
|
{
|
|
try
|
|
{
|
|
string result = await HttpService.CallUrlAsync(url);
|
|
if (!string.IsNullOrEmpty(result))
|
|
{
|
|
_redisMan.setRSV(redisKey, result);
|
|
}
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error(ex, $"Error in CallAndSaveToRedisAsync: URL={url}, Key={redisKey}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera dati da una URL e li deserializza (Workflow orchestrato).
|
|
/// </summary>
|
|
public async Task<T> GetAndDeserializeAsync<T>(string url)
|
|
{
|
|
string raw = await HttpService.CallUrlAsync(url);
|
|
return IOB_UT_NEXT.Services.Data.DataSerializer.Deserialize<T>(raw);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue una chiamata POST e salva il payload/risposta (Workflow orchestrato).
|
|
/// </summary>
|
|
public async Task<string> PostAndStoreAsync(string url, string payload, string redisKey)
|
|
{
|
|
try
|
|
{
|
|
string response = await Task.Run(() => HttpService.CallUrlPost(url, payload));
|
|
if (!string.IsNullOrEmpty(response))
|
|
{
|
|
_redisMan.setRSV(redisKey, response);
|
|
}
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error(ex, $"Error in PostAndStoreAsync: URL={url}, Key={redisKey}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
private readonly IobConfTree _config;
|
|
private readonly RedisIobCache _redisMan;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |