Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.6] Implement BaseRequest.get_extra_info() (#4196) #4231

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -199,6 +199,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 @@ -669,6 +669,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 @@ -364,6 +364,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 @@ -684,6 +685,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