Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aminalaee/fastapi-extended-route
Flask style url_for for FastAPI
https://github.com/aminalaee/fastapi-extended-route
asgi fastapi
Last synced: about 2 months ago
JSON representation
Flask style url_for for FastAPI
- Host: GitHub
- URL: https://github.com/aminalaee/fastapi-extended-route
- Owner: aminalaee
- Created: 2022-07-13T07:30:42.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-07-24T11:30:52.000Z (over 2 years ago)
- Last Synced: 2024-10-12T18:59:32.808Z (3 months ago)
- Topics: asgi, fastapi
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Opinionated FastAPI Extended Route
A small utility for FastAPI/Starlette Route and Request to address the following:
- `Request.url_for` doesn't allow adding `query parameters` at the moment.
- `URL` objects can't have multiple keys like `/?key=value&key=anothervalue`.With the following changes:
- Make `Request.url_for` to match path parameters and use unmatched params for query parameters.
- Provide a custom `Route` which has a new `Request` type.
- Allow `URL` object to add multiple keys in `query params`.
- ...Example:
```python
from fastapi import FastAPI
from fastapi.responses import PlainTextResponsefrom fastapi_extended_route import Request, Route
def index(request: Request) -> PlainTextResponse:
url = request.url_for("index", key=value)
# url == "http://testserver/?key=value"
return PlainTextResponse(url)app = FastAPI(
routes=[Route("/", index, name="index")],
)
```As you can see the only change is to use `Route` from the package and
the new `Request` object will have a customized `url_for` method which
handles both path parameters and query parameters.If/when these options are available in Starlette/FastAPI, this is no longer needed.