Skip to content

Controllers API

The Controllers API provides decorators and utilities for building HTTP endpoints and handling requests.

Controller Decorator

Controller

Bases: AscModule

__init__

__init__(standalone: bool = True, name: str | None = None, tags: list[str] = [], prefix: str = '', suffix: str = '', guards: Sequence[type[Guard] | type[ParamGuard]] = [], *, imports: Sequence[type[AscModuleRef | ControllerRef]] = [], providers: Sequence[Provider] = [], exports: Sequence[type[T] | str] = []) -> None

HTTP Method Decorators

Get module-attribute

Get = create_route_decorator('GET')

Post module-attribute

Post = create_route_decorator('POST')

Put module-attribute

Put = create_route_decorator('PUT')

Patch module-attribute

Patch = create_route_decorator('PATCH')

Delete module-attribute

Delete = create_route_decorator('DELETE')

create_route_decorator

create_route_decorator(method: Literal['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])

Factory function to create route decorators for different HTTP methods.

Parameters:

Name Type Description Default
method Literal["GET", "POST", "PUT", "PATCH", "DELETE"]

The HTTP method for the route.

required

Example Usage

from ascender.core import Controller, Get, Post, inject
from ascender.common.fastapi_interop import Body

from .services.user_service import UserService

@Controller(standalone=True)
class UserController:
    """User management endpoints."""

    def __init__(self, user_service: UserService):
        self.user_service = user_service

    @Get("{id}")
    async def get_user(self, id: str):
        """Get a user by ID."""
        return await self.user_service.find_by_id(id)

    @Get("/")
    async def list_users(self, page: int = 1, limit: int = 10):
        """List users with pagination."""
        return await self.user_service.list(page, limit)

    @Post("/")
    async def create_user(self, data: dict = Body(...)):
        """Create a new user."""
        return await self.user_service.create(data)

See Also