Skip to main content

class base view for fastapi

Project description

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-imp

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)

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-nest-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

fastapi_nest-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-nest-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi-nest-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for fastapi-nest-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5e3377013a2612afe8130186897d76e5eb846bb95dc74ab085e21e0e672502f4
MD5 4fda930993b01a820d969401838b5a16
BLAKE2b-256 5c26be89e357d7d3b1943f3bcffc08ae60c5e9e8b967d217031ef796d619535e

See more details on using hashes here.

File details

Details for the file fastapi_nest-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_nest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28ea1e936fe240f90e7541787ae1531b0b016d01d38f658fd2e3154f01606eba
MD5 c2d0e8d9bf8710105a02d59ca1fc7202
BLAKE2b-256 edd5af6d331a8a8d229b7a6210eae3c507af65ef693920eb299f113083898ffb

See more details on using hashes here.

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