Skip to main content

FastAPI utility to implement class-based routing with controllers and dependency injection.

Project description

Welcome to FastAPI Control 👋

License: MIT Last commit GitHub commit activity Github Stars Github Forks Github Watchers GitHub contributors

FastAPI utility to implement class-based routing with controllers and dependency injection.

Install

pip install git+https://github.com/leynier/fastapi-control@main

Usage

from fastapi import FastAPI

from fastapi_control import add_controllers, controller, get, inject


# Optionally declares an abstraction
class GreeterAbstraction:
    def greet(self):
        raise NotImplementedError()


# Implement the abstraction and make it available to the injection system
# using the @inject decorator
@inject(alias=GreeterAbstraction)
class GretterImplementation:
    def greet(self):
        return "Hello, world!"


# It is also possible to implement without abstraction and make it available
# to the injection system directly
@inject()
class SpanishGretterImplementation:
    def greet(self):
        return "Hola, mundo!"


@inject()
class NestedGretterImplementation:
    # When the @inject decorator is used, the arguments of the __init__
    # method are automatically injected (if the @inject decorator was used
    # in the argument type declarations)
    def __init__(self, spanish_gretter: SpanishGretterImplementation) -> None:
        self.gretter = spanish_gretter

    def greet(self):
        return self.gretter.greet()


# With the @controller decorator, we can declare class-based routing (also
# called controller) and it has the same parameters as FastAPI's APIRouter
@controller(prefix="/home")
class HomeController:
    # When the @controller decorator is used, the arguments of the __init__
    # method are automatically injected (if the @inject decorator was used
    # in the argument type declarations)
    def __init__(
        self,
        gretter: GreeterAbstraction,
        spanish_gretter: SpanishGretterImplementation,
        nested_gretter: NestedGretterImplementation,
    ) -> None:
        self.gretter = gretter
        self.spanish_gretter = spanish_gretter
        self.nested_gretter = nested_gretter

    # The @get decorator declares the method as a GET endpoint (there are
    # also @post, @put, @delete, @patch decorators) and the behavior is the
    # same as the corresponding FastAPI decorators.
    @get(path="/greet")
    def get_greet(self):
        return self.gretter.greet()

    @get(path="/spanish_greet")
    def get_spanish_greet(self):
        return self.spanish_gretter.greet()

    @get(path="/nested_greet")
    def get_nested_greet(self):
        return self.nested_gretter.greet()


api = FastAPI()
# Finally, it is necessary to add the controllers to the FastAPI instance
add_controllers(api)

Inspirations

This project is based on and inspired by the NEXTX and FastApi-RESTful projects.

The difference with FastApi-RESTful is that FastAPI Control implements an automatic dependency injection system independent of FastAPI.

The difference with NEXTX is that FastAPI Control only aims to solve the problem of class-based routes and automatic dependency injection, and uses the kink library for dependency injection which is still under maintenance while NEXTX uses python-inject which has not been maintained since 2020.

Many thanks to the creators and maintainers of those projects for providing inspiration and guidance for this one.

Authors

👨🏻‍💻 Leynier Gutiérrez González

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

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

Uploaded Source

Built Distribution

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

fastapi_control-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi-control-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Windows/10

File hashes

Hashes for fastapi-control-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c8c0210d98467e759c14ce614779ada41a03104b8de18dd0c38cb2cd9d461f6
MD5 56fcbfa2b5d5a79ce8224d3914e01d93
BLAKE2b-256 b94cf5240917880ba5da762fd610aa0c993e403ee97c24da873b40037ba3671a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_control-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Windows/10

File hashes

Hashes for fastapi_control-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2519006aa490f2c60b301f28fc0ebab33ddf73b8cfaee9c2611b103e694790c9
MD5 3b9dd7d8598206877ea4f72cc67bd622
BLAKE2b-256 7f95f29b66aee5b94554194b1b09296bd2b7eb9fa92cc2da2455f12511fa6cf6

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