Some checks failed
Build and Publish Docker Image / build-and-push (push) Has been cancelled
- Implemented CreateInstanceView for creating new instances. - Added HostsView for managing SSH hosts with CRUD operations. - Created InstancesView for displaying and managing instances. - Developed LogsView for viewing operation logs. - Introduced SecretsView for managing secrets associated with hosts. - Established SettingsView for configuring application settings. - Created MainWindow as the main application window with navigation. - Added app manifest and configuration files for logging and settings.
91 lines
3.2 KiB
C#
91 lines
3.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using OTSSignsOrchestrator.Core.Configuration;
|
|
|
|
namespace OTSSignsOrchestrator.Core.Services;
|
|
|
|
/// <summary>
|
|
/// Tests connectivity to deployed Xibo CMS instances using OAuth2.
|
|
/// </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)
|
|
{
|
|
return new XiboTestResult { IsValid = false, Message = "Connection timed out." };
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
return new XiboTestResult { IsValid = false, Message = $"Cannot reach Xibo instance: {ex.Message}" };
|
|
}
|
|
}
|
|
}
|
|
|
|
public class XiboTestResult
|
|
{
|
|
public bool IsValid { get; set; }
|
|
public string Message { get; set; } = string.Empty;
|
|
public int HttpStatus { get; set; }
|
|
}
|