unleash api via modpack config
This commit is contained in:
73
ModpackUpdater.Manager/ModpackFeatures.cs
Normal file
73
ModpackUpdater.Manager/ModpackFeatures.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user