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.0.0-preview.1 #1

Merged
merged 18 commits into from
Jun 10, 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
Prev Previous commit
Next Next commit
first pass implementation
  • Loading branch information
StephenHodgson committed May 28, 2023
commit d06bd96e97e0c72b9c427e84e103eeefd2cfd16a

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
@@ -1,8 +1,10 @@
{
"name": "Rest.BlockadeLabs.Editor",
"rootNamespace": "Rest.BlockadeLabs.Editor",
"name": "BlockadeLabs.Editor",
"rootNamespace": "BlockadeLabs.Editor",
"references": [
"Rest.BlockadeLabs"
"GUID:0e64303a4898a4d4ba94306e88e6c025",
"GUID:7958db66189566541a6363568aee1575",
"GUID:28b599e4adde9c94a9b9667d7b08fe83"
],
"includePlatforms": [
"Editor"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Rest.BlockadeLabs;
using System;
using UnityEditor;
using Utilities.Rest.Editor;

namespace BlockadeLabs.Editor
{
[CustomEditor(typeof(BlockadeLabsConfiguration))]
public class BlockadeLabsConfigurationInspector : BaseConfigurationInspector<BlockadeLabsConfiguration>
{
private static bool triggerReload;

private SerializedProperty apiKey;
private SerializedProperty proxyDomain;

#region Project Settings Window

[SettingsProvider]
private static SettingsProvider Preferences()
=> GetSettingsProvider(nameof(BlockadeLabs), CheckReload);

#endregion Project Settings Window

#region Inspector Window

private void OnEnable()
{
GetOrCreateInstance(target);

try
{
apiKey = serializedObject.FindProperty(nameof(apiKey));
proxyDomain = serializedObject.FindProperty(nameof(proxyDomain));
}
catch (Exception)
{
// Ignored
}
}

private void OnDisable() => CheckReload();

public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.Space();
EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck();

EditorGUILayout.PropertyField(apiKey);
EditorGUILayout.PropertyField(proxyDomain);

if (EditorGUI.EndChangeCheck())
{
triggerReload = true;
}

EditorGUI.indentLevel--;
serializedObject.ApplyModifiedProperties();
}

#endregion Inspector Window

private static void CheckReload()
{
if (triggerReload)
{
triggerReload = false;
EditorUtility.RequestScriptReload();
}
}
}
}

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

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
@@ -1,7 +1,10 @@
{
"name": "Rest.BlockadeLabs",
"rootNamespace": "Rest.BlockadeLabs",
"references": [],
"name": "BlockadeLabs",
"rootNamespace": "BlockadeLabs",
"references": [
"GUID:a6609af893242c7438d701ddd4cce46a",
"GUID:7958db66189566541a6363568aee1575"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Security.Authentication;
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
{
[Serializable]
public class BlockadeLabsAuthInfo : IAuthInfo
{
public BlockadeLabsAuthInfo(string apiKey)
{
if (string.IsNullOrWhiteSpace(apiKey))
{
throw new InvalidCredentialException(nameof(apiKey));
}

this.apiKey = apiKey;
}

[SerializeField]
private string apiKey;

/// <summary>
/// The API key, required to access the service.
/// </summary>
public string ApiKey => apiKey;
}
}

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
@@ -0,0 +1,145 @@
using System;
using System.IO;
using System.Linq;
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
{
public sealed class BlockadeLabsAuthentication : AbstractAuthentication<BlockadeLabsAuthentication, BlockadeLabsAuthInfo>
{
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.
/// </summary>
public BlockadeLabsAuthentication()
=> cachedDefault ??= (LoadFromAsset<BlockadeLabsConfiguration>() ??
LoadFromDirectory()) ??
LoadFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ??
LoadFromEnvironment();

/// <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) => authInfo = new BlockadeLabsAuthInfo(apiKey);

/// <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) => this.authInfo = authInfo;

public readonly BlockadeLabsAuthInfo authInfo;

/// <inheritdoc />
public override BlockadeLabsAuthInfo Info => authInfo ?? Default.Info;

private static BlockadeLabsAuthentication cachedDefault;

/// <summary>
/// The default authentication to use when no other auth is specified.
/// This can be set manually, or automatically loaded via environment variables or a config file.
/// <seealso cref="LoadFromEnvironment"/><seealso cref="LoadFromDirectory"/>
/// </summary>
public static BlockadeLabsAuthentication Default
{
get => cachedDefault ?? new BlockadeLabsAuthentication();
internal 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();

/// <inheritdoc />
public override BlockadeLabsAuthentication LoadFromEnvironment()
{
var apiKey = Environment.GetEnvironmentVariable(BLOCKADE_LABS_API_KEY);
return string.IsNullOrEmpty(apiKey) ? null : new BlockadeLabsAuthentication(apiKey);
}

/// <inheritdoc />
/// ReSharper disable once OptionalParameterHierarchyMismatch
public override BlockadeLabsAuthentication LoadFromDirectory(string directory = null, string filename = CONFIG_FILE, bool searchUp = true)
{
if (string.IsNullOrWhiteSpace(directory))
{
directory = Environment.CurrentDirectory;
}

BlockadeLabsAuthInfo tempAuthInfo = null;

var currentDirectory = new DirectoryInfo(directory);

while (tempAuthInfo == null && currentDirectory.Parent != null)
{
var filePath = Path.Combine(currentDirectory.FullName, filename);

if (File.Exists(filePath))
{
try
{
tempAuthInfo = JsonUtility.FromJson<BlockadeLabsAuthInfo>(File.ReadAllText(filePath));
break;
}
catch (Exception)
{
// try to parse the old way for backwards support.
}

var lines = File.ReadAllLines(filePath);
string apiKey = null;

foreach (var line in lines)
{
var parts = line.Split('=', ':');

for (var i = 0; i < parts.Length - 1; i++)
{
var part = parts[i];
var nextPart = parts[i + 1];

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

tempAuthInfo = new BlockadeLabsAuthInfo(apiKey);
}

if (searchUp)
{
currentDirectory = currentDirectory.Parent;
}
else
{
break;
}
}

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

return new BlockadeLabsAuthentication(tempAuthInfo);
}
}
}

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
@@ -0,0 +1,9 @@
using Utilities.WebRequestRest;

namespace Rest.BlockadeLabs
{
public abstract class BlockadeLabsBaseEndpoint : BaseEndPoint<BlockadeLabsClient, BlockadeLabsAuthentication, BlockadeLabsSettings>
{
protected BlockadeLabsBaseEndpoint(BlockadeLabsClient client) : base(client) { }
}
}

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
@@ -0,0 +1,38 @@
using Newtonsoft.Json;
using System.Net.Http;
using System.Security.Authentication;
using Utilities.WebRequestRest;

namespace Rest.BlockadeLabs
{
public class BlockadeLabsClient : BaseClient<BlockadeLabsAuthentication, BlockadeLabsSettings>
{
public BlockadeLabsClient(BlockadeLabsAuthentication authentication = null, BlockadeLabsSettings settings = null, HttpClient httpClient = null)
: base(authentication ?? BlockadeLabsAuthentication.Default, settings ?? BlockadeLabsSettings.Default, httpClient)
{
JsonSerializationOptions = new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore
};
}

protected override HttpClient SetupClient(HttpClient httpClient = null)
{
httpClient ??= new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "com.rest.blockadelabs");
return httpClient;
}

protected override void ValidateAuthentication()
{
if (!HasValidAuthentication)
{
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/RageAgainstThePixel/com.rest.blockadelabs#authentication for details.");
}
}

public override bool HasValidAuthentication => !string.IsNullOrWhiteSpace(Authentication.Info.ApiKey);

internal JsonSerializerSettings JsonSerializationOptions { get; }
}
}
Loading