diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-24 23:40:23 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-24 23:40:23 -0500 |
commit | bbd021f8736d1496f34a58b12bb0ee6c341d1c5e (patch) | |
tree | bd31841d13f8ada582fd6aaf0f97f918449c719a /src | |
parent | 05541c11a72735d79d98cf3ae14d592e70bd8f54 (diff) | |
download | SMAPI-bbd021f8736d1496f34a58b12bb0ee6c341d1c5e.tar.gz SMAPI-bbd021f8736d1496f34a58b12bb0ee6c341d1c5e.tar.bz2 SMAPI-bbd021f8736d1496f34a58b12bb0ee6c341d1c5e.zip |
decouple Pastebin client from log parser (#411)
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.Web/Controllers/LogParserController.cs | 20 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs | 17 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs (renamed from src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs) | 4 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs (renamed from src/SMAPI.Web/Framework/LogParser/PastebinClient.cs) | 30 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs (renamed from src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs) | 4 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs | 16 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/ConfigModels/LogParserConfig.cs | 12 | ||||
-rw-r--r-- | src/SMAPI.Web/Startup.cs | 10 | ||||
-rw-r--r-- | src/SMAPI.Web/appsettings.Development.json | 9 | ||||
-rw-r--r-- | src/SMAPI.Web/appsettings.json | 12 |
10 files changed, 82 insertions, 52 deletions
diff --git a/src/SMAPI.Web/Controllers/LogParserController.cs b/src/SMAPI.Web/Controllers/LogParserController.cs index 454440bb..b9227a2f 100644 --- a/src/SMAPI.Web/Controllers/LogParserController.cs +++ b/src/SMAPI.Web/Controllers/LogParserController.cs @@ -6,8 +6,8 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using StardewModdingAPI.Web.Framework; +using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.ConfigModels; -using StardewModdingAPI.Web.Framework.LogParser; using StardewModdingAPI.Web.ViewModels; namespace StardewModdingAPI.Web.Controllers @@ -22,7 +22,7 @@ namespace StardewModdingAPI.Web.Controllers private readonly LogParserConfig Config; /// <summary>The underlying Pastebin client.</summary> - private readonly PastebinClient PastebinClient; + private readonly IPastebinClient Pastebin; /// <summary>The first bytes in a valid zip file.</summary> /// <remarks>See <a href="https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers"/>.</remarks> @@ -37,13 +37,11 @@ namespace StardewModdingAPI.Web.Controllers ***/ /// <summary>Construct an instance.</summary> /// <param name="configProvider">The log parser config settings.</param> - public LogParserController(IOptions<LogParserConfig> configProvider) + /// <param name="pastebin">The Pastebin API client.</param> + public LogParserController(IOptions<LogParserConfig> configProvider, IPastebinClient pastebin) { - // init Pastebin client this.Config = configProvider.Value; - string version = this.GetType().Assembly.GetName().Version.ToString(3); - string userAgent = string.Format(this.Config.PastebinUserAgent, version); - this.PastebinClient = new PastebinClient(this.Config.PastebinBaseUrl, userAgent, this.Config.PastebinUserKey, this.Config.PastebinDevKey); + this.Pastebin = pastebin; } /*** @@ -67,9 +65,9 @@ namespace StardewModdingAPI.Web.Controllers /// <param name="id">The Pastebin paste ID.</param> [HttpGet, Produces("application/json")] [Route("log/fetch/{id}")] - public async Task<GetPasteResponse> GetAsync(string id) + public async Task<PasteInfo> GetAsync(string id) { - GetPasteResponse response = await this.PastebinClient.GetAsync(id); + PasteInfo response = await this.Pastebin.GetAsync(id); response.Content = this.DecompressString(response.Content); return response; } @@ -78,10 +76,10 @@ namespace StardewModdingAPI.Web.Controllers /// <param name="content">The log content to save.</param> [HttpPost, Produces("application/json"), AllowLargePosts] [Route("log/save")] - public async Task<SavePasteResponse> PostAsync([FromBody] string content) + public async Task<SavePasteResult> PostAsync([FromBody] string content) { content = this.CompressString(content); - return await this.PastebinClient.PostAsync(content); + return await this.Pastebin.PostAsync(content); } diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs new file mode 100644 index 00000000..630dfb76 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin +{ + /// <summary>An API client for Pastebin.</summary> + internal interface IPastebinClient : IDisposable + { + /// <summary>Fetch a saved paste.</summary> + /// <param name="id">The paste ID.</param> + Task<PasteInfo> GetAsync(string id); + + /// <summary>Save a paste to Pastebin.</summary> + /// <param name="content">The paste content.</param> + Task<SavePasteResult> PostAsync(string content); + } +} diff --git a/src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs index 4f8794db..955156eb 100644 --- a/src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs @@ -1,7 +1,7 @@ -namespace StardewModdingAPI.Web.Framework.LogParser +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin { /// <summary>The response for a get-paste request.</summary> - internal class GetPasteResponse + internal class PasteInfo { /// <summary>Whether the log was successfully fetched.</summary> public bool Success { get; set; } diff --git a/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs index 1cfaed17..ef83a91e 100644 --- a/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs @@ -8,10 +8,10 @@ using System.Threading.Tasks; using System.Web; using Pathoschild.Http.Client; -namespace StardewModdingAPI.Web.Framework.LogParser +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin { /// <summary>An API client for Pastebin.</summary> - internal class PastebinClient : IDisposable + internal class PastebinClient : IPastebinClient { /********* ** Properties @@ -43,7 +43,7 @@ namespace StardewModdingAPI.Web.Framework.LogParser /// <summary>Fetch a saved paste.</summary> /// <param name="id">The paste ID.</param> - public async Task<GetPasteResponse> GetAsync(string id) + public async Task<PasteInfo> GetAsync(string id) { try { @@ -54,30 +54,30 @@ namespace StardewModdingAPI.Web.Framework.LogParser // handle Pastebin errors if (string.IsNullOrWhiteSpace(content)) - return new GetPasteResponse { Error = "Received an empty response from Pastebin." }; + return new PasteInfo { Error = "Received an empty response from Pastebin." }; if (content.StartsWith("<!DOCTYPE")) - return new GetPasteResponse { Error = $"Received a captcha challenge from Pastebin. Please visit https://pastebin.com/{id} in a new window to solve it." }; - return new GetPasteResponse { Success = true, Content = content }; + return new PasteInfo { Error = $"Received a captcha challenge from Pastebin. Please visit https://pastebin.com/{id} in a new window to solve it." }; + return new PasteInfo { Success = true, Content = content }; } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { - return new GetPasteResponse { Error = "There's no log with that ID." }; + return new PasteInfo { Error = "There's no log with that ID." }; } catch (Exception ex) { - return new GetPasteResponse { Error = ex.ToString() }; + return new PasteInfo { Error = ex.ToString() }; } } /// <summary>Save a paste to Pastebin.</summary> /// <param name="content">The paste content.</param> - public async Task<SavePasteResponse> PostAsync(string content) + public async Task<SavePasteResult> PostAsync(string content) { try { // validate if (string.IsNullOrWhiteSpace(content)) - return new SavePasteResponse { Error = "The log content can't be empty." }; + return new SavePasteResult { Error = "The log content can't be empty." }; // post to API string response = await this.Client @@ -96,19 +96,19 @@ namespace StardewModdingAPI.Web.Framework.LogParser // handle Pastebin errors if (string.IsNullOrWhiteSpace(response)) - return new SavePasteResponse { Error = "Received an empty response from Pastebin." }; + return new SavePasteResult { Error = "Received an empty response from Pastebin." }; if (response.StartsWith("Bad API request")) - return new SavePasteResponse { Error = response }; + return new SavePasteResult { Error = response }; if (!response.Contains("/")) - return new SavePasteResponse { Error = $"Received an unknown response: {response}" }; + return new SavePasteResult { Error = $"Received an unknown response: {response}" }; // return paste ID string pastebinID = response.Split("/").Last(); - return new SavePasteResponse { Success = true, ID = pastebinID }; + return new SavePasteResult { Success = true, ID = pastebinID }; } catch (Exception ex) { - return new SavePasteResponse { Success = false, Error = ex.ToString() }; + return new SavePasteResult { Success = false, Error = ex.ToString() }; } } diff --git a/src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs index 1c0960a4..89dab697 100644 --- a/src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs @@ -1,7 +1,7 @@ -namespace StardewModdingAPI.Web.Framework.LogParser +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin { /// <summary>The response for a save-log request.</summary> - internal class SavePasteResponse + internal class SavePasteResult { /// <summary>Whether the log was successfully saved.</summary> public bool Success { get; set; } diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs index 19794920..61219414 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs @@ -52,5 +52,21 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /// <summary>The URL for a Nexus Mods API query excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary> public string NexusModUrlFormat { get; set; } + + /**** + ** Pastebin + ****/ + /// <summary>The base URL for the Pastebin API.</summary> + public string PastebinBaseUrl { get; set; } + + /// <summary>The user agent for the Pastebin API client, where {0} is the SMAPI version.</summary> + public string PastebinUserAgent { get; set; } + + /// <summary>The user key used to authenticate with the Pastebin API.</summary> + public string PastebinUserKey { get; set; } + + /// <summary>The developer key used to authenticate with the Pastebin API.</summary> + public string PastebinDevKey { get; set; } + } } diff --git a/src/SMAPI.Web/Framework/ConfigModels/LogParserConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/LogParserConfig.cs index df5d605d..198274b2 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/LogParserConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/LogParserConfig.cs @@ -8,17 +8,5 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels *********/ /// <summary>The root URL for the log parser controller.</summary> public string SectionUrl { get; set; } - - /// <summary>The base URL for the Pastebin API.</summary> - public string PastebinBaseUrl { get; set; } - - /// <summary>The user agent for the Pastebin API client, where {0} is the SMAPI version.</summary> - public string PastebinUserAgent { get; set; } - - /// <summary>The user key used to authenticate with the Pastebin API.</summary> - public string PastebinUserKey { get; set; } - - /// <summary>The developer key used to authenticate with the Pastebin API.</summary> - public string PastebinDevKey { get; set; } } } diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs index 7938520a..307c4ae9 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -10,6 +10,7 @@ using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; using StardewModdingAPI.Web.Framework.Clients.Nexus; +using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.Framework.RewriteRules; @@ -69,6 +70,7 @@ namespace StardewModdingAPI.Web baseUrl: api.ChucklefishBaseUrl, modPageUrlFormat: api.ChucklefishModPageUrlFormat )); + services.AddSingleton<IGitHubClient>(new GitHubClient( baseUrl: api.GitHubBaseUrl, releaseUrlFormat: api.GitHubReleaseUrlFormat, @@ -77,11 +79,19 @@ namespace StardewModdingAPI.Web username: api.GitHubUsername, password: api.GitHubPassword )); + services.AddSingleton<INexusClient>(new NexusClient( userAgent: api.NexusUserAgent, baseUrl: api.NexusBaseUrl, modUrlFormat: api.NexusModUrlFormat )); + + services.AddSingleton<IPastebinClient>(new PastebinClient( + baseUrl: api.PastebinBaseUrl, + userAgent: userAgent, + userKey: api.PastebinUserKey, + devKey: api.PastebinDevKey + )); } } diff --git a/src/SMAPI.Web/appsettings.Development.json b/src/SMAPI.Web/appsettings.Development.json index 4602d1ed..45fc30f3 100644 --- a/src/SMAPI.Web/appsettings.Development.json +++ b/src/SMAPI.Web/appsettings.Development.json @@ -18,11 +18,12 @@ }, "ApiClients": { "GitHubUsername": null, - "GitHubPassword": null - }, - "LogParser": { - "SectionUrl": "http://localhost:59482/log/", + "GitHubPassword": null, + "PastebinUserKey": null, "PastebinDevKey": null + }, + "LogParser": { + "SectionUrl": "http://localhost:59482/log/" } } diff --git a/src/SMAPI.Web/appsettings.json b/src/SMAPI.Web/appsettings.json index 4f70b41b..69b3b4f8 100644 --- a/src/SMAPI.Web/appsettings.json +++ b/src/SMAPI.Web/appsettings.json @@ -27,7 +27,11 @@ "NexusUserAgent": "Nexus Client v0.63.15", "NexusBaseUrl": "http://www.nexusmods.com/stardewvalley", - "NexusModUrlFormat": "mods/{0}" + "NexusModUrlFormat": "mods/{0}", + + "PastebinBaseUrl": "https://pastebin.com/", + "PastebinUserKey": null, // see top note + "PastebinDevKey": null // see top note }, "ModUpdateCheck": { @@ -39,10 +43,6 @@ "NexusKey": "Nexus" }, "LogParser": { - "SectionUrl": null, // see top note - "PastebinBaseUrl": "https://pastebin.com/", - "PastebinUserAgent": "SMAPI/{0} (+https://github.com/Pathoschild/SMAPI)", - "PastebinUserKey": null, // see top note - "PastebinDevKey": null // see top note + "SectionUrl": null // see top note } } |