diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 00:13:04 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 00:13:04 -0400 |
commit | edbc3ef3c08909486d6c4b8797f0e3c2934854fa (patch) | |
tree | e59aea823b05def6f6da36fae702f6121b0527e4 /src/StardewModdingAPI.Web/Framework | |
parent | dfae52b1e57677c1efa052b1073a2e8a56f32163 (diff) | |
download | SMAPI-edbc3ef3c08909486d6c4b8797f0e3c2934854fa.tar.gz SMAPI-edbc3ef3c08909486d6c4b8797f0e3c2934854fa.tar.bz2 SMAPI-edbc3ef3c08909486d6c4b8797f0e3c2934854fa.zip |
refactor Nexus code into generic vendor, rewrite using fluent HTTP client (#336)
Diffstat (limited to 'src/StardewModdingAPI.Web/Framework')
-rw-r--r-- | src/StardewModdingAPI.Web/Framework/IModRepository.cs | 17 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Framework/NexusModsClient.cs | 75 |
2 files changed, 92 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.Web/Framework/IModRepository.cs b/src/StardewModdingAPI.Web/Framework/IModRepository.cs new file mode 100644 index 00000000..ebf9850f --- /dev/null +++ b/src/StardewModdingAPI.Web/Framework/IModRepository.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using StardewModdingAPI.Web.Models; + +namespace StardewModdingAPI.Web.Framework +{ + /// <summary>A repository which provides mod metadata.</summary> + internal interface IModRepository : IDisposable + { + /********* + ** Public methods + *********/ + /// <summary>Get metadata about a mod in the repository.</summary> + /// <param name="id">The mod ID in this repository.</param> + Task<ModGenericModel> GetModInfoAsync(int id); + } +} diff --git a/src/StardewModdingAPI.Web/Framework/NexusModsClient.cs b/src/StardewModdingAPI.Web/Framework/NexusModsClient.cs new file mode 100644 index 00000000..8f010d56 --- /dev/null +++ b/src/StardewModdingAPI.Web/Framework/NexusModsClient.cs @@ -0,0 +1,75 @@ +using System; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Pathoschild.Http.Client; +using StardewModdingAPI.Web.Models; + +namespace StardewModdingAPI.Web.Framework +{ + /// <summary>An HTTP client for fetching mod metadata from Nexus Mods.</summary> + internal class NexusModsClient : IModRepository + { + /********* + ** Properties + *********/ + /// <summary>The underlying HTTP client.</summary> + private readonly IClient Client; + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public NexusModsClient() + { + this.Client = new FluentClient("http://www.nexusmods.com/stardewvalley") + .SetUserAgent("Nexus Client v0.63.15"); + } + + /// <summary>Get metadata about a mod in the repository.</summary> + /// <param name="id">The mod ID in this repository.</param> + public async Task<ModGenericModel> GetModInfoAsync(int id) + { + try + { + NexusResponseModel response = await this.Client + .GetAsync($"mods/{id}") + .As<NexusResponseModel>(); + return new ModGenericModel("Nexus", id, response.Name, response.Version, response.Url); + } + catch (Exception) + { + return new ModGenericModel("Nexus", id); + } + } + + /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> + public void Dispose() + { + this.Client.Dispose(); + } + + + /********* + ** Private models + *********/ + /// <summary>A mod metadata response from Nexus Mods.</summary> + private class NexusResponseModel + { + /********* + ** Accessors + *********/ + /// <summary>The unique mod ID.</summary> + public int ID { get; set; } + + /// <summary>The mod name.</summary> + public string Name { get; set; } + + /// <summary>The mod's semantic version number.</summary> + public string Version { get; set; } + + /// <summary>The mod's web URL.</summary> + [JsonProperty("mod_page_uri")] + public string Url { get; set; } + } + } +} |