using System; 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 Nexus Mods. internal class NexusRepository : IModRepository { /********* ** Properties *********/ /// The underlying HTTP client. private readonly IClient Client; /********* ** Accessors *********/ /// The unique key for this vendor. public string VendorKey { get; } /// The URL for a Nexus Mods API query excluding the base URL, where {0} is the mod ID. public string ModUrlFormat { get; } /********* ** Public methods *********/ /// Construct an instance. /// The unique key for this vendor. /// The user agent for the Nexus Mods API client. /// The base URL for the Nexus Mods API. /// The URL for a Nexus Mods API query excluding the , where {0} is the mod ID. public NexusRepository(string vendorKey, string userAgent, string baseUrl, string modUrlFormat) { this.VendorKey = vendorKey; this.ModUrlFormat = modUrlFormat; this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent); } /// Get metadata about a mod in the repository. /// The mod ID in this repository. public async Task GetModInfoAsync(string id) { try { NexusResponseModel response = await this.Client .GetAsync(string.Format(this.ModUrlFormat, id)) .As(); return response != null ? new ModInfoModel(response.Name, response.Version, response.Url) : 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 void Dispose() { this.Client.Dispose(); } /********* ** Private models *********/ /// A mod metadata response from Nexus Mods. private class NexusResponseModel { /********* ** Accessors *********/ /// The mod name. public string Name { get; set; } /// The mod's semantic version number. public string Version { get; set; } /// The mod's web URL. [JsonProperty("mod_page_uri")] public string Url { get; set; } } } }