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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
falcontyping-0.3.0.tar.gz
(9.0 kB
view hashes)
Built Distribution
Close
Hashes for falcontyping-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00bc1774c22edf4dbef154f77b4a15a33fdeaba3d6d1f2adf4f33e4ed3afcfb8 |
|
MD5 | 500903deabcac65c55257e3534326722 |
|
BLAKE2b-256 | 4f24a9a3f6917cd5d26eb52e0200479985775685118bbbbb3b4e7cd66ddc8b0b |