Skip to main content

ASGI middleware to unflatten JSON request bodies

Project description

Rugged

An ASGI middleware to unflatten JSON request keys into nested structures.

This behaviour might be familiar if you've used PHP before.

Example use case

The inspiration for this middleware came from a web app with forms that add variable numbers of fields dynamically. This web app uses htmx to submit forms as AJAX requests, and the hx-json-enc extension to send form data as JSON.

For example, with a form like so:

<h1>Your shopping list</h1>
<form method="post" action="/invite" hx-boost="true" hx-ext="json-enc">
    <label>List name <input type="text" name="title"></label>

    <h2>Items</h2>
    <input type="text" name="items[][item]"> x <input type="number" name="items[][qty]" default="1">
    <input type="text" name="items[][item]"> x <input type="number" name="items[][qty]" default="1">
    <input type="text" name="items[][item]"> x <input type="number" name="items[][qty]" default="1">

    <button onclick="addItemFields()">Add item</button>
    <button type="submit">Save</button>
</form>

The request body received by the server could look like this:

{
    "title": "Sofrito time",
    "items[][item]": ["carrots", "celery", "onions"],
    "items[][qty]": [1, 1, 2],
}

Using this middleware, the request body is unflattened into:

{
    "title": "Sofrito time",
    "items": [
        {"item": "carrots", "qty": 1},
        {"item": "celery", "qty": 1},
        {"item": "onions", "qty": 2},
    ],
}

This makes inputs easier to handle in a FastAPI application using Pydantic:

class ShoppingListItem(BaseModel):
    item: str
    qty: int


class ShoppingList(BaseModel):
    title: str
    items: list[ShoppingListItem]


@app.post("/list")
async def invite_users(shopping_list: ShoppingList):
    find_best_prices(shopping_list.items)

A canonical set of supported input names can be found by reading the unit tests for the unflatten() function, as well as the doc-string - docs coming soon!

Usage in starlette and fastapi can be seen in the respective test files, pending documentation.

Contributing & roadmap

  • This middleware is in very early development - things will change. It's being used in a small FastAPI + HTMX microsite.
  • See the roadmap for planned development
  • I am experimenting with Rye to package this project - if you'd like to contribute, the docs are over at rye-up.com

License

This project is released under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rugged-0.2.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

rugged-0.2.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file rugged-0.2.2.tar.gz.

File metadata

  • Download URL: rugged-0.2.2.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for rugged-0.2.2.tar.gz
Algorithm Hash digest
SHA256 37650f473ece8bd39a6e495e699ccea692745c7eb045cf193fa699dcf8f42b67
MD5 ed4564088b935089202487fcca15d62b
BLAKE2b-256 6abcd65e234eb9200f3abf18b95529009e8085bffa6813b1ffc1c0ae3c2a2c8a

See more details on using hashes here.

File details

Details for the file rugged-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: rugged-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for rugged-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5a4bd448b4e97c046fb0768be3ebb7c4c1c31e2eb1f31262133d5bcd37c026fd
MD5 a99b221be134a0ea20a086bfa935d4d7
BLAKE2b-256 0daf95d20c7c87c311ebf0d36fdc3df70f946bc17b21246068b41e1f62062e6a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page