Refactor the existing API implementation to use a modern standard for building hypermedia/HATEOAS APIs. We've chosen the JSON HAL format as it's one of the most well-known standards to date, is easily ignored by clients who don't care about hypermedia and thus recommended for use in public APIs and doesn't mess around with the response too much.
This PR uses a representer pattern to implement this via the Roar gem (and helper gems). The initial plan was to use Activerecord Serializers but this doesn't currently include JSON HAL adapters (only JSON API), and Roar seemed more flexible to work with.
We now also define a custom mime type :hal.
Example calls: /drawings.hal?page=1 (collections, paginated) or /drawings/10.hal (single resource). See example usage below.
Note
This is attempt 1 at defining what our hypermedia endpoints will look like. There are still many things to consider to create a client-self-navigable API, on top of versioning, caching etc. but working iteratively for now. TODO in a next PR:
Expose more fields, including image URLs. This follows what we currently export in CSV for now, minus organisation data.
Add previous/next links to collection representation (and possibly single rep)
Move the API into its own namespace and add versioning, so example calls would be /api/v1/drawings (and no need to post-fix .hal mimetype)
Tasks
[x] Implement the ability to request a JSON HAL representation of a single drawing resource exposing select fields, e.g. /drawings/1.hal
[x] Implement the ability to request a JSON HAL representation of a paginated drawing collection, e.g. /drawings/10.hal
Example Requests
Single Resource: /drawings/1.hal
{
"country": "GR",
"age": 10,
"gender": "female",
"mood_rating": 5,
"description": "Children and their families in the sea, playing, sunny",
"story": "Back story of artist",
"created_at": "2016-08-07T15:44:43.009Z",
"_links": {
"self": {
"href": "/drawings/7.hal"
}
}
}
Addresses issue: #37
What this does
Refactor the existing API implementation to use a modern standard for building hypermedia/HATEOAS APIs. We've chosen the JSON HAL format as it's one of the most well-known standards to date, is easily ignored by clients who don't care about hypermedia and thus recommended for use in public APIs and doesn't mess around with the response too much.
This PR uses a representer pattern to implement this via the Roar gem (and helper gems). The initial plan was to use Activerecord Serializers but this doesn't currently include JSON HAL adapters (only JSON API), and Roar seemed more flexible to work with.
We now also define a custom mime type
:hal
.Example calls: /drawings.hal?page=1 (collections, paginated) or /drawings/10.hal (single resource). See example usage below.
Note
This is attempt 1 at defining what our hypermedia endpoints will look like. There are still many things to consider to create a client-self-navigable API, on top of versioning, caching etc. but working iteratively for now. TODO in a next PR:
Tasks
Example Requests
Single Resource: /drawings/1.hal
Paginated collection: /drawings.hal?page=1