Skip to content

Instantly share code, notes, and snippets.

@jesperbjensen
Last active December 15, 2015 09:49
Show Gist options
  • Save jesperbjensen/5241293 to your computer and use it in GitHub Desktop.
Save jesperbjensen/5241293 to your computer and use it in GitHub Desktop.
A little helper class that helps shorten URL's from Bit.ly
using BitlyDotNET.Implementations;
using BitlyDotNET.Interfaces;
//NUGET:
// Install-Package Bitly.Net
//Get username and apiKey from BIT.LY - you can get it under your account, settings > advanced and then Legacy API Key.
public class UrlShortenerHelper
{
public static void Setup(string username, string apiKey)
{
Username = username;
ApiKey = apiKey;
Initialized = true;
}
public static string ShortenUrl(string url)
{
GuardInitialized();
IBitlyService s = new BitlyService(Username, ApiKey);
return s.Shorten(url);
}
private static void GuardInitialized()
{
if (!Initialized)
throw new InvalidOperationException("You need to call setup first.");
}
public static bool Initialized { get; protected set; }
protected static string ApiKey { get; set; }
protected static string Username { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment