def create_app_imperative():
    class SampleController:
        def __init__(self, x=Depends(get_x)):
            self.x = x

        def root(
            self,
            id: str = Query(
                ..., title="itemId", description="The id of the sample object"
            ),
        ):
            id += self.x.create()
            return SampleObject(id=id)

        def hello(self, 
            f: Filter, y=Depends(get_y)
        ):
            _id = f.foo
            _id += y
            _id += self.x.create()
            return SampleObject(id=_id)

    router = APIRouter()
    controller = Controller(router, openapi_tag={"name": "sample_controller"})

    controller.add_resource(SampleController)

    controller.route.add_api_route(
        "/",
        SampleController.root,
        tags=["sample_controller"],
        summary="return a sample object",
        response_model=SampleObject,
        methods=["GET"],
    )

    controller.route.add_api_route(
        "/hello", 
        SampleController.hello, 
        response_model=SampleObject, 
        methods=["POST"]
    )

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

    app.include_router(SampleController.router())
    return app
def create_app():
    app = FastAPI(
        title="A application using fastapi_router_controller",
        version="0.1.0",
        openapi_tags=ControllersTags,
    )

    app.include_router(Controller.router())
    return app
def create_app_declerative():
    router = APIRouter()
    controller = Controller(router, openapi_tag={"name": "sample_controller"})

    # With the 'resource' decorator define the controller Class linked to the Controller router arguments
    @controller.resource()
    class SampleController:
        def __init__(self, x=Depends(get_x)):
            self.x = x

        @controller.route.get(
            "/",
            tags=["sample_controller"],
            summary="return a sample object",
            response_model=SampleObject,
        )
        def root(
            self,
            id: str = Query(
                ..., title="itemId", description="The id of the sample object"
            ),
        ):
            id += self.x.create()
            return SampleObject(id=id)

        @controller.route.post(
            "/hello", response_model=SampleObject,
        )
        def hello(self, f: Filter, y=Depends(get_y)):
            _id = f.foo
            _id += y
            _id += self.x.create()
            return SampleObject(id=_id)

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

    app.include_router(SampleController.router())
    return app
from fastapi_router_controller import Controller, ControllersTags

from utils.config import Config
from utils.middleware import LogIncomingRequest, exception_handler, validation_exception_handler

#########################################
#### Configure the main application #####
#########################################
app = FastAPI(
    title='{}'.format(Config.read('app', 'name')),
    description='This is a very fancy project, with auto docs for the API and everything',
    version='0.0.1',
    docs_url=Config.read('app', 'api-docs.path'),
    openapi_tags=ControllersTags)

# configuring handler for validation error in order to format the response
app.exception_handler(RequestValidationError)(validation_exception_handler)

# configuring handler for generic error in order to format the response
app.exception_handler(Exception)(exception_handler)

# add middleware to process the request before it is taken by the router func
app.add_middleware(LogIncomingRequest)

#########################################
#### Configure all the implemented  #####
####  controllers in the main app   #####
#########################################
for router in Controller.routers():
    app.include_router(router)
import unittest

from pydantic import BaseModel
from fastapi_router_controller import Controller, ControllersTags
from fastapi import APIRouter, Depends, FastAPI, Query
from fastapi.testclient import TestClient

parent_router = APIRouter()
child_router = APIRouter()

parent_controller = Controller(parent_router, openapi_tag={"name": "parent"})
child_controller = Controller(child_router, openapi_tag={"name": "child"})


class Object(BaseModel):
    id: str


def get_x():
    class Foo:
        def create(self):
            return "XXX"

    return Foo()


class Filter(BaseModel):
    foo: str


@parent_controller.resource()
import unittest

from pydantic import BaseModel
from fastapi_router_controller import Controller, ControllersTags
from fastapi import APIRouter, Depends, FastAPI, Query
from fastapi.testclient import TestClient

router = APIRouter()
controller = Controller(router, openapi_tag={'name': 'sample_controller'})


class SampleObject(BaseModel):
    id: str


def get_x():
    class Foo:
        def create(self):
            return 'XXX'

    return Foo()


def get_y():
    try:
        yield 'get_y_dep'
    finally:
        print('get_y done')


class Filter(BaseModel):
Beispiel #7
0
from fastapi import APIRouter, status
from fastapi_router_controller import Controller
from fastapi.responses import JSONResponse

router = APIRouter()

controller = Controller(router)

# This is a Sample Controller that can be exteded by another to inherit its routes
@controller.resource()
class SampleParentController():
    @controller.route.get(
        '/parent_api', 
        summary='A sample API from the extended class, it will inherit the basepat of the child router')
    def sample_parent_api(_):
        return JSONResponse(status_code=status.HTTP_200_OK, content={ 'message': 'I\'m the parent'})