using System; using System.Net; using System.Threading.Tasks; using Newtonsoft.Json; using Pathoschild.Http.Client; using StardewModdingAPI.Models; namespace StardewModdingAPI.Web.Framework.ModRepositories { /// An HTTP client for fetching mod metadata from GitHub project releases. internal class GitHubRepository : RepositoryBase { /********* ** Properties *********/ /// The URL for a Nexus Mods API query excluding the base URL, where {0} is the mod ID. private readonly string ReleaseUrlFormat; /// The underlying HTTP client. private readonly IClient Client; /********* ** Public methods *********/ /// Construct an instance. /// The unique key for this vendor. /// The base URL for the Nexus Mods API. /// The URL for a Nexus Mods API query excluding the , where {0} is the mod ID. /// The user agent for the API client. /// The Accept header value expected by the GitHub API. /// The username with which to authenticate to the GitHub API. /// The password with which to authenticate to the GitHub API. public GitHubRepository(string vendorKey, string baseUrl, string releaseUrlFormat, string userAgent, string acceptHeader, string username, string password) : base(vendorKey) { this.ReleaseUrlFormat = releaseUrlFormat; this.Client = new FluentClient(baseUrl) .SetUserAgent(userAgent) .AddDefault(req => req.WithHeader("Accept", acceptHeader)); if (!string.IsNullOrWhiteSpace(username)) this.Client = this.Client.SetBasicAuthentication(username, password); } /// 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 { GitRelease release = await this.Client .GetAsync(string.Format(this.ReleaseUrlFormat, id)) .As(); return new ModInfoModel(id, this.NormaliseVersion(release.Tag), $"https://github.com/{id}/releases"); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { return new ModInfoModel("Found no mod with this ID."); } 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(); } /********* ** Private models *********/ /// Metadata about a GitHub release tag. private class GitRelease { /********* ** Accessors *********/ /// The display name. [JsonProperty("name")] public string Name { get; set; } /// The semantic version string. [JsonProperty("tag_name")] public string Tag { get; set; } } } }