using AIStudio.Tools.Validation;
namespace AIStudio.Tools.PluginSystem;
///
/// Static container for pending API keys during plugin loading.
///
public static class PendingEnterpriseApiKeys
{
private static readonly List PENDING_KEYS = [];
private static readonly Lock LOCK = new();
///
/// Adds a pending API key to the list, unless it cannot be sent.
///
///
/// The whitespace around the key goes, e.g., a line break which came along when somebody copied
/// the key before encrypting it. A key with any other character which an HTTP header cannot carry
/// stays out: stored, it would fail with every request, and nobody on the device could change it.
/// The caller logs that, since it knows which provider of which plugin the key belongs to.
///
/// The pending API key to add.
/// NONE when the key was added, otherwise why it was left out.
public static APIKeyIssue Add(PendingEnterpriseApiKey key)
{
var issue = APIKeyValidation.Check(key.ApiKey);
if (issue.IsProblem)
return issue;
lock (LOCK)
PENDING_KEYS.Add(key with { ApiKey = APIKeyValidation.Normalize(key.ApiKey) });
return APIKeyIssue.NONE;
}
///
/// Gets and clears all pending API keys.
///
/// A list of all pending API keys.
public static IReadOnlyList GetAndClear()
{
lock (LOCK)
{
var keys = PENDING_KEYS.ToList();
PENDING_KEYS.Clear();
return keys;
}
}
}