I'm building a JSON API for clients that need meaningful, specific errors. Everything has been going swimmingly for endpoints involving a single resource type, but we're facing challenges for requests involving multiple resource types (all operations should fail if one does).
One idea we're considering is having clients submit an array of operations that resemble single-resource requests and returning an array containing a response to each. Then we could wrap all calls in a transaction and roll it back if a resource fails to save.
A request might look like this:
{
"operations": [
{
"path": "/orders",
"action": "POST",
"data": {
"client_id": 1
}
},
{
"path": "/charges",
"action": "POST",
"data": {
"amount": 300,
},
"relationships": {
"order": {
"id": {
"pointer": { "/operations/0/data" }
}
}
}
}
]
}
And a response might look like:
{
"operations": [
{
"status": "201",
"data": {}
},
{
"status": "400",
"errors": [...]
}
]
}
So I'm wondering if it would be possible for an endpoint to programmatically call the existing, associated actions (e.g., OrdersController#create and ChargesController#create) within a transaction, collect their rendered responses, and render those responses as an array back to the client.
In my mind, the challenges seem to be:
- Resolving paths to controller actions
- Including correct params in each action
Any help is much appreciated--even if that help is "why would you ever want to do that?!? Do this instead!" Thanks!