Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Add type hints support to Falcon with Pydantic and Marshmallow integration

Project description

Falcon typing

Use type hints to specify request parameters with Marshmallow and Pydantic support.

Uses typedjson

Example

"""API."""
from typing import Union

from marshmallow import Schema as MarhmallowSchema
from marshmallow import fields
from pydantic import BaseModel as PydanticModel

# Create API
from falcontyping import TypedAPI, TypedResource
API = TypedAPI()


class UserV1(MarhmallowSchema):

    username = fields.String()


class UserV2(PydanticModel):

    username: str


class UserResource(TypedResource):

    def on_post(self, request, response, user: Union[UserV1, UserV2]) -> Union[UserV1, UserV2]:
        if isinstance(user, UserV1):
            return UserV1().load({'username': user.username})

        else:
            return UserV2(username=user.username)


class UserDetailsResource(TypedResource):

    def on_get(self, request, response, user_id: int) -> UserV2:
        return UserV2(username='user')


API.add_route('/users', UserResource())
API.add_route('/users/{user_id}', UserDetailsResource())

How to install

pip install falcontyping

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for falcontyping, version 0.2.8
Filename, size File type Python version Upload date Hashes
Filename, size falcontyping-0.2.8-py3-none-any.whl (14.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size falcontyping-0.2.8.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page