Skip to content

Commit

Permalink
Introduced versioning via Accept header with our own media type
Browse files Browse the repository at this point in the history
  • Loading branch information
Joakim Skoog committed Jan 2, 2016
1 parent 45aa639 commit 4d786fd
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Results;
using AnApiOfIceAndFire.Controllers;
using AnApiOfIceAndFire.Controllers.v0;
using AnApiOfIceAndFire.Domain;
using AnApiOfIceAndFire.Domain.Models;
using AnApiOfIceAndFire.Models.v0;
Expand Down
8 changes: 5 additions & 3 deletions AnApiOfIceAndFire/AnApiOfIceAndFire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@
<Compile Include="App_Start\UnityConfig.cs" />
<Compile Include="App_Start\UnityWebApiActivator.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\BooksController.cs" />
<Compile Include="Controllers\CharactersController.cs" />
<Compile Include="Controllers\v0\BooksController.cs" />
<Compile Include="Controllers\v0\CharactersController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\HousesController.cs" />
<Compile Include="Controllers\v0\HousesController.cs" />
<Compile Include="Controllers\v1\BooksController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Infrastructure\AcceptHeaderControllerSelector.cs" />
<Compile Include="Models\v0\Book.cs" />
<Compile Include="Models\v0\Character.cs" />
<Compile Include="Models\v0\House.cs" />
Expand Down
12 changes: 12 additions & 0 deletions AnApiOfIceAndFire/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using AnApiOfIceAndFire.Infrastructure;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
Expand Down Expand Up @@ -29,9 +32,18 @@ public static void Register(HttpConfiguration config)
//We want to represent our enums with their names instead of their numerical values. This is to make it more readable for the consumer.
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());

//Add our own media type to enable versioning via the accept header. Make this sexier, maybe use reflection to reflect all current namespaces?
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(AcceptHeaderControllerSelector.AllowedAcceptHeaderMediaType)
{
Parameters = { new NameValueHeaderValue(AcceptHeaderControllerSelector.AllowedAcceptHeaderMediaTypeParamter, "0") }
});

//Remove the possibility to serialize models to XML since we don't want to support that at the moment.
config.Formatters.Remove(config.Formatters.XmlFormatter);

//Replace the default IHttpControllerSelector with our own that selects controllers based on Accept header and namespaces.
config.Services.Replace(typeof(IHttpControllerSelector), new AcceptHeaderControllerSelector(config));

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Description;
using AnApiOfIceAndFire.Domain;
using AnApiOfIceAndFire.Models.v0;

namespace AnApiOfIceAndFire.Controllers
namespace AnApiOfIceAndFire.Controllers.v0
{
public class BooksController : ApiController
{
private readonly IBookService _bookService;

public BooksController(IBookService bookService)
{
if (bookService == null) throw new ArgumentNullException(nameof(bookService));
_bookService = bookService;
}
//public BooksController(IBookService bookService)
//{
// if (bookService == null) throw new ArgumentNullException(nameof(bookService));
// _bookService = bookService;
//}

[HttpGet]
[ResponseType(typeof(Book))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Description;
using AnApiOfIceAndFire.Models;
using AnApiOfIceAndFire.Models.v0;

namespace AnApiOfIceAndFire.Controllers
namespace AnApiOfIceAndFire.Controllers.v0
{
public class CharactersController : ApiController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Description;
using AnApiOfIceAndFire.Models;
using AnApiOfIceAndFire.Models.v0;

namespace AnApiOfIceAndFire.Controllers
namespace AnApiOfIceAndFire.Controllers.v0
{
public class HousesController : ApiController
{
Expand Down
162 changes: 162 additions & 0 deletions AnApiOfIceAndFire/Infrastructure/AcceptHeaderControllerSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Http.Routing;

namespace AnApiOfIceAndFire.Infrastructure
{
public class AcceptHeaderControllerSelector : IHttpControllerSelector
{
public const string AllowedAcceptHeaderMediaType = "application/vnd.anapioficeandfire+json";
public const string AllowedAcceptHeaderMediaTypeParamter = "version";

private const string ControllerKey = "controller";

private readonly HttpConfiguration _configuration;

private readonly Lazy<IDictionary<string, HttpControllerDescriptor>> _controllers;
private readonly ICollection<string> _duplicates;

private string _defaultControllerVersion;

public AcceptHeaderControllerSelector(HttpConfiguration configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
_configuration = configuration;
_duplicates = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_controllers = new Lazy<IDictionary<string, HttpControllerDescriptor>>(InitialiseControllerDictionary);
}

public HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
IHttpRouteData routeData = request.GetRouteData();
if (routeData == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}

string version = GetVersionFromMediaType(request);
if (version == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}

string controllerName = GetRouteVariable<string>(routeData, ControllerKey);
if (controllerName == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}

// Find a matching controller.
string key = string.Format(CultureInfo.InvariantCulture, "v{0}.{1}", version, controllerName);

HttpControllerDescriptor controllerDescriptor;
if (_controllers.Value.TryGetValue(key, out controllerDescriptor))
{
return controllerDescriptor;
}
if (_duplicates.Contains(key))
{
throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Multiple controllers were found that match this request."));
}

throw new HttpResponseException(HttpStatusCode.NotFound);
}

public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
{
return _controllers.Value;
}

private IDictionary<string, HttpControllerDescriptor> InitialiseControllerDictionary()
{
var dictionary = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);
var versionedNamespaces = new List<string>();

// Create a lookup table where key is "namespace.controller". The value of "namespace" is the last
// segment of the full namespace. For example:
// MyApplication.Controllers.V1.ProductsController => "V1.Products"
IAssembliesResolver assembliesResolver = _configuration.Services.GetAssembliesResolver();
IHttpControllerTypeResolver controllersResolver = _configuration.Services.GetHttpControllerTypeResolver();

ICollection<Type> controllerTypes = controllersResolver.GetControllerTypes(assembliesResolver);

foreach (Type t in controllerTypes)
{
var segments = t.Namespace.Split(Type.Delimiter);

// For the dictionary key, strip "Controller" from the end of the type name.
// This matches the behavior of DefaultHttpControllerSelector.
var controllerName = t.Name.Remove(t.Name.Length - DefaultHttpControllerSelector.ControllerSuffix.Length);
var namespaceName = segments[segments.Length - 1];

versionedNamespaces.Add(namespaceName.Remove(0,1)); //Remove the first character which should be v. Since we will add v whilst selecting the controller

var key = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", namespaceName, controllerName);

// Check for duplicate keys.
if (dictionary.Keys.Contains(key))
{
_duplicates.Add(key);
}
else
{
dictionary[key] = new HttpControllerDescriptor(_configuration, t.Name, t);
}
}

// Remove any duplicates from the dictionary, because these create ambiguous matches.
// For example, "Foo.V1.ProductsController" and "Bar.V1.ProductsController" both map to "v1.products".
foreach (string s in _duplicates)
{
dictionary.Remove(s);
}

//Calculate the default version, we choose to use the highest available version as the default one
_defaultControllerVersion = versionedNamespaces.OrderByDescending(x => x).First();

return dictionary;
}

// Get a value from the route data, if present.
private static T GetRouteVariable<T>(IHttpRouteData routeData, string name)
{
object result;
if (routeData.Values.TryGetValue(name, out result))
{
return (T)result;
}
return default(T);
}

private string GetVersionFromMediaType(HttpRequestMessage request)
{
var acceptHeader = request.Headers.Accept;

foreach (var mime in acceptHeader.OrderByDescending(x => x.Quality))
{
if (string.Equals(mime.MediaType, AllowedAcceptHeaderMediaType, StringComparison.InvariantCultureIgnoreCase))
{
var version = mime.Parameters.FirstOrDefault(x => x.Name.Equals(AllowedAcceptHeaderMediaTypeParamter, StringComparison.InvariantCultureIgnoreCase));

if (version == null)
{
return _defaultControllerVersion;
}

return version.Value;
}

}

return _defaultControllerVersion;
}
}
}

0 comments on commit 4d786fd

Please sign in to comment.