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>
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
<PackageReference Include="Unleash.Client" Version="4.1.9" />
</ItemGroup>
<ItemGroup>