diff --git a/README.rst b/README.rst index 074c594..92de6c8 100644 --- a/README.rst +++ b/README.rst @@ -8,7 +8,6 @@ How to install $ pip install aiohttp_pydantic - Example: -------- @@ -71,3 +70,77 @@ Example: $ curl -H "Content-Type: application/json" -X post http://127.0.0.1:8080/article --data '{"name": "toto", "nb_page": "3"}' {"name": "toto", "number_of_page": 3} + +API: +---- + +Inject Path Parameters +~~~~~~~~~~~~~~~~~~~~~~ + +To declare a path parameters, you must declare your argument as a `positional-only parameters`_: + + +Example: + +.. code-block:: python3 + + class AccountView(PydanticView): + async def get(self, customer_id: str, account_id: str, /): + ... + + app = web.Application() + app.router.add_get('/customers/{customer_id}/accounts/{account_id}', AccountView) + +Inject Query String Parameters +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To declare a query parameters, you must declare your argument as simple argument: + + +.. code-block:: python3 + + class AccountView(PydanticView): + async def get(self, customer_id: str): + ... + + app = web.Application() + app.router.add_get('/customers', AccountView) + +Inject Request Body +~~~~~~~~~~~~~~~~~~~ + +To declare a body parameters, you must declare your argument as a simple argument annotated with `pydantic Model`_. + + +.. code-block:: python3 + + class Customer(BaseModel): + first_name: str + last_name: str + + class CustomerView(PydanticView): + async def post(self, customer: Customer): + ... + + app = web.Application() + app.router.add_view('/customers', CustomerView) + +Inject HTTP headers +~~~~~~~~~~~~~~~~~~~ + +To declare a HTTP headers parameters, you must declare your argument as a `keyword-only argument`_. + + +.. code-block:: python3 + + class CustomerView(PydanticView): + async def get(self, *, authorization: str, expire_at: datetime): + ... + + app = web.Application() + app.router.add_view('/customers', CustomerView) + + +.. _positional-only parameters: https://www.python.org/dev/peps/pep-0570/ +.. _pydantic Model: https://pydantic-docs.helpmanual.io/usage/models/ +.. _keyword-only argument: https://www.python.org/dev/peps/pep-3102/