Skip to main content

A small utility for FastAPI Router.

Project description

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:

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

from 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.

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

fastapi_extended_route-0.1.0.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

fastapi_extended_route-0.1.0-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

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