Skip to content

Commit

Permalink
Refactor Hasheous client key handling (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-j-green authored Oct 18, 2024
1 parent 654f951 commit a9d1cd7
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 18 deletions.
18 changes: 16 additions & 2 deletions TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

HasheousClient.WebApp.HttpHelper.BaseUri = "https://hasheous.org/";
HasheousClient.WebApp.HttpHelper.APIKey = "";
HasheousClient.WebApp.HttpHelper.ClientKey = "";
HasheousClient.WebApp.HttpHelper.ClientKey = "rMnTbIjaCYtfDfCMX3Mes55rgBdrbUMfP2jgW_Nz2nYQdQ8EgHVZR0zfyvbd957x";

HasheousClient.Hasheous hasheous = new HasheousClient.Hasheous();

Expand Down Expand Up @@ -33,7 +33,21 @@
}

Console.WriteLine("Fetching metadata via metadata proxy from Hasheous...");
HasheousClient.Models.Metadata.IGDB.Game metadataProxy = hasheous.GetMetadataProxy<HasheousClient.Models.Metadata.IGDB.Game>(HasheousClient.Hasheous.MetadataProvider.IGDB, 3192);
// HasheousClient.Models.Metadata.IGDB.Game metadataProxy = hasheous.GetMetadataProxy<HasheousClient.Models.Metadata.IGDB.Game>(HasheousClient.Hasheous.MetadataProvider.IGDB, 3192);
// if (metadataProxy != null)
// {
// Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
// {
// Formatting = Newtonsoft.Json.Formatting.Indented,
// NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
// MaxDepth = 8
// };
// jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
// string outString = Newtonsoft.Json.JsonConvert.SerializeObject(metadataProxy, jsonSerializerSettings);
// Console.WriteLine(outString);
// }

Dictionary<string, object> metadataProxy = hasheous.GetMetadataProxy<Dictionary<string, object>>("Game", HasheousClient.Hasheous.MetadataProvider.IGDB, 3192);
if (metadataProxy != null)
{
Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
Expand Down
237 changes: 221 additions & 16 deletions hasheous-client/HasheousClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ namespace HasheousClient
{
public class Hasheous
{
/// <summary>
/// Retrieve a lookup item from Hasheous
/// </summary>
/// <param name="hash">
/// The hash to lookup
/// </param>
/// <returns>
/// The lookup item
/// </returns>
public LookupItemModel RetrieveFromHasheous(HashLookupModel hash)
{
Task<LookupItemModel> result = _RetrieveFromHasheousAsync(hash);
Expand All @@ -18,6 +27,15 @@ private async Task<LookupItemModel> _RetrieveFromHasheousAsync(HashLookupModel h
return result;
}

/// <summary>
/// Fix a match in Hasheous
/// </summary>
/// <param name="model">
/// The model to fix the match
/// </param>
/// <returns>
/// The fixed match model
/// </returns>
public FixMatchModel FixMatch(FixMatchModel model)
{
Task<FixMatchModel> result = _FixMatchAsync(model);
Expand All @@ -32,6 +50,12 @@ private async Task<FixMatchModel> _FixMatchAsync(FixMatchModel model)
return result;
}

/// <summary>
/// Get a list of platforms from Hasheous
/// </summary>
/// <returns>
/// A list of platforms
/// </returns>
public List<DataObjectItem> GetPlatforms()
{
Task<List<DataObjectItem>> result = _GetPlatformsAsync();
Expand Down Expand Up @@ -66,78 +90,259 @@ private async Task<List<DataObjectItem>> _GetPlatformsAsync()
}
}

/// <summary>
/// Access the Hasheous Metadata Proxy
/// </summary>
/// <typeparam name="T">
/// The type of metadata to retrieve
/// </typeparam>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="id">
/// The ID of the metadata to retrieve
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T GetMetadataProxy<T>(MetadataProvider metadataProvider, long id)
{
Task<T> result = _GetMetadataProxy<T>(metadataProvider, id);
string TypeName = typeof(T).Name;

Task<T> result = _GetMetadataProxy<T>(TypeName, metadataProvider, id);

return result.Result;
}

private async Task<T> _GetMetadataProxy<T>(MetadataProvider metadataProvider, long id)
/// <summary>
/// Access the Hasheous Metadata Proxy
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="TypeName">
/// The name of the type of metadata to load
/// </param>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="id">
/// The ID of the metadata to load
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T GetMetadataProxy<T>(string TypeName, MetadataProvider metadataProvider, long id)
{
string TypeName = typeof(T).Name;
Task<T> result = _GetMetadataProxy<T>(TypeName, metadataProvider, id);

return result.Result;
}

private async Task<T> _GetMetadataProxy<T>(string TypeName, MetadataProvider metadataProvider, long id)
{
var result = await HasheousClient.WebApp.HttpHelper.Get<T>($"/api/v1/MetadataProxy/{metadataProvider}/{TypeName}?id={id}");

return result;
}

/// <summary>
/// Access the Hasheous Metadata Proxy
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="slug">
/// The slug of the metadata to load
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T GetMetadataProxy<T>(MetadataProvider metadataProvider, string slug)
{
Task<T> result = _GetMetadataProxy<T>(metadataProvider, slug);
string TypeName = typeof(T).Name;

Task<T> result = _GetMetadataProxy<T>(TypeName, metadataProvider, slug);

return result.Result;
}

private async Task<T> _GetMetadataProxy<T>(MetadataProvider metadataProvider, string slug)
/// <summary>
/// Access the Hasheous Metadata Proxy
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="TypeName">
/// The name of the type of metadata to load
/// </param>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="slug">
/// The slug of the metadata to load
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T GetMetadataProxy<T>(string TypeName, MetadataProvider metadataProvider, string slug)
{
string TypeName = typeof(T).Name;
Task<T> result = _GetMetadataProxy<T>(TypeName, metadataProvider, slug);

return result.Result;
}

private async Task<T> _GetMetadataProxy<T>(string TypeName, MetadataProvider metadataProvider, string slug)
{
var result = await HasheousClient.WebApp.HttpHelper.Get<T>($"/api/v1/MetadataProxy/{metadataProvider}/{TypeName}?slug={slug}");

return result;
}

/// <summary>
/// Search the Hasheous Metadata Proxy for platform
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="search">
/// The search string to use
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T? GetMetadataProxy_SearchPlatform<T>(MetadataProvider metadataProvider, string search)
{
string TypeName = typeof(T).Name.Replace("[]", "");

if (TypeName == "Platform" && metadataProvider == MetadataProvider.IGDB)
if (metadataProvider == MetadataProvider.IGDB)
{
Task<T> result = _GetMetadataProxy_SearchPlatform<T>(metadataProvider, search);
Task<T> result = _GetMetadataProxy_SearchPlatform<T>(TypeName, metadataProvider, search);

return result.Result;
}

return default;
}

private async Task<T> _GetMetadataProxy_SearchPlatform<T>(MetadataProvider metadataProvider, string search)
/// <summary>
/// Search the Hasheous Metadata Proxy for platform
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="TypeName">
/// The name of the type of metadata to load
/// </param>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="search">
/// The search string to use
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T? GetMetadataProxy_SearchPlatform<T>(string TypeName, MetadataProvider metadataProvider, string search)
{
string TypeName = typeof(T).Name.Replace("[]", "");
if (metadataProvider == MetadataProvider.IGDB)
{
Task<T> result = _GetMetadataProxy_SearchPlatform<T>(TypeName, metadataProvider, search);

var result = await HasheousClient.WebApp.HttpHelper.Get<T>($"/api/v1/MetadataProxy/{metadataProvider}/Search/{TypeName}?SearchString={search}");
return result.Result;
}

return result;
return default;
}

private async Task<T> _GetMetadataProxy_SearchPlatform<T>(string TypeName, MetadataProvider metadataProvider, string search)
{
if (TypeName == "Platform")
{
var result = await HasheousClient.WebApp.HttpHelper.Get<T>($"/api/v1/MetadataProxy/{metadataProvider}/Search/{TypeName}?SearchString={search}");

return result;
}
else
{
throw new Exception("Invalid type for search");
}
}

/// <summary>
/// Search the Hasheous Metadata Proxy for game
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="platformId">
/// The ID of the platform to search within
/// </param>
/// <param name="search">
/// The search string to use
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T? GetMetadataProxy_SearchGame<T>(MetadataProvider metadataProvider, string platformId, string search)
{
string TypeName = typeof(T).Name.Replace("[]", "");

if (TypeName == "Game" && metadataProvider == MetadataProvider.IGDB)
if (metadataProvider == MetadataProvider.IGDB)
{
Task<T> result = _GetMetadataProxy_SearchGame<T>(TypeName, metadataProvider, platformId, search);

return result.Result;
}

return default;
}

/// <summary>
/// Search the Hasheous Metadata Proxy for game
/// </summary>
/// <typeparam name="T">
/// The type to load the metadata into
/// </typeparam>
/// <param name="TypeName">
/// The name of the type of metadata to load
/// </param>
/// <param name="metadataProvider">
/// The metadata provider to use
/// </param>
/// <param name="platformId">
/// The ID of the platform to search within
/// </param>
/// <param name="search">
/// The search string to use
/// </param>
/// <returns>
/// The metadata from the provider
/// </returns>
public T? GetMetadataProxy_SearchGame<T>(string TypeName, MetadataProvider metadataProvider, string platformId, string search)
{
if (metadataProvider == MetadataProvider.IGDB)
{
Task<T> result = _GetMetadataProxy_SearchGame<T>(metadataProvider, platformId, search);
Task<T> result = _GetMetadataProxy_SearchGame<T>(TypeName, metadataProvider, platformId, search);

return result.Result;
}

return default;
}

private async Task<T> _GetMetadataProxy_SearchGame<T>(MetadataProvider metadataProvider, string platformId, string search)
private async Task<T> _GetMetadataProxy_SearchGame<T>(string TypeName, MetadataProvider metadataProvider, string platformId, string search)
{
var result = await HasheousClient.WebApp.HttpHelper.Get<T>($"/api/v1/MetadataProxy/{metadataProvider}/Search/Platform/{platformId}/Game?SearchString={search}");
var result = await HasheousClient.WebApp.HttpHelper.Get<T>($"/api/v1/MetadataProxy/{metadataProvider}/Search/Platform/{platformId}/{TypeName}?SearchString={search}");

return result;
}
Expand Down

0 comments on commit a9d1cd7

Please sign in to comment.