FastAPI style decorators for starlette ASGI
Project description
starlette_decoRouter
A FastAPI style router for Starlette.
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
About The Project
FastApi is a great tool for developping API's in a quick and easy way. In their own words:
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
It is build using starlette which is in their own words:
a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.
The good
One of the things that I love about FastAPI is how easy it is to setup different routes. Just use a decorator above the function that corresponds to that route and done. It's easy to see which route corresponds to which function and vice versa.
The less good
In starlette you first declare different functions and then at the end of the file you map the path, allowed HTTP methods and endpoint function together.
This makes it easy to make mistakes as there is no way to immediately know which endoint corresponds to a certain function, unless you check the routes at the end of the file. They might not even be declared in that file at all.
The best of both
decoRouter auto generates your routes for you based on decorators, just like FastAPI. The downside is that you have to import an extra module, but the added bonus of easier to read and maintain code is certainly worth it.
Installation
You can simply install the module using pip.
> pip install decorouter
Usage
To use decoRouter you will need to know how starlette works. decoRouter will not create an app for you, it will only generate the routes.
For more info regarding starlette, please refer to the Documentation
Example
below is a basic example.
There is one endpoint '/
' which only accepts GET
requests, and returns {'hello': 'world'}
.
- example.py:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from decoRouter import Router
router = Router()
@router.get('/')
async def homepage(request):
return JSONResponse({'hello': 'world'})
app = Starlette(routes=router)
Then run the application...
$ uvicorn example:app
Multiple HTTP methods
It's also possible to accept multiple HTTP methods for one endpoint.
Below you can see one endpoint '/
' which accepts both POST
and PUT
requests.
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from decoRouter import Router
router = Router()
@router.PUT()
@router.POST('/')
async def homepage(request):
return JSONResponse({'hello': 'world'})
app = Starlette(routes=router)
As you can see only the last decorator has a path. It is not necessary to define it multiple times as only the last one will be used.
For example:
@router.PUT()
@router.POST('/')
@router.POST('/home')
async def home(request):
pass
Above, the second decorator will do nothing.
This will result in the endpoint '/home
' accepting both PUT
and POST
requests.
The endpoint '/
' will return a 404.
This means that there is ONE endpoint per function. You can't add multiple endpoints to the same function or vice versa.
Multiple endpoints
Unique endpoints are created based on the function name.
The function names will have no influence over the path of the endpoint.
If a function with a duplicate name is created it will NOT override the original one.
This this means.
Duplicate function names will be ignored, only the first one will be used.
@router.get('/')
async def home(request):
pass
@router.get('/home')
async def home(request):
pass
The above will result in only one endpoint: '/
'.
The second occurence of home will be ignored
Extra
starlette does not check for correct HTTP methods, so neither does decoRouter.
Methods are case insensitive. router.get()
is the same as router.GET()
or even router.GeT()
.
And since there are no checks, things like router.ilasdfggb()
are perfectly fine and will not result in an error. Keep this in mind while debugging.
Contact
Thomas - @TEeckhout - LinkedIn - thomas.eeckhout@outlook.be
Project Link: https://github.com/MrPigss/DecoRouter
Acknowledgments
Thank you
PS.
Apparently starlette still supports using decorators but these are not documented anywhere since they are deprecated since 0.13.0, see changelog.
Project details
Release history Release notifications | RSS feed
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 starlette_decoRouter-1.0.0.tar.gz
.
File metadata
- Download URL: starlette_decoRouter-1.0.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7c8738a12ea033cd8aad3e2d31fa1e16acfc977787c90d4685bd79d758bc885 |
|
MD5 | f9ab198a3cbbedc1e1e9f52605cefcbd |
|
BLAKE2b-256 | c65bcac31df9fd2a5a2c09933e0e0b03a595a376a25b5d46fb20b64e0f45db02 |
File details
Details for the file starlette_decoRouter-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: starlette_decoRouter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e2febe81066b60576e419b33e2f59c6219ca57742994aa00c20450cdf516f57 |
|
MD5 | 8bbeb2400f655f3c6606be2716758561 |
|
BLAKE2b-256 | bca3a4492bb3225345d367fd50aeae076449aacf961c443616f5e8816bda8735 |