/Code/Client/Inbox2/Plugins/StatusUpdates/UrlShorteners/BitlyUrlShortener.cs
C# | 56 lines | 47 code | 9 blank | 0 comment | 1 complexity | 5a7c121135dca987936019b8da2e48e1 MD5 | raw file
Possible License(s): BSD-3-Clause
- using System;
- using System.ComponentModel.Composition;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Xml.Linq;
- using Inbox2.Framework.Interfaces.Plugins;
- using Inbox2.Platform.Channels.Extensions;
- using Inbox2.Platform.Logging;
-
- namespace Inbox2.Plugins.StatusUpdates.UrlShorteners
- {
- [Export(typeof(IUrlShortener))]
- public class BitlyUrlShortener : IUrlShortener
- {
- public string Name
- {
- get { return "bit.ly"; }
- }
-
- public string Shorten(string url)
- {
- try
- {
- string bitlyUrl = string.Format("http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl={0}&login={1}&apiKey={2}",
- HttpUtility.UrlEncode(url), "waseem", "R_b36db4f298fcd25827e589aaadd0e0b6");
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(bitlyUrl);
-
- using (HttpWebResponse loWebResponse = (HttpWebResponse)request.GetResponse())
- using (var xmlStream = loWebResponse.GetResponseStream())
- {
- XDocument xmlFeed = XDocument.Parse(xmlStream.ReadString());
-
- var element = xmlFeed.Descendants("nodeKeyVal").First();
- var value = element.Element("shortUrl").Value;
-
- if (String.IsNullOrEmpty(value))
- {
- Logger.Error("Unable to shorten url. Result element = {0}", LogSource.UI, element);
-
- return url;
- }
-
- return value;
- }
- }
- catch (Exception ex)
- {
- Logger.Error("An error has occured while trying to shorten url. Exception = {0}", LogSource.UI, ex);
-
- return url;
- }
- }
- }
- }