Paste: bit.ly API Testing
Author: | Adrian |
Mode: | c# |
Date: | Fri, 3 Jun 2011 10:19:35 |
Plain Text |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using bitly.NET;
namespace ConsoleApplication1
{
class Program
{
static BitlyAPI myAPI = new BitlyAPI("myapihere", "myaccesskeyhere");
static void Main(string[] args)
{
string Again = "";
int Choice = -1;
do
{
Console.Clear();
Console.WriteLine("----------------------------------");
Console.WriteLine(" Welcome to bitly.NET testing");
Console.WriteLine("----------------------------------");
Console.WriteLine(" Options:");
Console.WriteLine("----------------------------------");
Console.WriteLine(" 1. Shorten URL");
Console.WriteLine(" 2. Expand URL");
Console.WriteLine(" 3. Validate Credentials");
Console.WriteLine(" 4. Get URL Clicks");
Console.WriteLine(" 5. Get URL Referrer");
Console.WriteLine("");
Console.Write("> ");
int.TryParse(Console.ReadLine(), out Choice);
switch (Choice)
{
case 1:
Shorten();
break;
case 2:
Expand();
break;
case 3:
Validate();
break;
case 4:
Clicks();
break;
case 5:
Referrer();
break;
default:
Console.WriteLine("Unknown Response");
continue;
}
Console.Write("Would you like to continue (Y/N)? ");
switch (Console.ReadLine())
{
case "Y":
case "y":
Again = "y";
break;
case "N":
case "n":
Again = "n";
break;
default:
Again = "y";
continue;
}
} while (Again != "n");
}
static void Shorten()
{
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" bit.ly URL Shortener");
Console.WriteLine("-------------------------");
Console.WriteLine("");
Console.Write("Enter URL to Shorten: ");
string longURL = Console.ReadLine();
ShortURL shortURL = myAPI.ShortenURL(longURL);
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" URL Information");
Console.WriteLine("-------------------------");
if (shortURL.StatusCode != 500)
{
Console.WriteLine("Status Text: " + shortURL.StatusText);
Console.WriteLine("Status Code: " + shortURL.StatusCode);
Console.WriteLine("Shorten URL: " + shortURL.URL);
Console.WriteLine("Long URL : " + shortURL.LongURL);
Console.WriteLine("URL Hash : " + shortURL.Hash);
Console.WriteLine("Global Hash: " + shortURL.GlobalHash);
Console.WriteLine("New Hash? : " + (shortURL.NewHash == true ? "Yes" : "No"));
Console.WriteLine("");
}
else
{
Console.WriteLine("Error: " + shortURL.StatusText);
}
}
static void Expand()
{
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" bit.ly URL Expander");
Console.WriteLine("-------------------------");
Console.WriteLine("");
Console.Write("Enter Short URL: ");
string shortURL = Console.ReadLine();
ExpandURL expandedURL = myAPI.ExpandURL(shortURL);
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" URL Information");
Console.WriteLine("-------------------------");
if (expandedURL.StatusCode != 500)
{
Console.WriteLine("Status Text: " + expandedURL.StatusText);
Console.WriteLine("Status Code: " + expandedURL.StatusCode);
Console.WriteLine("Shorten URL: " + expandedURL.URL);
Console.WriteLine("Long URL : " + expandedURL.LongURL);
Console.WriteLine("URL Hash : " + expandedURL.Hash);
Console.WriteLine("Global Hash: " + expandedURL.GlobalHash);
}
else
{
Console.WriteLine("Error: " + expandedURL.StatusText);
}
Console.WriteLine("");
}
static void Validate()
{
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" bit.ly Validater");
Console.WriteLine("-------------------------");
Console.WriteLine("");
Console.Write("Enter Login : ");
string Login = Console.ReadLine();
Console.Write("Enter APIKey: ");
string APIKey = Console.ReadLine();
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" Credential Info");
Console.WriteLine("-------------------------");
Authentication.LoginError error = myAPI.ValidateAPICredentials(Login, APIKey);
if (error == Authentication.LoginError.InvalidAPIKey) Console.WriteLine("Invalid API Key");
if (error == Authentication.LoginError.InvalidLogin) Console.WriteLine("Invalid Login");
if (error == Authentication.LoginError.Valid) Console.WriteLine("Credentials Valid.");
Console.WriteLine("");
}
static void Clicks()
{
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" bit.ly URL Clicks");
Console.WriteLine("-------------------------");
Console.WriteLine("");
Console.Write("Enter Short URL: ");
string shortURL = Console.ReadLine();
Clicks clickedURL = myAPI.GetClicks(shortURL);
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" URL Information");
Console.WriteLine("-------------------------");
if (clickedURL.StatusCode != 500)
{
Console.WriteLine("Status Text: " + clickedURL.StatusText);
Console.WriteLine("Status Code: " + clickedURL.StatusCode);
Console.WriteLine("Shorten URL: " + clickedURL.URL);
Console.WriteLine("URL Hash : " + clickedURL.Hash);
Console.WriteLine("Global Hash: " + clickedURL.GlobalHash);
Console.WriteLine("User Clicks: " + clickedURL.UserClicks);
Console.WriteLine("Global Clix: " + clickedURL.GlobalClicks);
}
else
{
Console.WriteLine("Error: " + clickedURL.StatusText);
}
Console.WriteLine("");
}
static void Referrer()
{
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" bit.ly URL Referrer");
Console.WriteLine("-------------------------");
Console.WriteLine("");
Console.Write("Enter Short URL: ");
string shortURL = Console.ReadLine();
Referrer shortenedURL = myAPI.GetReferrer(shortURL);
Console.Clear();
Console.WriteLine("-------------------------");
Console.WriteLine(" URL Information");
Console.WriteLine("-------------------------");
if (shortenedURL.StatusCode != 500)
{
Console.WriteLine("Status Text: " + shortenedURL.StatusText);
Console.WriteLine("Status Code: " + shortenedURL.StatusCode);
Console.WriteLine("Shorten URL: " + shortenedURL.URL);
Console.WriteLine("URL Hash : " + shortenedURL.Hash);
Console.WriteLine("Global Hash: " + shortenedURL.GlobalHash);
Console.WriteLine("User Clicks: " + shortenedURL.UserClicks);
if (shortenedURL.CreateBy != "") Console.WriteLine("Created By : " + shortenedURL.CreateBy);
if (shortenedURL.URLReferrer != "") Console.WriteLine("URL Ref. : " + shortenedURL.URLReferrer);
if (shortenedURL.ReferrerApp != "") Console.WriteLine("Ref. App : " + shortenedURL.ReferrerApp);
if (shortenedURL.ReferrerURL != "") Console.WriteLine("App URL : " + shortenedURL.ReferrerURL);
}
else
{
Console.WriteLine("Error: " + shortenedURL.StatusText);
}
Console.WriteLine("");
}
}
}
New Annotation