Skip to content

Instantly share code, notes, and snippets.

@hounsell
Last active August 29, 2015 13:56
Show Gist options
  • Save hounsell/9315045 to your computer and use it in GitHub Desktop.
Save hounsell/9315045 to your computer and use it in GitHub Desktop.
Quick and Dirty API Proxy for TCB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Caching;
using System.Net;
using System.Configuration;
using System.IO;
namespace TcbApiProxy
{
/// <summary>
/// Summary description for ApiProxy
/// </summary>
public class ApiProxy : IHttpHandler
{
private static ObjectCache requestCache;
static ApiProxy()
{
requestCache = MemoryCache.Default;
}
public void ProcessRequest(HttpContext context)
{
string type = context.Request.QueryString["type"];
string id = context.Request.QueryString["id"];
if (!string.IsNullOrEmpty(type))
{
string content = "ERR";
switch (type)
{
case "root":
content = FetchContentFromCacheOrLive(type, ConfigurationManager.AppSettings["RootUrl"]);
context.Response.ContentType = "application/json";
context.Response.Write(content);
break;
case "ver":
if (!string.IsNullOrEmpty(id))
{
try
{
content = FetchContentFromCacheOrLive(string.Format("{0},id={1}", type, id), string.Format(ConfigurationManager.AppSettings["VerUrl"], id));
context.Response.ContentType = "application/json";
context.Response.Write(content);
}
catch (Exception ex)
{
if (ex.Message == "404")
{
Throw404(context);
}
else
{
Throw500(context);
}
}
}
else
{
Throw404(context);
}
break;
case "bld":
if (!string.IsNullOrEmpty(id))
{
try
{
content = FetchContentFromCacheOrLive(string.Format("{0},id={1}", type, id), string.Format(ConfigurationManager.AppSettings["BldUrl"], id));
context.Response.ContentType = "application/json";
context.Response.Write(content);
}
catch (Exception ex)
{
if (ex.Message == "404")
{
Throw404(context);
}
else
{
Throw500(context);
}
}
}
else
{
Throw404(context);
}
break;
default:
Throw404(context);
break;
}
}
else
{
Throw404(context);
}
}
public static string FetchContentFromCacheOrLive(string name, string url)
{
if (requestCache.Contains(name))
{
return requestCache[name] as string;
}
else
{
HttpWebRequest wreq = HttpWebRequest.CreateHttp(url);
wreq.Headers.Add("Api-Key", ConfigurationManager.AppSettings["ApiKey"]);
wreq.UserAgent = "Tom's friendly API Proxy - Hi Rob!";
HttpWebResponse wres = wreq.GetResponse() as HttpWebResponse;
if (wres.StatusCode == HttpStatusCode.OK)
{
Stream str = wres.GetResponseStream();
TextReader tr = new StreamReader(str);
string content = tr.ReadToEnd();
tr.Close();
str.Close();
CacheItemPolicy p = new CacheItemPolicy();
p.AbsoluteExpiration = DateTime.Now.AddHours(1);
requestCache.Add(new CacheItem(name, content), p);
return content;
}
else if (wres.StatusCode == HttpStatusCode.NotFound)
{
throw new Exception("404");
}
else
{
throw new Exception("500");
}
}
}
private static void Throw404(HttpContext context)
{
context.Response.StatusCode = 404;
context.Response.SuppressContent = true;
context.Response.Flush();
context.Response.Close();
}
private static void Throw500(HttpContext context)
{
context.Response.StatusCode = 500;
context.Response.SuppressContent = true;
context.Response.Flush();
context.Response.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment