using System; using System.Threading.Tasks; using Newtonsoft.Json; using Pathoschild.Http.Client; using StardewModdingAPI.Web.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; } = "Nexus"; /********* ** Public methods *********/ /// Construct an instance. public NexusRepository() { this.Client = new FluentClient("http://www.nexusmods.com/stardewvalley") .SetUserAgent("Nexus Client v0.63.15"); } /// 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($"mods/{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; } } } }