Skip to main content

FastAPI Controller and Route Decorators

This package provides a set of decorators and utilities to simplify the creation of FastAPI controllers and routes. It allows you to define controllers as classes and routes as methods within those classes, with automatic dependency injection of shared dependencies.

Features

  • Controller Class: Define a group of related routes within a single class.
  • Route Decorators: Easily define route handlers with specific HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE).
  • Automatic Dependency Injection: Use Depends to inject dependencies into controller methods.
  • Simple Integration: Integrate with existing FastAPI applications with minimal changes.

Installation

To use these decorators, ensure that you have FastAPI installed in your environment. If not, you can install it using pip:

pip install fastapi-nest

Then, include the provided code in your project or package it accordingly.

Usage

Defining a Controller

To create a new controller, define a class and decorate it with @controller:

from nest import controller

@controller()
class MyController:
    pass

Defining Route Handlers

Use the provided HTTP method decorators to define route handlers within your controller:

from nest import controller, get, post

@controller()
class MyController:
    
    @get("/items")
    def read_items(self):
        return {"message": "Reading items"}

    @post("/items")
    def create_item(self, item: dict):
        return {"message": "Creating item", "item": item}

Dependency Injection

Use Depends to inject dependencies into your route handlers:

from fastapi import Depends
from nest import controller, get

def get_current_user():
    # Logic to get the current user
    return {"user_id": 1}

@controller()
class MyController:
    
    # attribute inject
    current_user: dict = Depends(get_current_user)
    
    @get("/users/me")
    def read_current_user(self, current_user: dict = Depends(get_current_user)):
        assert self.current_user["user_id"] == current_user["user_id"]
        return {"message": "Reading current user", "user": current_user}

Registering Controllers with FastAPI

To add your controller's routes to a FastAPI app, use the as_api_router utility:

from fastapi import FastAPI
from nest import as_api_router
from my_controller import MyController

app = FastAPI()

my_controller_router = as_api_router(MyController())
app.include_router(my_controller_router)

Release files for fastapi-nest 0.1.1

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-nest 0.1.1
File Size Uploaded
fastapi-nest-0.1.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-nest 0.1.1
File Interpreter ABI Platform
fastapi_nest-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / fastapi-nest-0.1.1.tar.gz

Download URL fastapi-nest-0.1.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
93799ae6bb53de085a8c2651d8b69bffd681d576d2cc35c07b436a3e86b156c5
BLAKE2b-256 checksum
How to use checksums
405b48696bf32d86e691e99570411782828b585b0394076b36c26d13f1811d28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / fastapi_nest-0.1.1-py3-none-any.whl

Download URL fastapi_nest-0.1.1-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29f2712fd1befe9374386c3d95ee5945eb4b2e7d2c5c80a7d669803ad7dab631
BLAKE2b-256 checksum
How to use checksums
d1b9fb1aa68c61b946d55bf574a5f38d5ea2baee4b518724f993b96d58abdc15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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