From 2c02dfe45a6d1252bfef557db8f39f97e57d3d19 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 22 Sep 2017 00:47:46 -0400 Subject: rewrite to make update-check logic vendor-agnostic (#336) --- .../Framework/IModRepository.cs | 17 ----- .../Framework/ModRepositories/IModRepository.cs | 24 +++++++ .../Framework/ModRepositories/NexusRepository.cs | 80 ++++++++++++++++++++++ .../Framework/NexusModsClient.cs | 75 -------------------- 4 files changed, 104 insertions(+), 92 deletions(-) delete mode 100644 src/StardewModdingAPI.Web/Framework/IModRepository.cs create mode 100644 src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs create mode 100644 src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs delete mode 100644 src/StardewModdingAPI.Web/Framework/NexusModsClient.cs (limited to 'src/StardewModdingAPI.Web/Framework') diff --git a/src/StardewModdingAPI.Web/Framework/IModRepository.cs b/src/StardewModdingAPI.Web/Framework/IModRepository.cs deleted file mode 100644 index ebf9850f..00000000 --- a/src/StardewModdingAPI.Web/Framework/IModRepository.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Threading.Tasks; -using StardewModdingAPI.Web.Models; - -namespace StardewModdingAPI.Web.Framework -{ - /// A repository which provides mod metadata. - internal interface IModRepository : IDisposable - { - /********* - ** Public methods - *********/ - /// Get metadata about a mod in the repository. - /// The mod ID in this repository. - Task GetModInfoAsync(int id); - } -} diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs new file mode 100644 index 00000000..43bad4e9 --- /dev/null +++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs @@ -0,0 +1,24 @@ +using System; +using System.Threading.Tasks; +using StardewModdingAPI.Web.Models; + +namespace StardewModdingAPI.Web.Framework.ModRepositories +{ + /// A repository which provides mod metadata. + internal interface IModRepository : IDisposable + { + /********* + ** Accessors + *********/ + /// The unique key for this vendor. + string VendorKey { get; } + + + /********* + ** Public methods + *********/ + /// Get metadata about a mod in the repository. + /// The mod ID in this repository. + Task GetModInfoAsync(string id); + } +} diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs new file mode 100644 index 00000000..37f309da --- /dev/null +++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs @@ -0,0 +1,80 @@ +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 new ModGenericModel($"{this.VendorKey}:{id}", response.Name, response.Version, response.Url); + } + catch (Exception ex) + { + return new ModGenericModel($"{this.VendorKey}:{id}", 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; } + } + } +} diff --git a/src/StardewModdingAPI.Web/Framework/NexusModsClient.cs b/src/StardewModdingAPI.Web/Framework/NexusModsClient.cs deleted file mode 100644 index 8f010d56..00000000 --- a/src/StardewModdingAPI.Web/Framework/NexusModsClient.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Pathoschild.Http.Client; -using StardewModdingAPI.Web.Models; - -namespace StardewModdingAPI.Web.Framework -{ - /// An HTTP client for fetching mod metadata from Nexus Mods. - internal class NexusModsClient : IModRepository - { - /********* - ** Properties - *********/ - /// The underlying HTTP client. - private readonly IClient Client; - - /********* - ** Public methods - *********/ - /// Construct an instance. - public NexusModsClient() - { - 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(int id) - { - try - { - NexusResponseModel response = await this.Client - .GetAsync($"mods/{id}") - .As(); - return new ModGenericModel("Nexus", id, response.Name, response.Version, response.Url); - } - catch (Exception) - { - return new ModGenericModel("Nexus", id); - } - } - - /// 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 unique mod ID. - public int ID { get; set; } - - /// 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; } - } - } -} -- cgit