murdho/rack-cargo: Batch requests for Rack APIs

Posted . Visible to the public.

rack-cargo Show archive.org snapshot gives your API a new /batch route that lets clients perform multiple API calls with a single request.

The batched calls are resolved internally and are then returned as an array of responses:

// This is batch request payload:
{
    "requests": [
        {
            "name": "order",
            "path": "/orders",
            "method": "POST",
            "body": {
                "address": "Home, 12345"
            }
        },
        {
            "name": "order_item",
            "path": "/orders/{{ order.uuid }}/items", // <-- here
            "method": "POST",
            "body": {
                "title": "A Book"
            }
        },
        {
            "name": "payment",
            "path": "/payments",
            "method": "POST",
            "body": {
                "orders": [
                    "{{ order.uuid }}" // <-- and here
                ]
            }
        }
    ]
}

// This is a possible response:
[
    {
        "name": "order", // <-- "order" part of "order.uuid"
        "status": 201,
        "headers": {},
        "body": {
            "uuid": "bf52fdb5-d1c3-4c66-ba7d-bdf4cd83f265", // <-- "uuid" part of "order.uuid"
            "address": "Home, 12345"
        }
    },
    {
        "name": "order_item",
        "status": 201,
        "headers": {},
        "body": {
            "uuid": "38bc4576-3b7e-40be-a1d6-ca795fe462c8",
            "title": "A Book"
        }
    },
    {
        "name": "payment",
        "status": 201,
        "headers": {},
        "body": {
            "uuid": "c4f9f261-7822-4217-80a2-06cf92934bf9",
            "orders": [
                "bf52fdb5-d1c3-4c66-ba7d-bdf4cd83f265"
            ]
        }
    }
]
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2017-08-01 12:29)