using System;
using System.Net;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
namespace StardewModdingAPI.Web.Framework.Clients.Pastebin
{
/// An API client for Pastebin.
internal class PastebinClient : IPastebinClient
{
/*********
** Fields
*********/
/// The underlying HTTP client.
private readonly IClient Client;
/*********
** Public methods
*********/
/// Construct an instance.
/// The base URL for the Pastebin API.
/// The user agent for the API client.
public PastebinClient(string baseUrl, string userAgent)
{
this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
}
/// 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("Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
{
this.Client.Dispose();
}
}
}