From d1f35676eefd296b10ff0dbd0bc43c75df7d8687 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Wed, 10 Jul 2024 18:24:49 +0200 Subject: [PATCH] unleash api via modpack config --- ModpackUpdater.Manager/ModpackFeatures.cs | 73 +++++++++++++++++ .../ModpackUpdater.Manager.csproj | 1 + ModpackUpdater.Model/ModpackConfig.cs | 2 + ModpackUpdater/AppFeatures.cs | 81 ------------------- ModpackUpdater/Form1.cs | 6 +- ModpackUpdater/ModpackUpdater.csproj | 1 - ModpackUpdater/Program.cs | 3 +- 7 files changed, 83 insertions(+), 84 deletions(-) create mode 100644 ModpackUpdater.Manager/ModpackFeatures.cs delete mode 100644 ModpackUpdater/AppFeatures.cs diff --git a/ModpackUpdater.Manager/ModpackFeatures.cs b/ModpackUpdater.Manager/ModpackFeatures.cs new file mode 100644 index 0000000..3e9e2df --- /dev/null +++ b/ModpackUpdater.Manager/ModpackFeatures.cs @@ -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; + } +} \ No newline at end of file diff --git a/ModpackUpdater.Manager/ModpackUpdater.Manager.csproj b/ModpackUpdater.Manager/ModpackUpdater.Manager.csproj index 5fc9312..73c3014 100644 --- a/ModpackUpdater.Manager/ModpackUpdater.Manager.csproj +++ b/ModpackUpdater.Manager/ModpackUpdater.Manager.csproj @@ -7,6 +7,7 @@ + diff --git a/ModpackUpdater.Model/ModpackConfig.cs b/ModpackUpdater.Model/ModpackConfig.cs index 1040a64..3d65607 100644 --- a/ModpackUpdater.Model/ModpackConfig.cs +++ b/ModpackUpdater.Model/ModpackConfig.cs @@ -9,6 +9,8 @@ public class ModpackConfig public string Key { get; set; } public string UpdateUrl { get; set; } public string InstallUrl { get; set; } + public string UnleashApiUrl { get; set; } + public string UnleashInstanceId { get; set; } [JsonIgnore] public string ConfigUrl { get; set; } diff --git a/ModpackUpdater/AppFeatures.cs b/ModpackUpdater/AppFeatures.cs deleted file mode 100644 index 2f138c4..0000000 --- a/ModpackUpdater/AppFeatures.cs +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/ModpackUpdater/Form1.cs b/ModpackUpdater/Form1.cs index 60c1460..8d10c7c 100644 --- a/ModpackUpdater/Form1.cs +++ b/ModpackUpdater/Form1.cs @@ -13,6 +13,7 @@ public partial class Form1 { private ModpackInfo modpackInfo = new(); private ModpackConfig updateConfig = new(); + private ModpackFeatures features; private bool currentUpdating = false; private UpdateCheckResult lastUpdateCheckResult = null; 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*/) { 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)); // 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 { diff --git a/ModpackUpdater/ModpackUpdater.csproj b/ModpackUpdater/ModpackUpdater.csproj index 61214ba..7ddd905 100644 --- a/ModpackUpdater/ModpackUpdater.csproj +++ b/ModpackUpdater/ModpackUpdater.csproj @@ -54,7 +54,6 @@ - diff --git a/ModpackUpdater/Program.cs b/ModpackUpdater/Program.cs index 0ab084d..5186d6d 100644 --- a/ModpackUpdater/Program.cs +++ b/ModpackUpdater/Program.cs @@ -81,12 +81,13 @@ public static class Program { var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder); var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig, info)); + var features = new ModpackFeatures(config); // Check features if (!string.IsNullOrWhiteSpace(updateOptions.ExtrasKey)) info.ExtrasKey = updateOptions.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 var installer = new ModpackInstaller(config, info);