using System; using System.Threading.Tasks; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Web.Framework.Clients.Nexus; namespace StardewModdingAPI.Web.Framework.ModRepositories { /// An HTTP client for fetching mod metadata from Nexus Mods. internal class NexusRepository : RepositoryBase { /********* ** Fields *********/ /// The underlying Nexus Mods API client. private readonly INexusClient Client; /********* ** Public methods *********/ /// Construct an instance. /// The underlying Nexus Mods API client. public NexusRepository(INexusClient client) : base(ModRepositoryKey.Nexus) { 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 (!uint.TryParse(id, out uint nexusID)) return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Nexus mod ID, must be an integer ID."); // fetch info try { NexusMod mod = await this.Client.GetModAsync(nexusID); if (mod == null) return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, "Found no Nexus mod with this ID."); if (mod.Error != null) { RemoteModStatus remoteStatus = mod.Status == NexusModStatus.Hidden || mod.Status == NexusModStatus.NotPublished ? RemoteModStatus.DoesNotExist : RemoteModStatus.TemporaryError; return new ModInfoModel().SetError(remoteStatus, mod.Error); } return new ModInfoModel(name: mod.Name, version: this.NormalizeVersion(mod.Version), previewVersion: mod.LatestFileVersion?.ToString(), url: mod.Url); } catch (Exception ex) { return new ModInfoModel().SetError(RemoteModStatus.TemporaryError, ex.ToString()); } } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public override void Dispose() { this.Client.Dispose(); } } }