Skip to main content

Provides integrations of classic libs with Falcon web framework

Project description

Classic Falcon Integration

Предоставляет простую интеграцию веб-фреймворка Falcon, SpecTree, orjson и classic-error-handling.

Установка

pip install classic-falcon-integration

Quickstart

from falcon import App, Request, Response
from spectree import Response as Responses
from pydantic import BaseModel
import waitress

from classic.components import component
from classic.falcon_integration import specification, register_all
from classic import db_tools


class Pet(BaseModel):
    id: int
    name: str
    age: int


class FilterPets(BaseModel):
    name__contains: list[str] | None = None
    age__le: int | None = None
    age__gt: int | None = None
    id: int | None = None


class NewPet(BaseModel):
    name: str
    age: int


@component
class PetsResource:
    db: db_tools.Engine

    @specification(
        query=FilterPets,
        resp=Responses(
            HTTP_200=list[Pet],
        ),
        operation_id='find_pets',
        tags=['pets'],
    )
    def on_get(self, request: Request, response: Response):
        # Валидация по спецификации отключена по умолчанию,
        # потому вызов валидации обязательно добавлять вручную.
        # Валидацию лучше сего проводить через model_validate,
        # так как далее данные все равно будут переданы в виде dict
        FilterPets.model_validate(request.params)
        
        # Здесь приведен пример работы с БД с classic-db-tools,
        # но здесь может быть что угодно
        with self.db:
            response.media = self.db.queries.filter_pets(
                **request.params
            ).returning(
                db_tools.ToCls(Pet, id='id'),
                returns=Pet,
            ).many()

    @specification(
        json=NewPet,
        resp=Responses(
            HTTP_200=Pet,
        ),
        tags=['pets'],
    )
    def on_post(self, request: Request, response: Response):
        NewPet.model_validate(request.media)
        with self.db:
            response.media = self.db.queries.save_pet(
                **request.media,
            )


if __name__ == '__main__':
    app = App()
    app.add_route('/api/pets', PetsResource())
    register_all(app)
    
    waitress.serve(
        app,
        host='127.0.0.1',
        port='8000',
    )

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

classic_falcon_integration-1.0.0.tar.gz (9.1 kB view details)

Uploaded Source

File details

Details for the file classic_falcon_integration-1.0.0.tar.gz.

File metadata

File hashes

Hashes for classic_falcon_integration-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4dd1e8ba78c061cec43b899704dc446e74c1a32e5d2678549ba5d9271523894a
MD5 c545a2a9bae4b6db16de125f491dd7a5
BLAKE2b-256 7359bfa8f28a90ae2a5bea53732a651cf89786d574f0ee935673db3464e9ef78

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