Posted By

CVertex on 08/14/09


Tagged

url http c bitly urlshortening


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

LeeRJohnson
umang_nine
vali29
emregulcan


URL Shortening Service Contract w/ Bitly Implementation


 / Published in: C#
 

  1. public interface IUrlShorteningService
  2. {
  3. string Shorten(string longUrl);
  4. }
  5.  
  6. public class UrlShorteningServiceException : Exception {
  7. public UrlShorteningServiceException(string message)
  8. : base(message) {
  9. }
  10.  
  11. public UrlShorteningServiceException(string message, Exception innerException)
  12. : base(message, innerException) {
  13. }
  14. }
  15.  
  16. public class BitlyServiceException : UrlShorteningServiceException {
  17.  
  18. public BitlyServiceException(int errorCode, string message)
  19. : base(message) {
  20. ErrorCode = errorCode;
  21. }
  22.  
  23.  
  24. public BitlyServiceException(int errorCode, string message, Exception innerException)
  25. : base(message, innerException) {
  26. ErrorCode = errorCode;
  27. }
  28.  
  29. public int ErrorCode { get; private set; }
  30. }
  31.  
  32. public class BitlyShorteningService : IUrlShorteningService {
  33. private const string _version = "2.0.1";
  34. private readonly string _apiKey;
  35. private readonly string _login;
  36. private bool _useCache = false;
  37. private Func<string, string> _shortener;
  38. private readonly JavaScriptSerializer _javaScriptSerializer;
  39.  
  40. //http://api.bit.ly/shorten?version=2.0.1&longUrl=http://cnn.com&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07
  41. const string _baseShortenUrl = "http://api.bit.ly/shorten";
  42.  
  43. #region ctors
  44.  
  45. public BitlyShorteningService(string login, string apiKey, bool useCache) {
  46. _login = login;
  47. _apiKey = apiKey;
  48. _javaScriptSerializer = new JavaScriptSerializer();
  49. UseCache = useCache;
  50. EnsureShortenerCore();
  51. }
  52.  
  53. public BitlyShorteningService(string login, string apiKey):this(login,apiKey,false) { }
  54.  
  55. #endregion
  56.  
  57. #region Caching
  58.  
  59. public bool UseCache {
  60. get {
  61. return _useCache;
  62. }
  63. set {
  64. if (value == _useCache) // no change
  65. {
  66. return;
  67. }
  68.  
  69. // use cache has changed, so change the shortener
  70. _useCache = value;
  71. SetShortenerCore();
  72. }
  73. }
  74.  
  75. private void EnsureShortenerCore()
  76. {
  77. if (_shortener==null)
  78. {
  79. SetShortenerCore();
  80. }
  81. }
  82.  
  83. private void SetShortenerCore()
  84. {
  85. if (_useCache)
  86. {
  87. _shortener = new Func<string, string>(ShortenCore).Memoize();
  88. } else
  89. {
  90. _shortener = ShortenCore;
  91. }
  92.  
  93.  
  94. }
  95.  
  96.  
  97. #endregion
  98.  
  99. #region Bit.ly objects
  100.  
  101. class Response<T> {
  102. public int errorCode;
  103. public string errorMessage;
  104. public Dictionary<string, T> results;
  105. }
  106.  
  107. class Result {
  108. public string hash;
  109. public string userHash;
  110. public string shortKeywordUrl;
  111. public string shortUrl;
  112.  
  113.  
  114. }
  115.  
  116. #endregion
  117.  
  118. #region Shortening
  119.  
  120. public string Shorten(string longUrl)
  121. {
  122. if (_shortener == null)
  123. return "";
  124.  
  125. return _shortener(longUrl);
  126. }
  127.  
  128. protected virtual string ShortenCore(string longUrl) {
  129. if (longUrl == null) throw new ArgumentNullException("longUrl");
  130.  
  131. try {
  132.  
  133. var uri = new UrlBuilder(_baseShortenUrl);
  134. uri.QueryString["login"] = _login;
  135. uri.QueryString["apiKey"] = _apiKey;
  136. uri.QueryString["version"] = _version;
  137. uri.QueryString["longUrl"] = longUrl;
  138.  
  139. using (var web = new WebClient()) {
  140. var resultString = web.DownloadString(uri.ToString());
  141. var result = _javaScriptSerializer.Deserialize<Response<Result>>(resultString);
  142. if (result.errorCode != 0) {
  143. throw new BitlyServiceException(result.errorCode, result.errorMessage);
  144. }
  145. return result.results[longUrl].shortUrl;
  146. }
  147.  
  148.  
  149.  
  150. } catch (Exception ex) {
  151. throw new UrlShorteningServiceException(String.Format("Error shortening long URL {0} with bit.ly",longUrl), ex);
  152. }
  153.  
  154.  
  155. }
  156.  
  157. #endregion
  158. }

Report this snippet  

You need to login to post a comment.