Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
@darrelmiller
darrelmiller / gist:2703205
Created May 15, 2012 16:51
Hierarchical MVC routing
var router = new ApiRouter("api").DispatchTo<RootController>()
.Add(new ApiRouter("Contacts").DispatchTo<ContactsController>())
.Add(new ApiRouter("Contact").WithHandler(new LoggingHandler())
.Add(new ApiRouter("{contactid}")
.WithConstraint("contactid", @"\d+")
.DispatchTo<ContactController>()
.Add(new ApiRouter("Address")
.Add(new ApiRouter("{addressid}")
GET POST /gists
GET /gists/public
GET /gists/starred
GET PATCH DELETE /gists/:id
GET /gists/:gist_id/comments
PUT /gists/:id/star
POST /gists/:id/fork
public interface IHttpService<T> {
HttpResponseMessage<T> Get(HttpRequestMessage<T> request);
HttpResponseMessage<T> Put(HttpRequestMessage<T> request);
HttpResponseMessage<T> Post(HttpRequestMessage<T> request);
HttpResponseMessage<T> Delete(HttpRequestMessage<T> request);
}
@darrelmiller
darrelmiller / gist:3018588
Created June 29, 2012 15:24
Xml Hal version of Mnot's json-problem example
<resource href="/ErrorLog/2323234">
<resource rel="http://example.com/probs/out-of-credit" ref="/ErrorLog/2323234/details">
<title>"You do not have enough credits."</title>
<detail>"Your current balance is 30, but that costs 50."</title>
<balance>30</balance>
<account>http://api.example.com/account/12345</account>
</resource>
</resource>
@darrelmiller
darrelmiller / gist:3628687
Created September 5, 2012 01:00
Perf testing Web API
// Server
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
@darrelmiller
darrelmiller / gist:3629815
Created September 5, 2012 03:21
Alternative handler
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<HttpResponseMessage>();
Interlocked.Increment(ref count);
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("HELLO WORLD")
};
@darrelmiller
darrelmiller / gist:3698177
Created September 11, 2012 12:47
A replacement for the old HttpResponseMessage<T>
public class HttpResponseMessage<T> : HttpResponseMessage
{
public HttpResponseMessage(HttpRequestMessage request, T value)
{
var config = request.GetConfiguration();
var contentNegotiator = config.Services.GetContentNegotiator();
var connegResult = contentNegotiator.Negotiate(
typeof(T), request, config.Formatters
);
request.CreateResponse(HttpStatusCode.Accepted, value);
@darrelmiller
darrelmiller / gist:3698325
Created September 11, 2012 13:07
Some Web API techniques
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace WebApiSelfHost
{
class Program
{
//Web API using WCF to Self-host
//Time to create init Server and Start HttpListener: 0
//Time for roundtrip: 302 body = {"Foo":"Bar"}
//Time for 2nd roundtrip (new WebRequest): 0 Body = {"Foo":"Bar"}
//Servicestack
//Time to create init AppHost and Start HttpListener: 129
//Time for roundtrip: 176 body = {"Foo":"Bar"}
//Time for 2nd roundtrip (new WebRequest): 0 Body = {"Foo":"Bar"}
public class UnitTests {
[Fact]
public void RoundTripUserAgent() {
var request = new HttpRequestMessage();
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("foo", "1.2"));
var userAgent = request.Headers.UserAgent.ToString();
var strongUserAgent = ProductInfoHeaderValue.Parse(userAgent);
Assert.Equal("foo", strongUserAgent.Product.Name);