using System;
using System.Threading.Tasks;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Web.Framework.Clients.GitHub;
namespace StardewModdingAPI.Web.Framework.ModRepositories
{
/// An HTTP client for fetching mod metadata from GitHub project releases.
internal class GitHubRepository : RepositoryBase
{
/*********
** Properties
*********/
/// The underlying GitHub API client.
private readonly IGitHubClient Client;
/*********
** Public methods
*********/
/// Construct an instance.
/// The underlying GitHub API client.
public GitHubRepository(IGitHubClient client)
: base(ModRepositoryKey.GitHub)
{
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 (!id.Contains("/") || id.IndexOf("/", StringComparison.InvariantCultureIgnoreCase) != id.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase))
return new ModInfoModel($"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/LookupAnything'.");
// fetch info
try
{
// get latest release (whether preview or stable)
GitRelease latest = await this.Client.GetLatestReleaseAsync(id, includePrerelease: true);
if (latest == null)
return new ModInfoModel("Found no mod with this ID.");
// split stable/prerelease if applicable
GitRelease preview = null;
if (latest.IsPrerelease)
{
GitRelease result = await this.Client.GetLatestReleaseAsync(id, includePrerelease: false);
if (result != null)
{
preview = latest;
latest = result;
}
}
// return data
return new ModInfoModel(name: id, version: this.NormaliseVersion(latest.Tag), previewVersion: this.NormaliseVersion(preview?.Tag), url: $"https://github.com/{id}/releases");
}
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();
}
}
}