Add ETag handling in enterprise environment logic

This commit is contained in:
Thorsten Sommer 2025-06-02 15:58:47 +02:00
parent 4ced06473b
commit d10f2af9f7
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5
3 changed files with 25 additions and 2 deletions

View File

@ -1,6 +1,8 @@
using System.Net.Http.Headers;
namespace AIStudio.Tools; namespace AIStudio.Tools;
public readonly record struct EnterpriseEnvironment(string ConfigurationServerUrl, Guid ConfigurationId) public readonly record struct EnterpriseEnvironment(string ConfigurationServerUrl, Guid ConfigurationId, EntityTagHeaderValue? ETag)
{ {
public bool IsActive => !string.IsNullOrEmpty(this.ConfigurationServerUrl) && this.ConfigurationId != Guid.Empty; public bool IsActive => !string.IsNullOrEmpty(this.ConfigurationServerUrl) && this.ConfigurationId != Guid.Empty;
} }

View File

@ -1,9 +1,29 @@
using System.IO.Compression; using System.IO.Compression;
using System.Net.Http.Headers;
namespace AIStudio.Tools.PluginSystem; namespace AIStudio.Tools.PluginSystem;
public static partial class PluginFactory public static partial class PluginFactory
{ {
public static async Task<EntityTagHeaderValue?> DetermineConfigPluginETagAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
{
try
{
var serverUrl = configServerUrl.EndsWith('/') ? configServerUrl[..^1] : configServerUrl;
var downloadUrl = $"{serverUrl}/{configPlugId}.zip";
using var http = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
var response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
return response.Headers.ETag;
}
catch (Exception e)
{
LOG.LogError(e, "An error occurred while determining the ETag for the configuration plugin.");
return null;
}
}
public static async Task<bool> TryDownloadingConfigPluginAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default) public static async Task<bool> TryDownloadingConfigPluginAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
{ {
if(!IS_INITIALIZED) if(!IS_INITIALIZED)

View File

@ -40,7 +40,8 @@ public sealed class EnterpriseEnvironmentService(ILogger<EnterpriseEnvironmentSe
var enterpriseConfigServerUrl = await rustService.EnterpriseEnvConfigServerUrl(); var enterpriseConfigServerUrl = await rustService.EnterpriseEnvConfigServerUrl();
var enterpriseConfigId = await rustService.EnterpriseEnvConfigId(); var enterpriseConfigId = await rustService.EnterpriseEnvConfigId();
var nextEnterpriseEnvironment = new EnterpriseEnvironment(enterpriseConfigServerUrl, enterpriseConfigId); var etag = await PluginFactory.DetermineConfigPluginETagAsync(enterpriseConfigId, enterpriseConfigServerUrl);
var nextEnterpriseEnvironment = new EnterpriseEnvironment(enterpriseConfigServerUrl, enterpriseConfigId, etag);
if (CURRENT_ENVIRONMENT != nextEnterpriseEnvironment) if (CURRENT_ENVIRONMENT != nextEnterpriseEnvironment)
{ {
logger.LogInformation("The enterprise environment has changed. Updating the current environment."); logger.LogInformation("The enterprise environment has changed. Updating the current environment.");