Skip to main content

FastAPI plugin for Spakky framework

Project description

Spakky FastAPI

FastAPI integration plugin for Spakky Framework.

Installation

pip install spakky-fastapi

Or install via Spakky extras:

pip install spakky[fastapi]

Features

  • Automatic route registration: Routes are registered from @ApiController classes
  • All HTTP methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, WebSocket
  • OpenAPI integration: Tags and documentation automatically configured
  • Error handling middleware: Built-in exception handling with debug mode
  • Context management: Request-scoped dependency injection support

Usage

Basic Controller

from spakky.plugins.fastapi.stereotypes.api_controller import ApiController
from spakky.plugins.fastapi.routes import get, post

@ApiController("/users", tags=["users"])
class UserController:
    def __init__(self, user_service: UserService) -> None:
        self.user_service = user_service

    @get("/{user_id}")
    async def get_user(self, user_id: int) -> User:
        return await self.user_service.get(user_id)

    @post("")
    async def create_user(self, request: CreateUserRequest) -> User:
        return await self.user_service.create(request)

Available Route Decorators

from spakky.plugins.fastapi.routes import (
    get,
    post,
    put,
    delete,
    patch,
    head,
    options,
    websocket,
)

@ApiController("/api")
class MyController:
    @get("/items")
    async def list_items(self) -> list[Item]:
        ...

    @post("/items")
    async def create_item(self, item: CreateItemRequest) -> Item:
        ...

    @put("/items/{item_id}")
    async def update_item(self, item_id: int, item: UpdateItemRequest) -> Item:
        ...

    @delete("/items/{item_id}")
    async def delete_item(self, item_id: int) -> None:
        ...

    @websocket("/ws")
    async def websocket_endpoint(self, websocket: WebSocket) -> None:
        await websocket.accept()
        while True:
            data = await websocket.receive_text()
            await websocket.send_text(f"Echo: {data}")

Accessing FastAPI Instance

from fastapi import FastAPI
from spakky.core.application.application import SpakkyApplication

# After application.start()
fast_api = application.container.get(FastAPI)

Testing with TestClient

from fastapi.testclient import TestClient

def test_get_user(application: SpakkyApplication) -> None:
    fast_api = application.container.get(FastAPI)
    client = TestClient(fast_api)
    response = client.get("/users/1")
    assert response.status_code == 200

Components

Component Description
ApiController Stereotype for REST API controllers with prefix and tags
get, post, put, etc. Route decorators for HTTP methods
websocket WebSocket endpoint decorator
ErrorHandlingMiddleware Built-in exception handling middleware
RegisterRoutesPostProcessor Automatic route registration post-processor

Configuration

The plugin automatically registers a FastAPI instance as a Pod. You can customize it by registering your own FastAPI instance before loading the plugin:

from fastapi import FastAPI

@Pod()
def custom_fastapi() -> FastAPI:
    return FastAPI(
        title="My API",
        description="Custom API configuration",
        version="1.0.0",
    )

License

MIT

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

spakky_fastapi-6.1.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

spakky_fastapi-6.1.2-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file spakky_fastapi-6.1.2.tar.gz.

File metadata

  • Download URL: spakky_fastapi-6.1.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spakky_fastapi-6.1.2.tar.gz
Algorithm Hash digest
SHA256 25be0194335eb38b95bbe02cc8869fa80b803eaf6e3a78be408a7341e6ea87ff
MD5 3a0612d10aa050e21de57cd9799202ed
BLAKE2b-256 5dda06ea38a90db6485c5a366c10cd4c9a7b5deea84f61c929dea459d1e757ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_fastapi-6.1.2.tar.gz:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spakky_fastapi-6.1.2-py3-none-any.whl.

File metadata

  • Download URL: spakky_fastapi-6.1.2-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spakky_fastapi-6.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f866e79717edf6a21c64620a66718b5748b8c26611b9ae083b6d806de4117acb
MD5 af1d96680d054431e5123229c2c44528
BLAKE2b-256 3789b41185501df3ca6ec506a0def42b4b5fa7e55edbe0b0b57bc5f7a5b2e0b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_fastapi-6.1.2-py3-none-any.whl:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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