Wrapper around NinjaAPI that adds file based routing and endpoint auto-discovery.
Project description
django-ninja-fsbr
File-System Based Routing for django-ninja
Why
- To enforce an opinionated project structure, that does not make me think.
- To enable easily mapping routes/endpoints to files.
- For fun. :)
Usage
The usage is most easily explained using an example. Let's say you want to have the following API:
/products/ GET, POST
/products/<product_id>/ GET
/products/<product_id>/stock/ GET, PUT
You can achieve this using the following directory layout:
├── root.py
└── views
└── products
├── index.py
├── __init__.py
└── product_id
├── index.py
├── __init__.py
└── stock.py
The root
module contains the definition of a router which specifies the base directory, that your automatic routes
will be relative to:
from ninja_fsbr import FilesystemBasedRouter
api = NinjaApi(
...
)
router = FilesystemBasedRouter(views_module="root.views")
router.auto_discover()
api.add_router("api/", router)
To define an "auto" route, just use the @router.auto_route
decorator instead django-ninja's @router.<method>
decorators:
from root import router
@router.auto_route(
methods=["GET"], # optional, if method name starts with get_ (or any other HTTP method name)
... # other django-ninja arguments here
)
def get_stock(request, product_id):
...
Conventions
- Any directory with the ending
_id
will be treated as a path parameter in the generated route. - The
index.py
will resolve to the path itself. - The supported HTTP method can be set by either passing a
methods
argument toauto_route
or by prefixing the view function with the verbs i.e. get_put_stock (to support both GET and PUT requests) - Routes are sorted by name
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
Built Distribution
File details
Details for the file django_ninja_fsbr-0.2.1.tar.gz
.
File metadata
- Download URL: django_ninja_fsbr-0.2.1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77963f3d32f6ceee2e84ae836394faebd474fc5507a8cf39f5935d7b9834f3bd |
|
MD5 | 0ea1a045c04497834b247dac7986374c |
|
BLAKE2b-256 | 8d3b1fef0144fa69d258e2c66ecbe2dba08e4af5fa4f2e965d7a1f2c4417f8c9 |
File details
Details for the file django_ninja_fsbr-0.2.1-py2.py3-none-any.whl
.
File metadata
- Download URL: django_ninja_fsbr-0.2.1-py2.py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96e1aafaae23c54a4e76462787d24d7be50bc054e6e7b2d8322f8463c949d8df |
|
MD5 | b02a77e49f84124b8f2e9aaa9b13fd02 |
|
BLAKE2b-256 | adbba927b9fd5fad0cefb90fcfd5689e951402ac6a5226798812a7cb6d902589 |