Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.rest.blockadelabs 1.2.0 #13

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
com.rest.blockadelabs 1.2.0
- updated authentication
- updated unit tests
  • Loading branch information
StephenHodgson committed Nov 29, 2023
commit 1dd0be236360c67098ac49bb31df856f2a82fbe0
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,43 @@

namespace BlockadeLabs
{
public sealed class BlockadeLabsAuthentication : AbstractAuthentication<BlockadeLabsAuthentication, BlockadeLabsAuthInfo>
public sealed class BlockadeLabsAuthentication : AbstractAuthentication<BlockadeLabsAuthentication, BlockadeLabsAuthInfo, BlockadeLabsConfiguration>
{
internal const string CONFIG_FILE = ".blockadelabs";
private const string BLOCKADE_LABS_API_KEY = nameof(BLOCKADE_LABS_API_KEY);

public static implicit operator BlockadeLabsAuthentication(string apiKey) => new BlockadeLabsAuthentication(apiKey);

/// <summary>
/// Instantiates a new Authentication object that will load the default config.
/// Instantiates an empty Authentication object.
/// </summary>
public BlockadeLabsAuthentication()
{
if (cachedDefault != null)
{
return;
}

cachedDefault = (LoadFromAsset<BlockadeLabsConfiguration>() ??
LoadFromDirectory()) ??
LoadFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ??
LoadFromEnvironment();
Info = cachedDefault?.Info;
}
public BlockadeLabsAuthentication() { }

/// <summary>
/// Instantiates a new Authentication object with the given <paramref name="apiKey"/>, which may be <see langword="null"/>.
/// </summary>
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
public BlockadeLabsAuthentication(string apiKey) => Info = new BlockadeLabsAuthInfo(apiKey);
public BlockadeLabsAuthentication(string apiKey)
{
Info = new BlockadeLabsAuthInfo(apiKey);
cachedDefault = this;
}

/// <summary>
/// Instantiates a new Authentication object with the given <paramref name="authInfo"/>, which may be <see langword="null"/>.
/// </summary>
/// <param name="authInfo"></param>
public BlockadeLabsAuthentication(BlockadeLabsAuthInfo authInfo) => Info = authInfo;
public BlockadeLabsAuthentication(BlockadeLabsAuthInfo authInfo)
{
Info = authInfo;
cachedDefault = this;
}

/// <summary>
/// Instantiates a new Authentication object with the given <see cref="configuration"/>.
/// </summary>
/// <param name="configuration"><see cref="BlockadeLabsConfiguration"/>.</param>
public BlockadeLabsAuthentication(BlockadeLabsConfiguration configuration) : this(configuration.ApiKey) { }

/// <inheritdoc />
public override BlockadeLabsAuthInfo Info { get; }
Expand All @@ -56,20 +58,20 @@ public BlockadeLabsAuthentication()
/// </summary>
public static BlockadeLabsAuthentication Default
{
get => cachedDefault ??= new BlockadeLabsAuthentication();
internal set => cachedDefault = value;
get => cachedDefault ??= new BlockadeLabsAuthentication().LoadDefault();
set => cachedDefault = value;
}

/// <inheritdoc />
public override BlockadeLabsAuthentication LoadFromAsset<T>()
=> Resources.LoadAll<T>(string.Empty)
.Where(asset => asset != null)
.Where(asset => asset is BlockadeLabsConfiguration config &&
!string.IsNullOrWhiteSpace(config.ApiKey))
.Select(asset => asset is BlockadeLabsConfiguration config
? new BlockadeLabsAuthentication(config.ApiKey)
: null)
.FirstOrDefault();
public override BlockadeLabsAuthentication LoadFromAsset(BlockadeLabsConfiguration configuration = null)
{
if (configuration == null)
{
Debug.LogWarning($"This can be speed this up by passing a {nameof(BlockadeLabsConfiguration)} to the {nameof(BlockadeLabsAuthentication)}.ctr");
configuration = Resources.LoadAll<BlockadeLabsConfiguration>(string.Empty).FirstOrDefault(o => o != null);
}

return configuration != null ? new BlockadeLabsAuthentication(configuration) : null;
}

/// <inheritdoc />
public override BlockadeLabsAuthentication LoadFromEnvironment()
Expand All @@ -87,6 +89,11 @@ public override BlockadeLabsAuthentication LoadFromDirectory(string directory =
directory = Environment.CurrentDirectory;
}

if (string.IsNullOrWhiteSpace(filename))
{
filename = CONFIG_FILE;
}

BlockadeLabsAuthInfo tempAuthInfo = null;

var currentDirectory = new DirectoryInfo(directory);
Expand Down Expand Up @@ -119,12 +126,11 @@ public override BlockadeLabsAuthentication LoadFromDirectory(string directory =
var part = parts[i];
var nextPart = parts[i + 1];

switch (part)
apiKey = part switch
{
case BLOCKADE_LABS_API_KEY:
apiKey = nextPart.Trim();
break;
}
BLOCKADE_LABS_API_KEY => nextPart.Trim(),
_ => apiKey
};
}
}

Expand All @@ -141,13 +147,7 @@ public override BlockadeLabsAuthentication LoadFromDirectory(string directory =
}
}

if (tempAuthInfo == null ||
string.IsNullOrEmpty(tempAuthInfo.ApiKey))
{
return null;
}

return new BlockadeLabsAuthentication(tempAuthInfo);
return string.IsNullOrEmpty(tempAuthInfo?.ApiKey) ? null : new BlockadeLabsAuthentication(tempAuthInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ namespace BlockadeLabs
{
public sealed class BlockadeLabsClient : BaseClient<BlockadeLabsAuthentication, BlockadeLabsSettings>
{
/// <summary>
/// Creates a new client for the BlockadeLabs API, handling auth and allowing for access to various API endpoints.
/// </summary>
/// <param name="authentication">The API authentication information to use for API calls,
/// or <see langword="null"/> to attempt to use the <see cref="BlockadeLabsAuthentication.Default"/>,
/// potentially loading from environment vars or from a config file.</param>
/// <param name="settings">Optional, <see cref="BlockadeLabsSettings"/> for specifying a proxy domain.</param>
/// <exception cref="AuthenticationException">Raised when authentication details are missing or invalid.</exception>
public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, BlockadeLabsSettings settings = null)
: base(authentication ?? BlockadeLabsAuthentication.Default, settings ?? BlockadeLabsSettings.Default)
{
Expand All @@ -18,6 +26,11 @@ public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, Bloc

protected override void ValidateAuthentication()
{
if (Authentication?.Info == null)
{
throw new InvalidCredentialException($"Invalid {nameof(BlockadeLabsAuthentication)}");
}

if (!HasValidAuthentication)
{
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/RageAgainstThePixel/com.rest.blockadelabs#authentication for details.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,61 @@ namespace BlockadeLabs
{
public sealed class BlockadeLabsSettings : ISettings
{
/// <summary>
/// Creates a new instance of <see cref="BlockadeLabsSettings"/> with default <see cref="BlockadeLabsSettingsInfo"/>.
/// </summary>
public BlockadeLabsSettings()
{
if (cachedDefault != null) { return; }

var config = Resources.LoadAll<BlockadeLabsConfiguration>(string.Empty)
.FirstOrDefault(asset => asset != null);
Info = new BlockadeLabsSettingsInfo();
cachedDefault = this;
}

if (config != null)
/// <summary>
/// Creates a new instance of <see cref="BlockadeLabsSettings"/> with provided <see cref="configuration"/>.
/// </summary>
/// <param name="configuration"><see cref="BlockadeLabsConfiguration"/>.</param>
public BlockadeLabsSettings(BlockadeLabsConfiguration configuration)
{
if (configuration == null)
{
Info = new BlockadeLabsSettingsInfo(config.ProxyDomain);
Default = new BlockadeLabsSettings(Info);
Debug.LogWarning($"This can be speed up by directly passing a {nameof(BlockadeLabsConfiguration)} to the {nameof(BlockadeLabsSettings)}.ctr");
configuration = Resources.LoadAll<BlockadeLabsConfiguration>(string.Empty).FirstOrDefault(asset => asset != null);
}
else

if (configuration == null)
{
Info = new BlockadeLabsSettingsInfo();
Default = new BlockadeLabsSettings(Info);
throw new MissingReferenceException($"Failed to find a valid {nameof(BlockadeLabsConfiguration)}!");
}

Info = new BlockadeLabsSettingsInfo(configuration.ProxyDomain);
cachedDefault = this;
}

/// <summary>
/// Creates a new instance of <see cref="BlockadeLabsSettings"/> with the provided <see cref="settingsInfo"/>.
/// </summary>
/// <param name="settingsInfo"><see cref="BlockadeLabsSettingsInfo"/>.</param>
public BlockadeLabsSettings(BlockadeLabsSettingsInfo settingsInfo)
=> Info = settingsInfo;
{
Info = settingsInfo;
cachedDefault = this;
}

/// <summary>
/// Creates a new instance of <see cref="BlockadeLabsSettings"/>.
/// </summary>
/// <param name="domain">Base api domain.</param>
public BlockadeLabsSettings(string domain)
=> Info = new BlockadeLabsSettingsInfo(domain);
{
Info = new BlockadeLabsSettingsInfo(domain);
cachedDefault = this;
}

private static BlockadeLabsSettings cachedDefault;

public static BlockadeLabsSettings Default
{
get => cachedDefault ??= new BlockadeLabsSettings();
get => cachedDefault ??= new BlockadeLabsSettings(configuration: null);
internal set => cachedDefault = value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace BlockadeLabs.Tests
{
internal abstract class AbstractTestFixture
{
protected readonly BlockadeLabsClient BlockadeLabsClient;

protected AbstractTestFixture()
{
var auth = new BlockadeLabsAuthentication().LoadDefaultsReversed();
var settings = new BlockadeLabsSettings();
BlockadeLabsClient = new BlockadeLabsClient(auth, settings);
//BlockadeLabsClient.EnableDebug = true;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Setup()
[Test]
public void Test_01_GetAuthFromEnv()
{
var auth = BlockadeLabsAuthentication.Default.LoadFromEnvironment();
var auth = new BlockadeLabsAuthentication().LoadFromEnvironment();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.IsNotEmpty(auth.Info.ApiKey);
Expand All @@ -31,7 +31,7 @@ public void Test_01_GetAuthFromEnv()
[Test]
public void Test_02_GetAuthFromFile()
{
var auth = BlockadeLabsAuthentication.Default.LoadFromPath(Path.GetFullPath(BlockadeLabsAuthentication.CONFIG_FILE));
var auth = new BlockadeLabsAuthentication().LoadFromPath(Path.GetFullPath(BlockadeLabsAuthentication.CONFIG_FILE));
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.AreEqual("key-test12", auth.Info.ApiKey);
Expand All @@ -40,7 +40,7 @@ public void Test_02_GetAuthFromFile()
[Test]
public void Test_03_GetAuthFromNonExistentFile()
{
var auth = BlockadeLabsAuthentication.Default.LoadFromDirectory(filename: "bad.config");
var auth = new BlockadeLabsAuthentication().LoadFromDirectory(filename: "bad.config");
Assert.IsNull(auth);
}

Expand All @@ -63,13 +63,14 @@ public void Test_04_GetAuthFromConfiguration()
cleanup = true;
}

var config = AssetDatabase.LoadAssetAtPath<BlockadeLabsConfiguration>(configPath);
var auth = BlockadeLabsAuthentication.Default.LoadFromAsset<BlockadeLabsConfiguration>();
var configuration = AssetDatabase.LoadAssetAtPath<BlockadeLabsConfiguration>(configPath);
Assert.IsNotNull(configuration);
var auth = new BlockadeLabsAuthentication().LoadFromAsset(configuration);

Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.IsNotEmpty(auth.Info.ApiKey);
Assert.AreEqual(auth.Info.ApiKey, config.ApiKey);
Assert.AreEqual(auth.Info.ApiKey, configuration.ApiKey);

if (cleanup)
{
Expand All @@ -82,13 +83,12 @@ public void Test_04_GetAuthFromConfiguration()
public void Test_05_Authentication()
{
var defaultAuth = BlockadeLabsAuthentication.Default;
var manualAuth = new BlockadeLabsAuthentication("key-testAA");

Assert.IsNotNull(defaultAuth);
Assert.IsNotNull(defaultAuth.Info.ApiKey);
Assert.AreEqual(defaultAuth.Info.ApiKey, BlockadeLabsAuthentication.Default.Info.ApiKey);

BlockadeLabsAuthentication.Default = new BlockadeLabsAuthentication("key-testAA");
var manualAuth = new BlockadeLabsAuthentication("key-testAA");
Assert.IsNotNull(manualAuth);
Assert.IsNotNull(manualAuth.Info.ApiKey);
Assert.AreEqual(manualAuth.Info.ApiKey, BlockadeLabsAuthentication.Default.Info.ApiKey);
Expand Down Expand Up @@ -154,6 +154,10 @@ public void TearDown()
{
File.Delete(BlockadeLabsAuthentication.CONFIG_FILE);
}


BlockadeLabsSettings.Default = null;
BlockadeLabsAuthentication.Default = null;
}
}
}
Loading