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 Optional, Union
from pydantic import BaseModel as PydanticModel
# Create API
from falcontyping import TypedAPI, TypedResource
API = TypedAPI()
class UserV1(PydanticModel):
username: str
class UserV2(PydanticModel):
username: str
balance: float
class UserResource(TypedResource):
def on_post(self, request, response, user: Union[UserV2, UserV1]) -> Union[UserV2, UserV1]:
if isinstance(user, UserV2):
return UserV2(username=user.username, balance=user.balance)
else:
return UserV1(username=user.username)
class UserDetailsResource(TypedResource):
def on_get(self, request, response, user_id: int) -> Optional[Union[UserV2, UserV1]]:
if user_id == 1:
return UserV2(username='user', balance=0.0)
if user_id == 0:
return UserV1(username='user')
return None
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.1.tar.gz
(9.0 kB
view hashes)
Built Distribution
Close
Hashes for falcontyping-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddcea2f60e29f050472450bfee01567cd048b30b861e654d92b7790dd5bbfea3 |
|
MD5 | 494766aa4d19021b66132badf70d023b |
|
BLAKE2b-256 | ca1a50d8015af3e03ea02d1406b08b881f948bac0ea7a4bf9149827451d3d670 |