using System;
using System.Threading.Tasks;
using StardewModdingAPI.Toolkit.Framework.Clients.WebApi;
using StardewModdingAPI.Web.Framework.Clients.Chucklefish;
namespace StardewModdingAPI.Web.Framework.ModRepositories
{
/// An HTTP client for fetching mod metadata from the Chucklefish mod site.
internal class ChucklefishRepository : RepositoryBase
{
/*********
** Properties
*********/
/// The underlying HTTP client.
private readonly IChucklefishClient Client;
/*********
** Public methods
*********/
/// Construct an instance.
/// The unique key for this vendor.
/// The underlying HTTP client.
public ChucklefishRepository(string vendorKey, IChucklefishClient client)
: base(vendorKey)
{
this.Client = client;
}
/// Get metadata about a mod in the repository.
/// The mod ID in this repository.
public override async Task GetModInfoAsync(string id)
{
// validate ID format
if (!uint.TryParse(id, out uint realID))
return new ModInfoModel($"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID.");
// fetch info
try
{
var mod = await this.Client.GetModAsync(realID);
if (mod == null)
return new ModInfoModel("Found no mod with this ID.");
// create model
return new ModInfoModel(name: mod.Name, version: this.NormaliseVersion(mod.Version), url: mod.Url);
}
catch (Exception ex)
{
return new ModInfoModel(ex.ToString());
}
}
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public override void Dispose()
{
this.Client.Dispose();
}
}
}