Skip to content

Commit

Permalink
Implement BaseRequest.get_extra_info() (#4196)
Browse files Browse the repository at this point in the history
* Add get_extra_info for BaseRequest

`BaseRequest.get_extra_info()` provides a shortcut for accessing extra
information from the underlying protocols' transport

* Add get_extra_info default value test

* Add return type annotation to BaseRequest.get_extra_info()

* Remove async from BaseRequest.get_extra_info()

* Rewrite get_extra_info test, cover all branches

* Add news entry to CHANGES for #4189

* Add Raphael Bialon to CONTRIBUTORS.txt

* Add documentation for BaseRequest.get_extra_info()

* Update web_reference.rst

Add versionadded to get_extra_info() documentation
  • Loading branch information
rbialon authored and asvetlov committed Oct 18, 2019
1 parent 36bb09d commit fbc9cf6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/4189.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement BaseRequest.get_extra_info() to access a protocol transports' extra info.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Pepe Osca
Philipp A.
Pieter van Beek
Rafael Viotti
Raphael Bialon
Raúl Cumplido
Required Field
Robert Lu
Expand Down
12 changes: 12 additions & 0 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,18 @@ async def post(self) -> 'MultiDictProxy[Union[str, bytes, FileField]]':
self._post = MultiDictProxy(out)
return self._post

def get_extra_info(self, name: str, default: Any = None) -> Any:
"""Extra info from protocol transport"""
protocol = self._protocol
if protocol is None:
return default

transport = protocol.transport
if transport is None:
return default

return transport.get_extra_info(name, default)

def __repr__(self) -> str:
ascii_encodable_path = self.path.encode('ascii', 'backslashreplace') \
.decode('ascii')
Expand Down
12 changes: 12 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ and :ref:`aiohttp-web-signals` handlers.

:return: a cloned :class:`Request` instance.

.. method:: get_extra_info(name, default=None)

Reads extra information from the protocol's transport.
If no value associated with ``name`` is found, ``default`` is returned.

:param str name: The key to look up in the transport extra information.

:param default: Default value to be used when no value for ``name`` is
found (default is ``None``).

.. versionadded:: 3.7

.. comethod:: read()

Read request body, returns :class:`bytes` object with body content.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import socket
from collections.abc import MutableMapping
from typing import Any
from unittest import mock

import pytest
Expand Down Expand Up @@ -694,6 +695,31 @@ def test_url_https_with_closed_transport() -> None:
assert str(req.url).startswith('https://')


async def test_get_extra_info() -> None:
valid_key = 'test'
valid_value = 'existent'
default_value = 'default'

def get_extra_info(name: str, default: Any = None):
return {valid_key: valid_value}.get(name, default)
transp = mock.Mock()
transp.get_extra_info.side_effect = get_extra_info
req = make_mocked_request('GET', '/', transport=transp)

req_extra_info = req.get_extra_info(valid_key, default_value)
transp_extra_info = req._protocol.transport.get_extra_info(valid_key,
default_value)
assert req_extra_info == transp_extra_info

req._protocol.transport = None
extra_info = req.get_extra_info(valid_key, default_value)
assert extra_info == default_value

req._protocol = None
extra_info = req.get_extra_info(valid_key, default_value)
assert extra_info == default_value


def test_eq() -> None:
req1 = make_mocked_request('GET', '/path/to?a=1&b=2')
req2 = make_mocked_request('GET', '/path/to?a=1&b=2')
Expand Down

0 comments on commit fbc9cf6

Please sign in to comment.