Skip to content

Commit 3e3b71f

Browse files
committed
Added info about RequestKey & ResponseKey to docs
1 parent 15e86c7 commit 3e3b71f

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

docs/faq.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ support the :class:`dict` interface.
8787

8888
Therefore, data may be stored inside a request object. ::
8989

90-
async def handler(request):
91-
request['unique_key'] = data
90+
request_id_key = web.RequestKey("request_id_key", str)
91+
92+
@web.middleware
93+
async def request_id_middleware(request, handler):
94+
request[request_id_key] = "some_request_id"
95+
return await handler(request)
96+
97+
async def handler(request):
98+
request_id = request[request_id_key]
9299

93100
See https://github.com/aio-libs/aiohttp_session code for an example.
94101
The ``aiohttp_session.get_session(request)`` method uses ``SESSION_KEY``

docs/web_advanced.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,13 @@ Request's storage
446446
^^^^^^^^^^^^^^^^^
447447

448448
Variables that are only needed for the lifetime of a :class:`Request`, can be
449-
stored in a :class:`Request`::
449+
stored in a :class:`Request`. Similarly to :class:`Application`, :class:`RequestKey`
450+
instances or strings can be used as keys::
451+
452+
my_private_key = web.RequestKey("my_private_key", str)
450453

451454
async def handler(request):
452-
request['my_private_key'] = "data"
455+
request[my_private_key] = "data"
453456
...
454457

455458
This is mostly useful for :ref:`aiohttp-web-middlewares` and
@@ -464,9 +467,11 @@ also support :class:`collections.abc.MutableMapping` interface. This is useful
464467
when you want to share data with signals and middlewares once all the work in
465468
the handler is done::
466469

470+
my_metric_key = web.ResponseKey("my_metric_key", int)
471+
467472
async def handler(request):
468473
[ do all the work ]
469-
response['my_metric'] = 123
474+
response[my_metric_key] = 123
470475
return response
471476

472477

@@ -722,18 +727,20 @@ In contrast, when accessing the stream directly (not recommended in middleware):
722727

723728
When working with raw stream data that needs to be shared between middleware and handlers::
724729

730+
raw_body_key = web.RequestKey("raw_body_key", bytes)
731+
725732
async def stream_parsing_middleware(
726733
request: web.Request,
727734
handler: Callable[[web.Request], Awaitable[web.StreamResponse]]
728735
) -> web.StreamResponse:
729736
# Read stream once and store the data
730737
raw_data = await request.content.read()
731-
request['raw_body'] = raw_data
738+
request[raw_body_key] = raw_data
732739
return await handler(request)
733740

734741
async def handler(request: web.Request) -> web.Response:
735742
# Access the stored data instead of reading the stream again
736-
raw_data = request.get('raw_body', b'')
743+
raw_data = request.get(raw_body_key, b'')
737744
return web.Response(body=raw_data)
738745

739746
Example

docs/web_reference.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ and :ref:`aiohttp-web-signals` handlers.
534534
request copy with changed *path*, *method* etc.
535535

536536

537+
.. class:: RequestKey(name, t)
538+
539+
This class should be used for the keys in :class:`Request` and
540+
:class:`BaseRequest`. They provide a type-safe alternative to
541+
`str` keys when checking your code with a type checker (e.g. mypy).
542+
They also avoid name clashes with keys from different libraries etc.
543+
544+
:param name: A name to help with debugging. This should be the same as
545+
the variable name (much like how :class:`typing.TypeVar`
546+
is used).
547+
548+
:param t: The type that should be used for the value in the dict (e.g.
549+
`str`, `Iterator[int]` etc.)
550+
551+
537552

538553

539554
.. _aiohttp-web-response:
@@ -1357,6 +1372,23 @@ content type and *data* encoded by ``dumps`` parameter
13571372
(:func:`json.dumps` by default).
13581373

13591374

1375+
.. class:: ResponseKey(name, t)
1376+
1377+
This class should be used for the keys in :class:`Response`,
1378+
:class:`FileResponse` and :class:`StreamResponse`. They provide
1379+
a type-safe alternative to `str` keys when checking your code
1380+
with a type checker (e.g. mypy). They also avoid name clasheswith keys from different libraries etc.
1381+
1382+
:param name: A name to help with debugging. This should be the same as
1383+
the variable name (much like how :class:`typing.TypeVar`
1384+
is used).
1385+
1386+
:param t: The type that should be used for the value in the dict (e.g.
1387+
`str`, `Iterator[int]` etc.)
1388+
1389+
1390+
1391+
13601392
.. _aiohttp-web-app-and-router:
13611393

13621394
Application and Router
@@ -1631,6 +1663,7 @@ Application and Router
16311663
:param t: The type that should be used for the value in the dict (e.g.
16321664
`str`, `Iterator[int]` etc.)
16331665

1666+
16341667
.. class:: Server
16351668

16361669
A protocol factory compatible with

0 commit comments

Comments
 (0)