Advertisement
tomlev

PasteBin client in C#

Aug 2nd, 2011
3,726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Net;
  5.  
  6. namespace PasteBin
  7. {
  8.     class PasteBinClient
  9.     {
  10.         private const string _apiPostUrl = "http://pastebin.com/api/api_post.php";
  11.         private const string _apiLoginUrl = "http://pastebin.com/api/api_login.php";
  12.  
  13.         private readonly string _apiDevKey;
  14.         private string _userName;
  15.         private string _apiUserKey;
  16.  
  17.         public PasteBinClient(string apiDevKey)
  18.         {
  19.             if (string.IsNullOrEmpty(apiDevKey))
  20.                 throw new ArgumentNullException("apiDevKey");
  21.             _apiDevKey = apiDevKey;
  22.         }
  23.  
  24.         public string UserName
  25.         {
  26.             get { return _userName; }
  27.         }
  28.  
  29.         public void Login(string userName, string password)
  30.         {
  31.             if (string.IsNullOrEmpty(userName))
  32.                 throw new ArgumentNullException("userName");
  33.             if (string.IsNullOrEmpty(password))
  34.                 throw new ArgumentNullException("password");
  35.  
  36.             var parameters = GetBaseParameters();
  37.             parameters[ApiParameters.UserName] = userName;
  38.             parameters[ApiParameters.UserPassword] = password;
  39.  
  40.             WebClient client = new WebClient();
  41.             byte[] bytes = client.UploadValues(_apiLoginUrl, parameters);
  42.             string resp = GetResponseText(bytes);
  43.             if (resp.StartsWith("Bad API request"))
  44.                 throw new PasteBinApiException(resp);
  45.  
  46.             _userName = userName;
  47.             _apiUserKey = resp;
  48.         }
  49.  
  50.         public void Logout()
  51.         {
  52.             _userName = null;
  53.             _apiUserKey = null;
  54.         }
  55.  
  56.         public string Paste(PasteBinEntry entry)
  57.         {
  58.             if (entry == null)
  59.                 throw new ArgumentNullException("entry");
  60.             if (string.IsNullOrEmpty(entry.Text))
  61.                 throw new ArgumentException("The paste text must be set", "entry");
  62.  
  63.             var parameters = GetBaseParameters();
  64.             parameters[ApiParameters.Option] = "paste";
  65.             parameters[ApiParameters.PasteCode] = entry.Text;
  66.             SetIfNotEmpty(parameters, ApiParameters.PasteName, entry.Title);
  67.             SetIfNotEmpty(parameters, ApiParameters.PasteFormat, entry.Format);
  68.             SetIfNotEmpty(parameters, ApiParameters.PastePrivate, entry.Private ? "1" : "0");
  69.             SetIfNotEmpty(parameters, ApiParameters.PasteExpireDate, FormatExpireDate(entry.Expiration));
  70.             SetIfNotEmpty(parameters, ApiParameters.UserKey, _apiUserKey);
  71.  
  72.             WebClient client = new WebClient();
  73.             byte[] bytes = client.UploadValues(_apiPostUrl, parameters);
  74.             string resp = GetResponseText(bytes);
  75.             if (resp.StartsWith("Bad API request"))
  76.                 throw new PasteBinApiException(resp);
  77.             return resp;
  78.  
  79.         }
  80.  
  81.         private static string FormatExpireDate(PasteBinExpiration expiration)
  82.         {
  83.             switch (expiration)
  84.             {
  85.                 case PasteBinExpiration.Never:
  86.                     return "N";
  87.                 case PasteBinExpiration.TenMinutes:
  88.                     return "10M";
  89.                 case PasteBinExpiration.OneHour:
  90.                     return "1H";
  91.                 case PasteBinExpiration.OneDay:
  92.                     return "1D";
  93.                 case PasteBinExpiration.OneMonth:
  94.                     return "1M";
  95.                 default:
  96.                     throw new ArgumentException("Invalid expiration date");
  97.             }
  98.         }
  99.  
  100.         private static void SetIfNotEmpty(NameValueCollection parameters, string name, string value)
  101.         {
  102.             if (!string.IsNullOrEmpty(value))
  103.                 parameters[name] = value;
  104.         }
  105.  
  106.         private NameValueCollection GetBaseParameters()
  107.         {
  108.             var parameters = new NameValueCollection();
  109.             parameters[ApiParameters.DevKey] = _apiDevKey;
  110.            
  111.             return parameters;
  112.         }
  113.  
  114.         private static string GetResponseText(byte[] bytes)
  115.         {
  116.             using (var ms = new MemoryStream(bytes))
  117.             using (var reader = new StreamReader(ms))
  118.             {
  119.                 return reader.ReadToEnd();
  120.             }
  121.         }
  122.  
  123.         private static class ApiParameters
  124.         {
  125.             public const string DevKey = "api_dev_key";
  126.             public const string UserKey = "api_user_key";
  127.             public const string Option = "api_option";
  128.             public const string UserName = "api_user_name";
  129.             public const string UserPassword = "api_user_password";
  130.             public const string PasteCode = "api_paste_code";
  131.             public const string PasteName = "api_paste_name";
  132.             public const string PastePrivate = "api_paste_private";
  133.             public const string PasteFormat = "api_paste_format";
  134.             public const string PasteExpireDate = "api_paste_expire_date";
  135.         }
  136.     }
  137.  
  138.     public class PasteBinApiException : Exception
  139.     {
  140.         public PasteBinApiException(string message)
  141.             : base(message)
  142.         {
  143.         }
  144.     }
  145.  
  146.     public class PasteBinEntry
  147.     {
  148.         public string Title { get; set; }
  149.         public string Text { get; set; }
  150.         public string Format { get; set; }
  151.         public bool Private { get; set; }
  152.         public PasteBinExpiration Expiration { get; set; }
  153.     }
  154.  
  155.     public enum PasteBinExpiration
  156.     {
  157.         Never,
  158.         TenMinutes,
  159.         OneHour,
  160.         OneDay,
  161.         OneMonth
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement