Skip to main content

Issues black LinkedIn


starlette_decoRouter

A FastAPI style router for Starlette.
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Installation
  3. Usage
  4. Contact
  5. Acknowledgments

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. FastAPI Screen Shot It's easy to see which route corresponds to which function and vice versa.

(back to top)

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. starlette Screen Shot

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.

(back to top)

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. decoRouter Screen Shot

(back to top)

Installation

You can simply install the module using pip.

  • > pip install decorouter

(back to top)

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.

(back to top)

Contact

Thomas - @TEeckhout - LinkedIn - thomas.eeckhout@outlook.be
Project Link: https://github.com/MrPigss/DecoRouter

(back to top)

Acknowledgments

Thank you

(back to top)

PS.

Apparently starlette still supports using decorators but these are not documented anywhere since they are deprecated since 0.13.0, see changelog.

Metadata

Release files for starlette-decoRouter 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for starlette-decoRouter 1.0.0
File Size Uploaded Provenance Actions
starlette_decoRouter-1.0.0.tar.gz 5.2 kB Without provenance Details for starlette_decoRouter-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for starlette-decoRouter 1.0.0
File Interpreter ABI Platform Actions
starlette_decoRouter-1.0.0-py3-none-any.whl Python 3 none any Details for starlette_decoRouter-1.0.0-py3-none-any.whl

Total release size: 10.1 kB

Release files / starlette_decoRouter-1.0.0.tar.gz

Download URL starlette_decoRouter-1.0.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a7c8738a12ea033cd8aad3e2d31fa1e16acfc977787c90d4685bd79d758bc885
BLAKE2b-256 checksum
How to use checksums
c65bcac31df9fd2a5a2c09933e0e0b03a595a376a25b5d46fb20b64e0f45db02
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / starlette_decoRouter-1.0.0-py3-none-any.whl

Download URL starlette_decoRouter-1.0.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e2febe81066b60576e419b33e2f59c6219ca57742994aa00c20450cdf516f57
BLAKE2b-256 checksum
How to use checksums
bca3a4492bb3225345d367fd50aeae076449aacf961c443616f5e8816bda8735
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for starlette-decoRouter 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page