Created
February 14, 2012 16:21
-
-
Save i-e-b/1827873 to your computer and use it in GitHub Desktop.
Slap-on-top rest services
This file contains hidden or 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 System; | |
using System.Reflection; | |
using System.Web; | |
namespace Host | |
{ | |
public class Global : HttpApplication | |
{ | |
protected void Application_BeginRequest (object sender, EventArgs e) { | |
if (HttpContext.Current.Request.RawUrl.StartsWith(RestHandler.RestPrefix)) { | |
Server.Transfer(new RestHandler(), true); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 System.Collections.Specialized; | |
namespace Rest { | |
public interface IRestResponder { | |
string ServiceName { get; } | |
object Handle (NameValueCollection parameters); | |
} | |
} |
This file contains hidden or 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 System; | |
using System.Collections.Specialized; | |
using MediaFreedom.Content.Contracts; | |
using MediaFreedom.Content.Delivery.Contracts; | |
using MediaFreedom.Delivery.Application; | |
using StructureMap; | |
namespace Rest { | |
/// <summary> | |
/// Sample call --> http://example.com/api/v1/sample?id=myref | |
/// </summary> | |
public class MediaGet : IRestResponder{ | |
public string ServiceName { | |
get { return "sample"; } | |
} | |
public object Handle(NameValueCollection parameters) { | |
var id = Guid.Parse(parameters["id"]); | |
// ... do a bunch of work here ... | |
return new { | |
sampleValue = "hello, world", | |
yourId = id.ToString() | |
}; | |
} | |
} | |
} |
This file contains hidden or 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 System.Web; | |
using System.Web.Script.Serialization; | |
using System.Web.UI; | |
using Rest; | |
namespace Host { | |
public class RestHandler : Page, IHttpHandler { // 'Page' required for Server.Transfer in Global.asax | |
public const string RestPrefix = "/api/v1/"; | |
new public void ProcessRequest (HttpContext context) { | |
var service = context.Request.Url.AbsolutePath.Replace(RestPrefix, ""); | |
var obj = GetResponseObject(context, service); | |
if (obj == null) return; | |
context.Response.AddHeader("Content-Disposition", "inline;filename=\"" + service + ".json\""); | |
context.Response.ContentType = "application/json"; | |
var js = new JavaScriptSerializer(); | |
context.Response.Write(js.Serialize(obj)); | |
} | |
object GetResponseObject (HttpContext context, string service) { | |
foreach (var responder in RestResponders.All()) { | |
if (service == responder.ServiceName) { | |
return responder.Handle(context.Request.Params); | |
} | |
} | |
context.Response.StatusCode = 404; | |
context.Response.Flush(); | |
context.Response.End(); | |
return null; | |
} | |
} | |
} |
This file contains hidden or 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 System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Net; | |
using Rest.Sample; | |
namespace Rest { | |
public class RestResponders { | |
public static IEnumerable<IRestResponder> All () { | |
return new IRestResponder[] { | |
new MySampleResponder(), | |
new OtherResponder() | |
//...etc... | |
// can do this with IoC instead | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment