Skip to main content

fastapi-router-controller

Build PyPI version fury.io

A FastAPI utility to allow Controller Class usage

Installation:

install the package

pip install fastapi-router-controller

How to use

Here we see a Fastapi CBV (class based view) application with class wide Basic Auth dependencies.

import uvicorn

from pydantic import BaseModel
from fastapi_router_controller import Controller
from fastapi import APIRouter, Depends, FastAPI, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials

router = APIRouter()
controller = Controller(router)
security = HTTPBasic()


def verify_auth(credentials: HTTPBasicCredentials = Depends(security)):
    correct_username = credentials.username == "john"
    correct_password = credentials.password == "silver"
    if not (correct_username and correct_password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect auth",
            headers={"WWW-Authenticate": "Basic"},
        )
    return credentials.username


class Foo(BaseModel):
    bar: str = "wow"


async def amazing_fn():
    return Foo(bar="amazing_variable")


@controller.resource()
class ExampleController:

    # add class wide dependencies e.g. auth
    dependencies = [Depends(verify_auth)]

    # you can define in the Controller init some FastApi Dependency and them are automatically loaded in controller methods
    def __init__(self, x: Foo = Depends(amazing_fn)):
        self.x = x

    @controller.route.get(
        "/some_api", summary="A sample description", response_model=Foo
    )
    def sample_api(self):
        print(self.x.bar)  # -> amazing_variable
        return self.x


# Load the controller to the main FastAPI app

app = FastAPI(
    title="A sample application using fastapi_router_controller", version="0.1.0"
)

app.include_router(ExampleController.router())

uvicorn.run(app, host="0.0.0.0", port=9090)

Screenshot

All you expect from Fastapi

Swagger UI

Also the login dialog

Swagger UI Login

For some Example use-cases visit the example folder

Metadata

Release files for fastapi-router-controller 0.6.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 fastapi-router-controller 0.6.0
File Size Uploaded Provenance Actions
fastapi_router_controller-0.6.0.tar.gz 9.9 kB Without provenance Details for fastapi_router_controller-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-router-controller 0.6.0
File Interpreter ABI Platform Actions
fastapi_router_controller-0.6.0-py3-none-any.whl Python 3 none any Details for fastapi_router_controller-0.6.0-py3-none-any.whl

Total release size: 24.2 kB

Release files / fastapi_router_controller-0.6.0.tar.gz

Download URL fastapi_router_controller-0.6.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5e38535567bff117ffd380d13b3bd229721ecd2f07a8442d9c13ad541146b133
BLAKE2b-256 checksum
How to use checksums
336cca3306f8e942c90e7872915e517874297dbf20c111f1f4c6b3c62ffa6d3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / fastapi_router_controller-0.6.0-py3-none-any.whl

Download URL fastapi_router_controller-0.6.0-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d727ca3aa4ea6b8631c3a3f1207afa27df1062b18dcbc542192b2feefcf4f40
BLAKE2b-256 checksum
How to use checksums
17ce313fe8fd082485022cee9797b94551bd3b9b6f7c59d6cad2fc198f4002ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for fastapi-router-controller 0.6.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.

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