Skip to content

Commit 44f3710

Browse files
committed
Use kwargs in aiohttp.client.request (#10300)
(cherry picked from commit 3d06cc1)
1 parent 0c32bc8 commit 44f3710

File tree

4 files changed

+96
-102
lines changed

4 files changed

+96
-102
lines changed

CHANGES/10300.feature.rst

+2
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

+76-102
Original file line numberDiff line numberDiff line change
@@ -1471,106 +1471,80 @@ async def __aexit__(
14711471
await self._session.close()
14721472

14731473

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

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

docs/spelling_wordlist.txt

+2
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

+16
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)