From a8422874df55c4a1f528865d5d8583ec5d682a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sviatoslav=20Sydorenko=20=28=D0=A1=D0=B2=D1=8F=D1=82=D0=BE?= =?UTF-8?q?=D1=81=D0=BB=D0=B0=D0=B2=20=D0=A1=D0=B8=D0=B4=D0=BE=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=BA=D0=BE=29?= Date: Thu, 30 May 2024 15:44:42 +0200 Subject: [PATCH] Fix the `aiohttp` examples to clean up resources The previously present snippets presented a misuse of `aiohttp`'s APIs. While technically they work, they don't release the allocated resources like async CMs would. And so we recommend using the latter. --- docs/source/index.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/source/index.md b/docs/source/index.md index e27d03e..8713ce3 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -140,8 +140,9 @@ import truststore truststore.inject_into_ssl() -http = aiohttp.ClientSession() -resp = await http.request("GET", "https://example.com") +async with aiohttp.ClientSession() as http_client: + async with http_client.get("https://example.com") as http_response: + ... ``` If you'd like to use the `truststore.SSLContext` directly you can pass @@ -154,8 +155,9 @@ import truststore ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) -http = aiohttp.ClientSession(ssl=ctx) -resp = await http.request("GET", "https://example.com") +async with aiohttp.ClientSession(ssl=ctx) as http_client: + async with http_client.get("https://example.com") as http_response: + ... ``` ### Using truststore with Requests