Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankravets committed Jan 21, 2023
1 parent 6db3eb8 commit 1503eb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
16 changes: 0 additions & 16 deletions platformio/home/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@

import socket

from starlette.concurrency import run_in_threadpool

from platformio import util
from platformio.compat import IS_WINDOWS
from platformio.http import HTTPSession
from platformio.proc import where_is_program


class AsyncSession(HTTPSession):
async def request( # pylint: disable=signature-differs,invalid-overridden-method
self, *args, **kwargs
):
func = super().request
return await run_in_threadpool(func, *args, **kwargs)


@util.memoized(expire="60s")
def requests_session():
return AsyncSession()


@util.memoized(expire="60s")
def get_core_fullpath():
return where_is_program("platformio" + (".exe" if IS_WINDOWS else ""))
Expand Down
22 changes: 15 additions & 7 deletions platformio/home/rpc/handlers/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@
from functools import cmp_to_key

import click
from starlette.concurrency import run_in_threadpool

from platformio import fs
from platformio.cache import ContentCache
from platformio.device.list.util import list_logical_devices
from platformio.home import helpers
from platformio.http import ensure_internet_on
from platformio.http import HTTPSession, ensure_internet_on


class HTTPAsyncSession(HTTPSession):
async def request( # pylint: disable=signature-differs,invalid-overridden-method
self, *args, **kwargs
):
func = super().request
return await run_in_threadpool(func, *args, **kwargs)


class OSRPC:
@staticmethod
async def fetch_content(uri, data=None, headers=None, cache_valid=None):
async def fetch_content(url, data=None, headers=None, cache_valid=None):
if not headers:
headers = {
"User-Agent": (
Expand All @@ -38,7 +46,7 @@ async def fetch_content(uri, data=None, headers=None, cache_valid=None):
"Safari/603.3.8"
)
}
cache_key = ContentCache.key_from_args(uri, data) if cache_valid else None
cache_key = ContentCache.key_from_args(url, data) if cache_valid else None
with ContentCache() as cc:
if cache_key:
result = cc.get(cache_key)
Expand All @@ -48,11 +56,11 @@ async def fetch_content(uri, data=None, headers=None, cache_valid=None):
# check internet before and resolve issue with 60 seconds timeout
ensure_internet_on(raise_exception=True)

session = helpers.requests_session()
session = HTTPAsyncSession()
if data:
r = await session.post(uri, data=data, headers=headers)
r = await session.post(url, data=data, headers=headers)
else:
r = await session.get(uri, headers=headers)
r = await session.get(url, headers=headers)

r.raise_for_status()
result = r.text
Expand Down

0 comments on commit 1503eb5

Please sign in to comment.