From 05541c11a72735d79d98cf3ae14d592e70bd8f54 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 Dec 2017 23:28:07 -0500 Subject: decouple API clients from mods API (#411) --- .../ModRepositories/ChucklefishRepository.cs | 50 ++++------------------ 1 file changed, 9 insertions(+), 41 deletions(-) (limited to 'src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs') diff --git a/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs index 06ec58ed..266055a6 100644 --- a/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs +++ b/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs @@ -1,9 +1,7 @@ using System; -using System.Net; using System.Threading.Tasks; -using HtmlAgilityPack; -using Pathoschild.Http.Client; using StardewModdingAPI.Common.Models; +using StardewModdingAPI.Web.Framework.Clients.Chucklefish; namespace StardewModdingAPI.Web.Framework.ModRepositories { @@ -13,14 +11,8 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories /********* ** Properties *********/ - /// The base URL for the Chucklefish mod site. - private readonly string BaseUrl; - - /// The URL for a mod page excluding the base URL, where {0} is the mod ID. - private readonly string ModPageUrlFormat; - /// The underlying HTTP client. - private readonly IClient Client; + private readonly IChucklefishClient Client; /********* @@ -28,15 +20,11 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories *********/ /// Construct an instance. /// The unique key for this vendor. - /// The user agent for the API client. - /// The base URL for the Chucklefish mod site. - /// The URL for a mod page excluding the , where {0} is the mod ID. - public ChucklefishRepository(string vendorKey, string userAgent, string baseUrl, string modPageUrlFormat) + /// The underlying HTTP client. + public ChucklefishRepository(string vendorKey, IChucklefishClient client) : base(vendorKey) { - this.BaseUrl = baseUrl; - this.ModPageUrlFormat = modPageUrlFormat; - this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent); + this.Client = client; } /// Get metadata about a mod in the repository. @@ -44,38 +32,18 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories public override async Task GetModInfoAsync(string id) { // validate ID format - if (!uint.TryParse(id, out uint _)) + if (!uint.TryParse(id, out uint realID)) return new ModInfoModel($"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID."); // fetch info try { - // fetch HTML - string html; - try - { - html = await this.Client - .GetAsync(string.Format(this.ModPageUrlFormat, id)) - .AsString(); - } - catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) - { + var mod = await this.Client.GetModAsync(realID); + if (mod == null) return new ModInfoModel("Found no mod with this ID."); - } - - // parse HTML - var doc = new HtmlDocument(); - doc.LoadHtml(html); - - // extract mod info - string url = new UriBuilder(new Uri(this.BaseUrl)) { Path = string.Format(this.ModPageUrlFormat, id) }.Uri.ToString(); - string name = doc.DocumentNode.SelectSingleNode("//meta[@name='twitter:title']").Attributes["content"].Value; - if (name.StartsWith("[SMAPI] ")) - name = name.Substring("[SMAPI] ".Length); - string version = doc.DocumentNode.SelectSingleNode("//h1/span").InnerText; // create model - return new ModInfoModel(name, this.NormaliseVersion(version), url); + return new ModInfoModel(mod.Name, this.NormaliseVersion(mod.Version), mod.Url); } catch (Exception ex) { -- cgit