using System; using System.Threading.Tasks; using StardewModdingAPI.Common.Models; 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 unique key for this vendor. /// The underlying GitHub API client. public GitHubRepository(string vendorKey, IGitHubClient 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 (!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 GitRelease latest = await this.Client.GetLatestReleaseAsync(id, includePrerelease: true); GitRelease preview = null; if (latest == null) return new ModInfoModel("Found no mod with this ID."); // get latest stable release (if not latest) if (latest.IsPrerelease) { preview = latest; latest = await this.Client.GetLatestReleaseAsync(id, includePrerelease: false); } // 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(); } } }