Skip to content

Commit 52e4ea6

Browse files
authored
[PR #10300/3d06cc1][3.12] Use kwargs in aiohttp.client.request (#10302)
(cherry picked from commit 3d06cc1)
1 parent d5dd9d0 commit 52e4ea6

File tree

4 files changed

+96
-102
lines changed

4 files changed

+96
-102
lines changed

CHANGES/10300.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :py:func:`~aiohttp.request` to make it accept ``_RequestOptions`` kwargs.
2+
-- by :user:`Cycloctane`.

aiohttp/client.py

Lines changed: 76 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,106 +1469,80 @@ async def __aexit__(
14691469
await self._session.close()
14701470

14711471

1472-
def request(
1473-
method: str,
1474-
url: StrOrURL,
1475-
*,
1476-
params: Query = None,
1477-
data: Any = None,
1478-
json: Any = None,
1479-
headers: Optional[LooseHeaders] = None,
1480-
skip_auto_headers: Optional[Iterable[str]] = None,
1481-
auth: Optional[BasicAuth] = None,
1482-
allow_redirects: bool = True,
1483-
max_redirects: int = 10,
1484-
compress: Optional[str] = None,
1485-
chunked: Optional[bool] = None,
1486-
expect100: bool = False,
1487-
raise_for_status: Optional[bool] = None,
1488-
read_until_eof: bool = True,
1489-
proxy: Optional[StrOrURL] = None,
1490-
proxy_auth: Optional[BasicAuth] = None,
1491-
timeout: Union[ClientTimeout, object] = sentinel,
1492-
cookies: Optional[LooseCookies] = None,
1493-
version: HttpVersion = http.HttpVersion11,
1494-
connector: Optional[BaseConnector] = None,
1495-
read_bufsize: Optional[int] = None,
1496-
loop: Optional[asyncio.AbstractEventLoop] = None,
1497-
max_line_size: int = 8190,
1498-
max_field_size: int = 8190,
1499-
) -> _SessionRequestContextManager:
1500-
"""Constructs and sends a request.
1501-
1502-
Returns response object.
1503-
method - HTTP method
1504-
url - request url
1505-
params - (optional) Dictionary or bytes to be sent in the query
1506-
string of the new request
1507-
data - (optional) Dictionary, bytes, or file-like object to
1508-
send in the body of the request
1509-
json - (optional) Any json compatible python object
1510-
headers - (optional) Dictionary of HTTP Headers to send with
1511-
the request
1512-
cookies - (optional) Dict object to send with the request
1513-
auth - (optional) BasicAuth named tuple represent HTTP Basic Auth
1514-
auth - aiohttp.helpers.BasicAuth
1515-
allow_redirects - (optional) If set to False, do not follow
1516-
redirects
1517-
version - Request HTTP version.
1518-
compress - Set to True if request has to be compressed
1519-
with deflate encoding.
1520-
chunked - Set to chunk size for chunked transfer encoding.
1521-
expect100 - Expect 100-continue response from server.
1522-
connector - BaseConnector sub-class instance to support
1523-
connection pooling.
1524-
read_until_eof - Read response until eof if response
1525-
does not have Content-Length header.
1526-
loop - Optional event loop.
1527-
timeout - Optional ClientTimeout settings structure, 5min
1528-
total timeout by default.
1529-
Usage::
1530-
>>> import aiohttp
1531-
>>> resp = await aiohttp.request('GET', 'http://python.org/')
1532-
>>> resp
1533-
<ClientResponse(python.org/) [200]>
1534-
>>> data = await resp.read()
1535-
"""
1536-
connector_owner = False
1537-
if connector is None:
1538-
connector_owner = True
1539-
connector = TCPConnector(loop=loop, force_close=True)
1540-
1541-
session = ClientSession(
1542-
loop=loop,
1543-
cookies=cookies,
1544-
version=version,
1545-
timeout=timeout,
1546-
connector=connector,
1547-
connector_owner=connector_owner,
1548-
)
1472+
if sys.version_info >= (3, 11) and TYPE_CHECKING:
15491473

1550-
return _SessionRequestContextManager(
1551-
session._request(
1552-
method,
1553-
url,
1554-
params=params,
1555-
data=data,
1556-
json=json,
1557-
headers=headers,
1558-
skip_auto_headers=skip_auto_headers,
1559-
auth=auth,
1560-
allow_redirects=allow_redirects,
1561-
max_redirects=max_redirects,
1562-
compress=compress,
1563-
chunked=chunked,
1564-
expect100=expect100,
1565-
raise_for_status=raise_for_status,
1566-
read_until_eof=read_until_eof,
1567-
proxy=proxy,
1568-
proxy_auth=proxy_auth,
1569-
read_bufsize=read_bufsize,
1570-
max_line_size=max_line_size,
1571-
max_field_size=max_field_size,
1572-
),
1573-
session,
1574-
)
1474+
def request(
1475+
method: str,
1476+
url: StrOrURL,
1477+
*,
1478+
version: HttpVersion = http.HttpVersion11,
1479+
connector: Optional[BaseConnector] = None,
1480+
loop: Optional[asyncio.AbstractEventLoop] = None,
1481+
**kwargs: Unpack[_RequestOptions],
1482+
) -> _SessionRequestContextManager: ...
1483+
1484+
else:
1485+
1486+
def request(
1487+
method: str,
1488+
url: StrOrURL,
1489+
*,
1490+
version: HttpVersion = http.HttpVersion11,
1491+
connector: Optional[BaseConnector] = None,
1492+
loop: Optional[asyncio.AbstractEventLoop] = None,
1493+
**kwargs: Any,
1494+
) -> _SessionRequestContextManager:
1495+
"""Constructs and sends a request.
1496+
1497+
Returns response object.
1498+
method - HTTP method
1499+
url - request url
1500+
params - (optional) Dictionary or bytes to be sent in the query
1501+
string of the new request
1502+
data - (optional) Dictionary, bytes, or file-like object to
1503+
send in the body of the request
1504+
json - (optional) Any json compatible python object
1505+
headers - (optional) Dictionary of HTTP Headers to send with
1506+
the request
1507+
cookies - (optional) Dict object to send with the request
1508+
auth - (optional) BasicAuth named tuple represent HTTP Basic Auth
1509+
auth - aiohttp.helpers.BasicAuth
1510+
allow_redirects - (optional) If set to False, do not follow
1511+
redirects
1512+
version - Request HTTP version.
1513+
compress - Set to True if request has to be compressed
1514+
with deflate encoding.
1515+
chunked - Set to chunk size for chunked transfer encoding.
1516+
expect100 - Expect 100-continue response from server.
1517+
connector - BaseConnector sub-class instance to support
1518+
connection pooling.
1519+
read_until_eof - Read response until eof if response
1520+
does not have Content-Length header.
1521+
loop - Optional event loop.
1522+
timeout - Optional ClientTimeout settings structure, 5min
1523+
total timeout by default.
1524+
Usage::
1525+
>>> import aiohttp
1526+
>>> async with aiohttp.request('GET', 'http://python.org/') as resp:
1527+
... print(resp)
1528+
... data = await resp.read()
1529+
<ClientResponse(https://www.python.org/) [200 OK]>
1530+
"""
1531+
connector_owner = False
1532+
if connector is None:
1533+
connector_owner = True
1534+
connector = TCPConnector(loop=loop, force_close=True)
1535+
1536+
session = ClientSession(
1537+
loop=loop,
1538+
cookies=kwargs.pop("cookies", None),
1539+
version=version,
1540+
timeout=kwargs.pop("timeout", sentinel),
1541+
connector=connector,
1542+
connector_owner=connector_owner,
1543+
)
1544+
1545+
return _SessionRequestContextManager(
1546+
session._request(method, url, **kwargs),
1547+
session,
1548+
)

docs/spelling_wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ app
1313
app’s
1414
apps
1515
arg
16+
args
1617
Arsenic
1718
async
1819
asyncio
@@ -169,6 +170,7 @@ keepaliving
169170
kib
170171
KiB
171172
kwarg
173+
kwargs
172174
latin
173175
lifecycle
174176
linux

tests/test_client_functional.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,6 +3382,22 @@ async def handler(request: web.Request) -> web.Response:
33823382
await server.close()
33833383

33843384

3385+
async def test_aiohttp_request_ssl(
3386+
aiohttp_server: AiohttpServer,
3387+
ssl_ctx: ssl.SSLContext,
3388+
client_ssl_ctx: ssl.SSLContext,
3389+
) -> None:
3390+
async def handler(request: web.Request) -> web.Response:
3391+
return web.Response()
3392+
3393+
app = web.Application()
3394+
app.router.add_get("/", handler)
3395+
server = await aiohttp_server(app, ssl=ssl_ctx)
3396+
3397+
async with aiohttp.request("GET", server.make_url("/"), ssl=client_ssl_ctx) as resp:
3398+
assert resp.status == 200
3399+
3400+
33853401
async def test_yield_from_in_session_request(aiohttp_client: AiohttpClient) -> None:
33863402
# a test for backward compatibility with yield from syntax
33873403
async def handler(request):

0 commit comments

Comments
 (0)