apicco icon indicating copy to clipboard operation
apicco copied to clipboard

Poor man's batch API

Open SokratisVidros opened this issue 4 years ago • 2 comments

How about adding a controller preprocessor that will kick in when the JSON request payload is an array and will iterate over each array item and execute the corresponding handler?

E.g.

[{
  name: 'foo',
  email: '[email protected]'
},
{
  name: 'bar',
  email: '[email protected]
}]

Let's discuss the process and cons.

SokratisVidros avatar May 25 '20 15:05 SokratisVidros

Pros

  • Batch API endpoint for each JSON API powered by Apicco
  • Fast to implement without extra coding
  • Save N HTTP round trips when N calls for N resources are required.

Cons

  • Coarse grain requests with a lot of redundancy
  • Suboptimal batch API resource utilisation since it will be like doing N discrete HTTP calls

SokratisVidros avatar May 25 '20 15:05 SokratisVidros

I like the idea of using the same endpoint for batch operations and reusing the handler by adding a middleware. I believe there are cases where business logic cannot be applied in batches but you want to send all request data in one request so using this approach seems perfect. We could also provide a way to handle batch operations in a special way. We currently support handler, validate as exports of controllers. We could check if there is a batchHandler export to use for such case or default to handler.

klouvas avatar May 25 '20 15:05 klouvas