Created
September 1, 2014 04:53
-
-
Save trcio/cc2007bfcffb08708061 to your computer and use it in GitHub Desktop.
A fully-featured paste.sx API wrapper.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Net; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
namespace PasteSx | |
{ | |
/// <summary> | |
/// Represents the method that will handle the CreatePasteCompleted event of a PasteSxProvider. | |
/// </summary> | |
/// <param name="e">A CreatePasteCompletedEventArgs that contains event data.</param> | |
public delegate void CreatePasteCompletedEventHandler(CreatePasteCompletedEventArgs e); | |
/// <summary> | |
/// Provides methods for paste-creation on paste.sx. | |
/// </summary> | |
public class PasteSxProvider | |
{ | |
private const string API_ENDPOINT = "https://paste.sx/api.php"; | |
/// <summary> | |
/// Occurs when an asynchronous paste-creation completes. | |
/// </summary> | |
public event CreatePasteCompletedEventHandler CreatePasteCompleted; | |
/// <summary> | |
/// Your paste.sx API Key. | |
/// </summary> | |
public string ApiKey { get; set; } | |
/// <summary> | |
/// Initializes a new instance of the PasteSxProvider class with the specified API Key. | |
/// </summary> | |
/// <param name="apiKey">Your paste.sx API Key.</param> | |
public PasteSxProvider(string apiKey) | |
{ | |
if (string.IsNullOrWhiteSpace(apiKey)) | |
throw new ArgumentNullException("apiKey"); | |
if (apiKey.Length != 32) | |
throw new ArgumentOutOfRangeException("apiKey"); | |
ApiKey = apiKey; | |
} | |
/// <summary> | |
/// Creates a paste using the specified information. | |
/// </summary> | |
/// <param name="title">The title of the paste. Can be empty.</param> | |
/// <param name="content">The content of the paste.</param> | |
/// <param name="password">The password of the paste. Can be empty.</param> | |
/// <param name="encrypted">Whether the paste will be encrypted or not.</param> | |
/// <returns>A CreatePasteResult that contains operation data.</returns> | |
public CreatePasteResult CreatePaste(string title, string content, string password, bool encrypted) | |
{ | |
if (string.IsNullOrWhiteSpace(content)) | |
throw new ArgumentNullException("content"); | |
var values = GetValues(title, content, password, encrypted); | |
using (var client = new WebClient()) | |
{ | |
client.Proxy = null; | |
var rawJson = Encoding.UTF8.GetString(client.UploadValues(API_ENDPOINT, values)); | |
return JsonHelper.Deserialize<CreatePasteResult>(rawJson); | |
} | |
} | |
/// <summary> | |
/// Creates a paste using the specified information. This method does not block the calling thread. | |
/// </summary> | |
/// <param name="title">The title of the paste. Can be empty.</param> | |
/// <param name="content">The content of the paste.</param> | |
/// <param name="password">The password of the paste. Can be empty.</param> | |
/// <param name="encrypted">Whether the paste will be encrypted or not.</param> | |
public void CreatePasteAsync(string title, string content, string password, bool encrypted) | |
{ | |
if (string.IsNullOrWhiteSpace(content)) | |
throw new ArgumentNullException("content"); | |
var values = GetValues(title, content, password, encrypted); | |
using (var client = new WebClient()) | |
{ | |
client.Proxy = null; | |
client.UploadValuesCompleted += client_UploadValuesCompleted; | |
client.UploadValuesAsync(new Uri(API_ENDPOINT), values); | |
} | |
} | |
private void client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e) | |
{ | |
if (e.Cancelled || e.Error != null) | |
CreatePasteCompleted.Invoke(new CreatePasteCompletedEventArgs(new CreatePasteResult(false))); | |
else | |
{ | |
var rawJson = Encoding.UTF8.GetString(e.Result); | |
CreatePasteCompleted.Invoke( | |
new CreatePasteCompletedEventArgs(JsonHelper.Deserialize<CreatePasteResult>(rawJson))); | |
} | |
} | |
private NameValueCollection GetValues(string title, string content, string password, bool encrypted) | |
{ | |
return new NameValueCollection | |
{ | |
{"api_key", ApiKey}, | |
{"title", title}, | |
{"content", content}, | |
{"password", password}, | |
{"encrypted", encrypted.ToString().ToLowerInvariant()} | |
}; | |
} | |
} | |
[DataContract] | |
public class CreatePasteResult | |
{ | |
/// <summary> | |
/// Gets a value indicating whether the operation was successful. | |
/// </summary> | |
public bool Success | |
{ | |
get { return string.Equals(Status, "success"); } | |
} | |
/// <summary> | |
/// Gets the status of the operation. | |
/// </summary> | |
[DataMember(Name = "status")] | |
public string Status { get; set; } | |
/// <summary> | |
/// Gets the message of the operation. Only set when an error has occured. | |
/// </summary> | |
[DataMember(Name = "message")] | |
public string Message { get; set; } | |
/// <summary> | |
/// Gets the link of the created paste. | |
/// </summary> | |
[DataMember(Name = "link")] | |
public string PasteLink { get; set; } | |
/// <summary> | |
/// Gets the deletehash of the created paste. | |
/// </summary> | |
[DataMember(Name = "delete_hash")] | |
public string PasteDeleteHash { get; set; } | |
public CreatePasteResult() { } | |
public CreatePasteResult(bool success) | |
{ | |
Status = (success) ? "success" : "error"; | |
} | |
} | |
/// <summary> | |
/// Provides data for the CreatePasteCompleted event. | |
/// </summary> | |
public class CreatePasteCompletedEventArgs : EventArgs | |
{ | |
/// <summary> | |
/// The CreatePasteResult of the operation. | |
/// </summary> | |
public CreatePasteResult Result { get; private set; } | |
public CreatePasteCompletedEventArgs(CreatePasteResult result) | |
{ | |
Result = result; | |
} | |
} | |
public static class JsonHelper | |
{ | |
public static T Deserialize<T>(string json) | |
{ | |
var s = new DataContractJsonSerializer(typeof(T)); | |
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) | |
{ | |
return (T)s.ReadObject(ms); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment