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
import falcon
# Import Marshmallow
from marshmallow import Schema as MarhmallowSchema
from marshmallow import fields
# Import Pydantic
from pydantic import BaseModel as PydanticModel
from pydantic import ValidationError
from falcontyping import TypedAPI, TypedResource, TypingMiddleware
# Create a TypedAPI
API = TypedAPI(
falcon.API(middleware=[TypingMiddleware()])
)
class UserPydantic(PydanticModel):
username: str
class UserMarshmallow(MarhmallowSchema):
username = fields.String()
class UserResource(TypedResource):
# A rather contrived example
def on_post(self, request, response,
# Specify query parameter type hint
_id : int,
# Specify body parameter type hint
user: Union[UserPydantic, UserMarshmallow]) -> Union[UserPydantic, UserMarshmallow]:
return UserMarshmallow().load({'username': user.username})
API.add_route('/users/{_id}', UserResource())
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.2.6.tar.gz
(8.8 kB
view hashes)
Built Distribution
Close
Hashes for falcontyping-0.2.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11ddfbdfee18f5853349de2bf0b4a8754978c1f0428eb51dc9a84c2a553c12f3 |
|
MD5 | 9b7b5471baccfebc7963f1a48441c4b4 |
|
BLAKE2b-256 | ac4cfb1136d4a556f928e8de6d4f63efce93607ec7d7ebe016e5cc9ffcc72fa6 |