I'm trying to apply HATEOAS to the existing application and I'm having trouble with modeling a form inputs that would be driven by the API response.
The app is allowing to search & book connections between two places. First endpoint allows for searching the connections GET /connections?from={lat,lon}&to={lat,lon}&departure={dateTime}
and returns following payload (response body).
[
{
"id": "aaa",
"carrier": "Fast Bus",
"price": 3.20,
"departure": "2019-04-05T12:30"
},
{
"id": "bbb",
"carrier": "Airport Bus",
"price": 4.60,
"departure": "2019-04-05T13:30"
},
{
"id": "ccc",
"carrier": "Slow bus",
"price": 1.60,
"departure": "2019-04-05T11:30"
}
]
In order to make an order for one of connections, the client needs to make a POST /orders
request with one of following payloads (request body):
- email required
{ "connectionId": "aaa", "email": "[email protected]" }
- email & flight number required (carrier handles only aiprort connections)
{ "connectionId": "bbb", "email": "[email protected]", "flightNumber": "EA1234" }
- phone number required
{ "connectionId": "ccc", "phoneNumber": "+44 111 222 333" }
The payload is different, because different connections may be handled by different carriers and each of them may require some different set of information to provide. I would like to inform the API client, what fields are required when creating an order. The question I have is how do I do this with HATEOAS?
I checked different specs and this is what I could tell from reading the specs:
- HAL & HAL-FORMS There are
"_templates"
but, there is no URI in the template itself. It’s presumed to operate on the self link, which in my case would be /connections... not /orders. - JSON-LD I couldn't find anything about forms or templates support.
- JSON-API I couldn't find anything about forms or templates support.
- Collection+JSON There is at most one
"template"
per document, therefore it's presumed that all elements of the collection have the same fields which is not the case in my app. - Siren Looks like the
"actions"
would fit my use case, but the project seems dead and there are no supporting libraries for many major languages. - CPHL The project seems dead, very little documentation and no libraries.
- Ion There is nice support for forms, but I couldn't find any supporting libraries. Looks like it's just a spec for now.
Is such a common problem as having forms driven by the API still unsolved with spec and tooling?