summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/LogParser
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-12-24 23:40:23 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-12-24 23:40:23 -0500
commitbbd021f8736d1496f34a58b12bb0ee6c341d1c5e (patch)
treebd31841d13f8ada582fd6aaf0f97f918449c719a /src/SMAPI.Web/Framework/LogParser
parent05541c11a72735d79d98cf3ae14d592e70bd8f54 (diff)
downloadSMAPI-bbd021f8736d1496f34a58b12bb0ee6c341d1c5e.tar.gz
SMAPI-bbd021f8736d1496f34a58b12bb0ee6c341d1c5e.tar.bz2
SMAPI-bbd021f8736d1496f34a58b12bb0ee6c341d1c5e.zip
decouple Pastebin client from log parser (#411)
Diffstat (limited to 'src/SMAPI.Web/Framework/LogParser')
-rw-r--r--src/SMAPI.Web/Framework/LogParser/GetPasteResponse.cs15
-rw-r--r--src/SMAPI.Web/Framework/LogParser/PastebinClient.cs134
-rw-r--r--src/SMAPI.Web/Framework/LogParser/SavePasteResponse.cs15
3 files changed, 0 insertions, 164 deletions
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
-{
- /// <summary>The response for a get-paste request.</summary>
- internal class GetPasteResponse
- {
- /// <summary>Whether the log was successfully fetched.</summary>
- public bool Success { get; set; }
-
- /// <summary>The fetched paste content (if <see cref="Success"/> is <c>true</c>).</summary>
- public string Content { get; set; }
-
- /// <summary>The error message (if saving failed).</summary>
- 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
-{
- /// <summary>An API client for Pastebin.</summary>
- internal class PastebinClient : IDisposable
- {
- /*********
- ** Properties
- *********/
- /// <summary>The underlying HTTP client.</summary>
- private readonly IClient Client;
-
- /// <summary>The user key used to authenticate with the Pastebin API.</summary>
- private readonly string UserKey;
-
- /// <summary>The developer key used to authenticate with the Pastebin API.</summary>
- private readonly string DevKey;
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="baseUrl">The base URL for the Pastebin API.</param>
- /// <param name="userAgent">The user agent for the API client.</param>
- /// <param name="userKey">The user key used to authenticate with the Pastebin API.</param>
- /// <param name="devKey">The developer key used to authenticate with the Pastebin API.</param>
- public PastebinClient(string baseUrl, string userAgent, string userKey, string devKey)
- {
- this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
- this.UserKey = userKey;
- this.DevKey = devKey;
- }
-
- /// <summary>Fetch a saved paste.</summary>
- /// <param name="id">The paste ID.</param>
- public async Task<GetPasteResponse> 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("<!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 };
- }
- catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
- {
- return new GetPasteResponse { Error = "There's no log with that ID." };
- }
- catch (Exception ex)
- {
- return new GetPasteResponse { Error = ex.ToString() };
- }
- }
-
- /// <summary>Save a paste to Pastebin.</summary>
- /// <param name="content">The paste content.</param>
- public async Task<SavePasteResponse> 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<string, string>
- {
- ["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() };
- }
- }
-
- /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
- public void Dispose()
- {
- this.Client.Dispose();
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>Build an HTTP content body with form-url-encoded content.</summary>
- /// <param name="data">The content to encode.</param>
- /// <remarks>This bypasses an issue where <see cref="FormUrlEncodedContent"/> restricts the body length to the maximum size of a URL, which isn't applicable here.</remarks>
- private HttpContent GetFormUrlEncodedContent(IDictionary<string, string> 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
-{
- /// <summary>The response for a save-log request.</summary>
- internal class SavePasteResponse
- {
- /// <summary>Whether the log was successfully saved.</summary>
- public bool Success { get; set; }
-
- /// <summary>The saved paste ID (if <see cref="Success"/> is <c>true</c>).</summary>
- public string ID { get; set; }
-
- /// <summary>The error message (if saving failed).</summary>
- public string Error { get; set; }
- }
-}