From bbd021f8736d1496f34a58b12bb0ee6c341d1c5e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 Dec 2017 23:40:23 -0500 Subject: decouple Pastebin client from log parser (#411) --- .../Framework/Clients/Pastebin/IPastebinClient.cs | 17 +++ .../Framework/Clients/Pastebin/PasteInfo.cs | 15 +++ .../Framework/Clients/Pastebin/PastebinClient.cs | 134 +++++++++++++++++++++ .../Framework/Clients/Pastebin/SavePasteResult.cs | 15 +++ .../Framework/ConfigModels/ApiClientsConfig.cs | 16 +++ .../Framework/ConfigModels/LogParserConfig.cs | 12 -- .../Framework/LogParser/GetPasteResponse.cs | 15 --- .../Framework/LogParser/PastebinClient.cs | 134 --------------------- .../Framework/LogParser/SavePasteResponse.cs | 15 --- 9 files changed, 197 insertions(+), 176 deletions(-) create mode 100644 src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs create mode 100644 src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs create mode 100644 src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs create mode 100644 src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs delete mode 100644 src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs delete mode 100644 src/SMAPI.Web/Framework/LogParser/PastebinClient.cs delete mode 100644 src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs (limited to 'src/SMAPI.Web/Framework') 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 +{ + /// An API client for Pastebin. + internal interface IPastebinClient : IDisposable + { + /// Fetch a saved paste. + /// The paste ID. + Task GetAsync(string id); + + /// Save a paste to Pastebin. + /// The paste content. + Task PostAsync(string content); + } +} diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs new file mode 100644 index 00000000..955156eb --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs @@ -0,0 +1,15 @@ +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin +{ + /// The response for a get-paste request. + internal class PasteInfo + { + /// Whether the log was successfully fetched. + public bool Success { get; set; } + + /// The fetched paste content (if is true). + public string Content { get; set; } + + /// The error message (if saving failed). + public string Error { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs new file mode 100644 index 00000000..ef83a91e --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using Pathoschild.Http.Client; + +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin +{ + /// An API client for Pastebin. + internal class PastebinClient : IPastebinClient + { + /********* + ** Properties + *********/ + /// The underlying HTTP client. + private readonly IClient Client; + + /// The user key used to authenticate with the Pastebin API. + private readonly string UserKey; + + /// The developer key used to authenticate with the Pastebin API. + private readonly string DevKey; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The base URL for the Pastebin API. + /// The user agent for the API client. + /// The user key used to authenticate with the Pastebin API. + /// The developer key used to authenticate with the Pastebin API. + public PastebinClient(string baseUrl, string userAgent, string userKey, string devKey) + { + this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent); + this.UserKey = userKey; + this.DevKey = devKey; + } + + /// Fetch a saved paste. + /// The paste ID. + public async Task GetAsync(string id) + { + try + { + // get from API + string content = await this.Client + .GetAsync($"raw/{id}") + .AsString(); + + // handle Pastebin errors + if (string.IsNullOrWhiteSpace(content)) + return new PasteInfo { Error = "Received an empty response from Pastebin." }; + if (content.StartsWith("Save a paste to Pastebin. + /// The paste content. + public async Task PostAsync(string content) + { + try + { + // validate + if (string.IsNullOrWhiteSpace(content)) + return new SavePasteResult { Error = "The log content can't be empty." }; + + // post to API + string response = await this.Client + .PostAsync("api/api_post.php") + .WithBodyContent(this.GetFormUrlEncodedContent(new Dictionary + { + ["api_option"] = "paste", + ["api_user_key"] = this.UserKey, + ["api_dev_key"] = this.DevKey, + ["api_paste_private"] = "1", // unlisted + ["api_paste_name"] = $"SMAPI log {DateTime.UtcNow:s}", + ["api_paste_expire_date"] = "N", // never expire + ["api_paste_code"] = content + })) + .AsString(); + + // handle Pastebin errors + if (string.IsNullOrWhiteSpace(response)) + return new SavePasteResult { Error = "Received an empty response from Pastebin." }; + if (response.StartsWith("Bad API request")) + return new SavePasteResult { Error = response }; + if (!response.Contains("/")) + return new SavePasteResult { Error = $"Received an unknown response: {response}" }; + + // return paste ID + string pastebinID = response.Split("/").Last(); + return new SavePasteResult { Success = true, ID = pastebinID }; + } + catch (Exception ex) + { + return new SavePasteResult { Success = false, Error = ex.ToString() }; + } + } + + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + public void Dispose() + { + this.Client.Dispose(); + } + + + /********* + ** Private methods + *********/ + /// Build an HTTP content body with form-url-encoded content. + /// The content to encode. + /// This bypasses an issue where restricts the body length to the maximum size of a URL, which isn't applicable here. + private HttpContent GetFormUrlEncodedContent(IDictionary data) + { + string body = string.Join("&", from arg in data select $"{HttpUtility.UrlEncode(arg.Key)}={HttpUtility.UrlEncode(arg.Value)}"); + return new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); + } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs new file mode 100644 index 00000000..89dab697 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/SavePasteResult.cs @@ -0,0 +1,15 @@ +namespace StardewModdingAPI.Web.Framework.Clients.Pastebin +{ + /// The response for a save-log request. + internal class SavePasteResult + { + /// Whether the log was successfully saved. + public bool Success { get; set; } + + /// The saved paste ID (if is true). + public string ID { get; set; } + + /// The error message (if saving failed). + public string Error { 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 /// The URL for a Nexus Mods API query excluding the , where {0} is the mod ID. public string NexusModUrlFormat { get; set; } + + /**** + ** Pastebin + ****/ + /// The base URL for the Pastebin API. + public string PastebinBaseUrl { get; set; } + + /// The user agent for the Pastebin API client, where {0} is the SMAPI version. + public string PastebinUserAgent { get; set; } + + /// The user key used to authenticate with the Pastebin API. + public string PastebinUserKey { get; set; } + + /// The developer key used to authenticate with the Pastebin API. + 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 *********/ /// The root URL for the log parser controller. public string SectionUrl { get; set; } - - /// The base URL for the Pastebin API. - public string PastebinBaseUrl { get; set; } - - /// The user agent for the Pastebin API client, where {0} is the SMAPI version. - public string PastebinUserAgent { get; set; } - - /// The user key used to authenticate with the Pastebin API. - public string PastebinUserKey { get; set; } - - /// The developer key used to authenticate with the Pastebin API. - public string PastebinDevKey { get; set; } } } diff --git a/src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs b/src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs deleted file mode 100644 index 4f8794db..00000000 --- a/src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace StardewModdingAPI.Web.Framework.LogParser -{ - /// The response for a get-paste request. - internal class GetPasteResponse - { - /// Whether the log was successfully fetched. - public bool Success { get; set; } - - /// The fetched paste content (if is true). - public string Content { get; set; } - - /// The error message (if saving failed). - public string Error { get; set; } - } -} diff --git a/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs b/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs deleted file mode 100644 index 1cfaed17..00000000 --- a/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; -using System.Web; -using Pathoschild.Http.Client; - -namespace StardewModdingAPI.Web.Framework.LogParser -{ - /// An API client for Pastebin. - internal class PastebinClient : IDisposable - { - /********* - ** Properties - *********/ - /// The underlying HTTP client. - private readonly IClient Client; - - /// The user key used to authenticate with the Pastebin API. - private readonly string UserKey; - - /// The developer key used to authenticate with the Pastebin API. - private readonly string DevKey; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The base URL for the Pastebin API. - /// The user agent for the API client. - /// The user key used to authenticate with the Pastebin API. - /// The developer key used to authenticate with the Pastebin API. - public PastebinClient(string baseUrl, string userAgent, string userKey, string devKey) - { - this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent); - this.UserKey = userKey; - this.DevKey = devKey; - } - - /// Fetch a saved paste. - /// The paste ID. - public async Task GetAsync(string id) - { - try - { - // get from API - string content = await this.Client - .GetAsync($"raw/{id}") - .AsString(); - - // handle Pastebin errors - if (string.IsNullOrWhiteSpace(content)) - return new GetPasteResponse { Error = "Received an empty response from Pastebin." }; - if (content.StartsWith("Save a paste to Pastebin. - /// The paste content. - public async Task PostAsync(string content) - { - try - { - // validate - if (string.IsNullOrWhiteSpace(content)) - return new SavePasteResponse { Error = "The log content can't be empty." }; - - // post to API - string response = await this.Client - .PostAsync("api/api_post.php") - .WithBodyContent(this.GetFormUrlEncodedContent(new Dictionary - { - ["api_option"] = "paste", - ["api_user_key"] = this.UserKey, - ["api_dev_key"] = this.DevKey, - ["api_paste_private"] = "1", // unlisted - ["api_paste_name"] = $"SMAPI log {DateTime.UtcNow:s}", - ["api_paste_expire_date"] = "N", // never expire - ["api_paste_code"] = content - })) - .AsString(); - - // handle Pastebin errors - if (string.IsNullOrWhiteSpace(response)) - return new SavePasteResponse { Error = "Received an empty response from Pastebin." }; - if (response.StartsWith("Bad API request")) - return new SavePasteResponse { Error = response }; - if (!response.Contains("/")) - return new SavePasteResponse { Error = $"Received an unknown response: {response}" }; - - // return paste ID - string pastebinID = response.Split("/").Last(); - return new SavePasteResponse { Success = true, ID = pastebinID }; - } - catch (Exception ex) - { - return new SavePasteResponse { Success = false, Error = ex.ToString() }; - } - } - - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - public void Dispose() - { - this.Client.Dispose(); - } - - - /********* - ** Private methods - *********/ - /// Build an HTTP content body with form-url-encoded content. - /// The content to encode. - /// This bypasses an issue where restricts the body length to the maximum size of a URL, which isn't applicable here. - private HttpContent GetFormUrlEncodedContent(IDictionary data) - { - string body = string.Join("&", from arg in data select $"{HttpUtility.UrlEncode(arg.Key)}={HttpUtility.UrlEncode(arg.Value)}"); - return new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); - } - } -} diff --git a/src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs b/src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs deleted file mode 100644 index 1c0960a4..00000000 --- a/src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace StardewModdingAPI.Web.Framework.LogParser -{ - /// The response for a save-log request. - internal class SavePasteResponse - { - /// Whether the log was successfully saved. - public bool Success { get; set; } - - /// The saved paste ID (if is true). - public string ID { get; set; } - - /// The error message (if saving failed). - public string Error { get; set; } - } -} -- cgit