Skip to main content

A FastAPI utility to allow Controller Class usage

Project description

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

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_router_controller-0.6.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_router_controller-0.6.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_router_controller-0.6.0.tar.gz.

File metadata

File hashes

Hashes for fastapi_router_controller-0.6.0.tar.gz
Algorithm Hash digest
SHA256 5e38535567bff117ffd380d13b3bd229721ecd2f07a8442d9c13ad541146b133
MD5 a9b879d104e9ed08bd1001c77c77a7b1
BLAKE2b-256 336cca3306f8e942c90e7872915e517874297dbf20c111f1f4c6b3c62ffa6d3a

See more details on using hashes here.

File details

Details for the file fastapi_router_controller-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_router_controller-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d727ca3aa4ea6b8631c3a3f1207afa27df1062b18dcbc542192b2feefcf4f40
MD5 3195e433e137773e607d018ecc6a7e4e
BLAKE2b-256 17ce313fe8fd082485022cee9797b94551bd3b9b6f7c59d6cad2fc198f4002ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page