unleash api via modpack config

This commit is contained in:
2024-07-10 18:24:49 +02:00
parent 1bd0e87211
commit d1f35676ee
7 changed files with 83 additions and 84 deletions

View File

@@ -0,0 +1,73 @@
using ModpackUpdater.Model;
using Unleash;
namespace ModpackUpdater;
public class ModpackFeatures(ModpackConfig modpackConfig)
{
private IUnleash api;
private UnleashContext context;
private UnleashSettings settings;
public static string FeatureAllowExtas => "allow-extras";
~ModpackFeatures()
{
api?.Dispose();
}
public bool IsEnabled(string feature)
{
return IsEnabled(feature, null);
}
public bool IsEnabled(string feature, AppFeatureContext context)
{
return CheckFeature(feature, context);
}
private bool InitializeApi()
{
if (api == null)
{
settings = new UnleashSettings
{
AppName = "Modpack Updater",
UnleashApi = new Uri(modpackConfig.UnleashApiUrl),
FetchTogglesInterval = TimeSpan.FromSeconds(60 * 5),
InstanceTag = modpackConfig.UnleashInstanceId,
};
api = new DefaultUnleash(settings);
}
return api != null;
}
private bool CheckFeature(string name, AppFeatureContext context)
{
return InitializeApi() && api.IsEnabled(name, GetContext(context));
}
private UnleashContext GetContext(AppFeatureContext ccontext)
{
context ??= new();
context.CurrentTime = DateTime.Now;
ccontext?.Apply(context);
return context;
}
}
public abstract class AppFeatureContext
{
public abstract void Apply(UnleashContext context);
}
public class AllowExtrasFeatureContext(ModpackInfo info, ModpackConfig config) : AppFeatureContext
{
public override void Apply(UnleashContext context)
{
context.UserId = info.ExtrasKey;
context.Environment = config.Key;
}
}

View File

@@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" /> <PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
<PackageReference Include="Unleash.Client" Version="4.1.9" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -9,6 +9,8 @@ public class ModpackConfig
public string Key { get; set; } public string Key { get; set; }
public string UpdateUrl { get; set; } public string UpdateUrl { get; set; }
public string InstallUrl { get; set; } public string InstallUrl { get; set; }
public string UnleashApiUrl { get; set; }
public string UnleashInstanceId { get; set; }
[JsonIgnore] [JsonIgnore]
public string ConfigUrl { get; set; } public string ConfigUrl { get; set; }

View File

@@ -1,81 +0,0 @@
using ModpackUpdater.Model;
using Unleash;
namespace ModpackUpdater;
public enum AppFeatures
{
AllowExtras
}
public static class AppFeaturesExtensions
{
private const string apiUrl = "https://git.pilzinsel64.de/api/v4/feature_flags/unleash/2";
private const string instanceId = "glffct-3vCzJXChAnxjsgvoHijR";
private static IUnleash api;
private static UnleashContext context;
private static UnleashSettings settings;
public static bool IsEnabled(this AppFeatures feature, AppFeatureContext context)
{
return feature switch
{
AppFeatures.AllowExtras => CheckFeature("allow-extras", false, context),
_ => throw new NotSupportedException(),
};
}
public static bool IsEnabled(this AppFeatures feature)
{
return feature switch
{
_ => throw new NotSupportedException(),
};
}
private static bool InitializeApi()
{
if (api == null)
{
settings = new UnleashSettings
{
AppName = "Modpack Updater",
UnleashApi = new Uri(apiUrl),
FetchTogglesInterval = TimeSpan.FromSeconds(60 * 5),
InstanceTag = instanceId,
};
api = new DefaultUnleash(settings);
}
return api != null;
}
private static bool CheckFeature(string name, bool defaultValue, AppFeatureContext context)
{
return InitializeApi() && api.IsEnabled(name, GetContext(context), defaultValue);
}
private static UnleashContext GetContext(AppFeatureContext ccontext)
{
context ??= new();
context.CurrentTime = DateTime.Now;
ccontext?.Apply(context);
return context;
}
}
public abstract class AppFeatureContext
{
public abstract void Apply(UnleashContext context);
}
public class AllowExtrasFeatureContext(ModpackInfo info, ModpackConfig config) : AppFeatureContext
{
public override void Apply(UnleashContext context)
{
context.UserId = info.ExtrasKey;
context.Environment = config.Key;
}
}

View File

@@ -13,6 +13,7 @@ public partial class Form1
{ {
private ModpackInfo modpackInfo = new(); private ModpackInfo modpackInfo = new();
private ModpackConfig updateConfig = new(); private ModpackConfig updateConfig = new();
private ModpackFeatures features;
private bool currentUpdating = false; private bool currentUpdating = false;
private UpdateCheckResult lastUpdateCheckResult = null; private UpdateCheckResult lastUpdateCheckResult = null;
private readonly UpdateCheckOptionsAdv updateOptions; private readonly UpdateCheckOptionsAdv updateOptions;
@@ -101,6 +102,9 @@ public partial class Form1
{ {
} }
if (modpackInfo != null)
features = new(updateConfig);
if (modpackInfo == null || string.IsNullOrWhiteSpace(RadTextBoxControl_MinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/) if (modpackInfo == null || string.IsNullOrWhiteSpace(RadTextBoxControl_MinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/)
{ {
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
@@ -168,7 +172,7 @@ public partial class Form1
SetStatus(LangRes.StatusText_CheckingForUpdates, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small)); SetStatus(LangRes.StatusText_CheckingForUpdates, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small));
// Check for extras once again // Check for extras once again
updateOptions.IncludeExtras = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(modpackInfo, updateConfig)); updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(modpackInfo, updateConfig));
try try
{ {

View File

@@ -54,7 +54,6 @@
<PackageReference Include="Pilz.UI.Telerik.SymbolFactory" Version="2.0.3" /> <PackageReference Include="Pilz.UI.Telerik.SymbolFactory" Version="2.0.3" />
<PackageReference Include="Pilz.Win32" Version="2.0.0" /> <PackageReference Include="Pilz.Win32" Version="2.0.0" />
<PackageReference Include="UI.for.WinForms.Common" Version="2024.2.514" /> <PackageReference Include="UI.for.WinForms.Common" Version="2024.2.514" />
<PackageReference Include="Unleash.Client" Version="4.1.9" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -81,12 +81,13 @@ public static class Program
{ {
var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder); var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder);
var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig, info)); var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig, info));
var features = new ModpackFeatures(config);
// Check features // Check features
if (!string.IsNullOrWhiteSpace(updateOptions.ExtrasKey)) if (!string.IsNullOrWhiteSpace(updateOptions.ExtrasKey))
info.ExtrasKey = updateOptions.ExtrasKey; info.ExtrasKey = updateOptions.ExtrasKey;
if (!string.IsNullOrWhiteSpace(info.ExtrasKey)) if (!string.IsNullOrWhiteSpace(info.ExtrasKey))
updateOptions.IncludeExtras = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(info, config)); updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(info, config));
// Check for update // Check for update
var installer = new ModpackInstaller(config, info); var installer = new ModpackInstaller(config, info);