forked from platformio/platformio-core
Minor refactoring
This commit is contained in:
@@ -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 ""))
|
||||
|
@@ -19,17 +19,25 @@ import shutil
|
||||
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": (
|
||||
@@ -38,7 +46,7 @@ class OSRPC:
|
||||
"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)
|
||||
@@ -48,11 +56,11 @@ class OSRPC:
|
||||
# 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
|
||||
|
Reference in New Issue
Block a user