-
Hello, I created aiohttp-pydantic to validate request body and header regarding function annotations. I hope it'll be usefull. from aiohttp_pydantic import PydanticView
from aiohttp_pydantic.oas.typing import r200, r201
class Pet(BaseModel):
id: int
name: str
age: int
class Error(BaseModel):
error: str
class PetCollectionView(PydanticView):
async def get(self, age: Optional[int] = None) -> r200[List[Pet]]:
# The age parameter will be get from the query string.
# -> r200 is optional and only used to the generation of the open api documentation.
...
async def post(self, pet: Pet) -> r201[Pet]:
# The pet parameter will be get from the request body and will be cast to a Pet object.
...
class PetItemView(PydanticView):
async def get(self, id: int, /) -> Union[r200[Pet], r404[Error]]:
# The id parameter will be get from the path (note the /)
...
async def put(self, id: int, /, pet: Pet) -> r200[Pet]:
# The id parameter will be get from the path and pet from the request body
...
app = Application()
app.router.add_view("/pets", PetCollectionView)
app.router.add_view("/pets/{id}", PetItemView)] Have a look at demo for a complete example Have a nice day! |
Beta Was this translation helpful? Give feedback.
Answered by
webknjaz
Nov 13, 2020
Replies: 1 comment 1 reply
-
Cool! Feel free to send a PR adding a link to docs! |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
webknjaz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool! Feel free to send a PR adding a link to docs!