Last active
December 15, 2015 09:49
-
-
Save jesperbjensen/5241293 to your computer and use it in GitHub Desktop.
A little helper class that helps shorten URL's from Bit.ly
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 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