From a28a3d31c9c70024c2bf47b6ba404569808d4bf0 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Thu, 29 Jun 2023 21:28:46 +0300 Subject: [PATCH] Keep http session per active PIO Home --- platformio/home/rpc/handlers/os.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/platformio/home/rpc/handlers/os.py b/platformio/home/rpc/handlers/os.py index 57bbdcef..b8dcfc9b 100644 --- a/platformio/home/rpc/handlers/os.py +++ b/platformio/home/rpc/handlers/os.py @@ -37,8 +37,10 @@ class HTTPAsyncSession(HTTPSession): class OSRPC(BaseRPCHandler): - @staticmethod - async def fetch_content(url, data=None, headers=None, cache_valid=None): + _http_session = None + + @classmethod + async def fetch_content(cls, url, data=None, headers=None, cache_valid=None): if not headers: headers = { "User-Agent": ( @@ -57,11 +59,13 @@ class OSRPC(BaseRPCHandler): # check internet before and resolve issue with 60 seconds timeout ensure_internet_on(raise_exception=True) - session = HTTPAsyncSession() + if not cls._http_session: + cls._http_session = HTTPAsyncSession() + if data: - r = await session.post(url, data=data, headers=headers) + r = await cls._http_session.post(url, data=data, headers=headers) else: - r = await session.get(url, headers=headers) + r = await cls._http_session.get(url, headers=headers) r.raise_for_status() result = r.text