I'm curious to know how others have dealt with the issue of generating hypermedia links for their web APIs? Specifically, I'm using ASP.NET Web API, and am torn between having operations return hypermedia-related types, or returning the resource itself, and having the hypermedia stuff happen later in the pipeline. That is, do people tend to do things like:
public Resource<Order> GetOrder(int id) { return new Resource<Order>() { Content = new Order(), Links = new LinkCollection<Order>() { new AddOrderLink(), new UpdateOrderLink()} }
Or something more like
public Order GetOrder(int id) { return new Order(); }
And then add hypermedia links inside an HttpOperationHandler or custom formatter or something?
If the approach is more like #2, how do you know what links to generate? Just have some standard set of links that get generated for all Order objects? Attributes decorating various operations in OrdersController?
REST architectural style lets us use the hypermedia links in the API response contents. It allows the client to dynamically navigate to the appropriate resources by traversing the hypermedia links.
A hypermedia API is an API that returns hypermedia, typically HTML over HTTP. This style of API is distinguished from data APIs that do not return a hypermedia. The most familiar form of this latter style of API today is the ubiquitous JSON API.
1. What is HATEOAS? HATEOAS is a constraint on REST that says that a client of a REST application need only know a single fixed URL to access it. Any and all resources should be discoverable dynamically from that URL through hyperlinks included in the representations of returned resources.
I prefer option two (adding the hypermedia links later in the pipeline) and blogged about doing this yesterday.
The solution was to "enrich" my resources with hypermedia links before they are returned to the client using a message handler.
You can use the Hyprlinkr from github
I'm planning to use it in my next project as it seens to be nice and easy to do it and you can get it via nuget package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With