diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 00:47:46 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 00:47:46 -0400 |
commit | 2c02dfe45a6d1252bfef557db8f39f97e57d3d19 (patch) | |
tree | 4fb8b5759d56ba6a62a452e09180ede280228b3b /src/StardewModdingAPI.Web/Framework/ModRepositories | |
parent | edbc3ef3c08909486d6c4b8797f0e3c2934854fa (diff) | |
download | SMAPI-2c02dfe45a6d1252bfef557db8f39f97e57d3d19.tar.gz SMAPI-2c02dfe45a6d1252bfef557db8f39f97e57d3d19.tar.bz2 SMAPI-2c02dfe45a6d1252bfef557db8f39f97e57d3d19.zip |
rewrite to make update-check logic vendor-agnostic (#336)
Diffstat (limited to 'src/StardewModdingAPI.Web/Framework/ModRepositories')
-rw-r--r-- | src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs | 24 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs | 80 |
2 files changed, 104 insertions, 0 deletions
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 +{ + /// <summary>A repository which provides mod metadata.</summary> + internal interface IModRepository : IDisposable + { + /********* + ** Accessors + *********/ + /// <summary>The unique key for this vendor.</summary> + string VendorKey { get; } + + + /********* + ** 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(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 +{ + /// <summary>An HTTP client for fetching mod metadata from Nexus Mods.</summary> + internal class NexusRepository : IModRepository + { + /********* + ** Properties + *********/ + /// <summary>The underlying HTTP client.</summary> + private readonly IClient Client; + + + /********* + ** Accessors + *********/ + /// <summary>The unique key for this vendor.</summary> + public string VendorKey { get; } = "Nexus"; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public NexusRepository() + { + 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(string id) + { + try + { + NexusResponseModel response = await this.Client + .GetAsync($"mods/{id}") + .As<NexusResponseModel>(); + return new ModGenericModel($"{this.VendorKey}:{id}", response.Name, response.Version, response.Url); + } + catch (Exception ex) + { + return new ModGenericModel($"{this.VendorKey}:{id}", ex.ToString()); + } + } + + /// <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 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; } + } + } +} |