2026-02-18 10:43:27 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2026-02-12 15:24:25 -05:00
|
|
|
using Microsoft.Extensions.Options;
|
2026-02-18 10:43:27 -05:00
|
|
|
using OTSSignsOrchestrator.Core.Configuration;
|
2026-02-12 15:24:25 -05:00
|
|
|
|
2026-02-18 10:43:27 -05:00
|
|
|
namespace OTSSignsOrchestrator.Core.Services;
|
2026-02-12 15:24:25 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
2026-02-18 10:43:27 -05:00
|
|
|
/// Tests connectivity to deployed Xibo CMS instances using OAuth2.
|
2026-02-12 15:24:25 -05:00
|
|
|
/// </summary>
|
|
|
|
|
public class XiboApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
private readonly XiboOptions _options;
|
|
|
|
|
private readonly ILogger<XiboApiService> _logger;
|
|
|
|
|
|
|
|
|
|
public XiboApiService(
|
|
|
|
|
IHttpClientFactory httpClientFactory,
|
|
|
|
|
IOptions<XiboOptions> options,
|
|
|
|
|
ILogger<XiboApiService> logger)
|
|
|
|
|
{
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
_options = options.Value;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<XiboTestResult> TestConnectionAsync(string instanceUrl, string username, string password)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Testing Xibo connection to {InstanceUrl}", instanceUrl);
|
|
|
|
|
|
|
|
|
|
var client = _httpClientFactory.CreateClient("XiboApi");
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(_options.TestConnectionTimeoutSeconds);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var baseUrl = instanceUrl.TrimEnd('/');
|
|
|
|
|
var tokenUrl = $"{baseUrl}/api/authorize/access_token";
|
|
|
|
|
|
|
|
|
|
var formContent = new FormUrlEncodedContent(new[]
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<string, string>("grant_type", "client_credentials"),
|
|
|
|
|
new KeyValuePair<string, string>("client_id", username),
|
|
|
|
|
new KeyValuePair<string, string>("client_secret", password)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await client.PostAsync(tokenUrl, formContent);
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Xibo connection test succeeded for {InstanceUrl}", instanceUrl);
|
|
|
|
|
return new XiboTestResult
|
|
|
|
|
{
|
|
|
|
|
IsValid = true,
|
|
|
|
|
Message = "Connected successfully.",
|
|
|
|
|
HttpStatus = (int)response.StatusCode
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning("Xibo connection test failed: {InstanceUrl} | status={StatusCode}",
|
|
|
|
|
instanceUrl, (int)response.StatusCode);
|
|
|
|
|
|
|
|
|
|
return new XiboTestResult
|
|
|
|
|
{
|
|
|
|
|
IsValid = false,
|
|
|
|
|
Message = response.StatusCode switch
|
|
|
|
|
{
|
|
|
|
|
System.Net.HttpStatusCode.Unauthorized => "Invalid Xibo credentials.",
|
|
|
|
|
System.Net.HttpStatusCode.Forbidden => "User lacks API permissions.",
|
|
|
|
|
System.Net.HttpStatusCode.ServiceUnavailable => "Xibo instance not ready.",
|
|
|
|
|
_ => $"Unexpected response: {(int)response.StatusCode}"
|
|
|
|
|
},
|
|
|
|
|
HttpStatus = (int)response.StatusCode
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
2026-02-18 10:43:27 -05:00
|
|
|
return new XiboTestResult { IsValid = false, Message = "Connection timed out." };
|
2026-02-12 15:24:25 -05:00
|
|
|
}
|
|
|
|
|
catch (HttpRequestException ex)
|
|
|
|
|
{
|
2026-02-18 10:43:27 -05:00
|
|
|
return new XiboTestResult { IsValid = false, Message = $"Cannot reach Xibo instance: {ex.Message}" };
|
2026-02-12 15:24:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class XiboTestResult
|
|
|
|
|
{
|
|
|
|
|
public bool IsValid { get; set; }
|
|
|
|
|
public string Message { get; set; } = string.Empty;
|
|
|
|
|
public int HttpStatus { get; set; }
|
|
|
|
|
}
|