summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/Pastebin
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Clients/Pastebin')
-rw-r--r--src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs28
-rw-r--r--src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs12
2 files changed, 30 insertions, 10 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs
index 813ea115..7f40e713 100644
--- a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs
+++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs
@@ -1,15 +1,35 @@
+using System.Diagnostics.CodeAnalysis;
+
namespace StardewModdingAPI.Web.Framework.Clients.Pastebin
{
/// <summary>The response for a get-paste request.</summary>
internal class PasteInfo
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>Whether the log was successfully fetched.</summary>
- public bool Success { get; set; }
+ [MemberNotNullWhen(true, nameof(PasteInfo.Content))]
+ [MemberNotNullWhen(false, nameof(PasteInfo.Error))]
+ public bool Success => this.Error == null || this.Content != null;
/// <summary>The fetched paste content (if <see cref="Success"/> is <c>true</c>).</summary>
- public string Content { get; set; }
+ public string? Content { get; internal set; }
+
+ /// <summary>The error message (if <see cref="Success"/> is <c>false</c>).</summary>
+ public string? Error { get; }
+
- /// <summary>The error message if saving failed.</summary>
- public string Error { get; set; }
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="content">The fetched paste content.</param>
+ /// <param name="error">The error message, if it failed.</param>
+ public PasteInfo(string? content, string? error)
+ {
+ this.Content = content;
+ this.Error = error;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs
index 1be00be7..0e00f071 100644
--- a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs
@@ -33,24 +33,24 @@ namespace StardewModdingAPI.Web.Framework.Clients.Pastebin
try
{
// get from API
- string content = await this.Client
+ 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." };
+ return new PasteInfo(null, "Received an empty response from Pastebin.");
if (content.StartsWith("<!DOCTYPE"))
- 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 };
+ return new PasteInfo(null, $"Received a captcha challenge from Pastebin. Please visit https://pastebin.com/{id} in a new window to solve it.");
+ return new PasteInfo(content, null);
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
- return new PasteInfo { Error = "There's no log with that ID." };
+ return new PasteInfo(null, "There's no log with that ID.");
}
catch (Exception ex)
{
- return new PasteInfo { Error = $"Pastebin error: {ex}" };
+ return new PasteInfo(null, $"Pastebin error: {ex}");
}
}