Revert "bypass proxies for schedule API"
This reverts commit bfbb8920e2.
This commit is contained in:
parent
bfbb8920e2
commit
0de8c62b73
1 changed files with 8 additions and 23 deletions
|
|
@ -10,7 +10,7 @@ import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.error import HTTPError, URLError
|
from urllib.error import HTTPError, URLError
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
from urllib.request import ProxyHandler, Request, build_opener
|
from urllib.request import Request, urlopen
|
||||||
from zoneinfo import ZoneInfo
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
@ -30,11 +30,9 @@ MOSCOW = ZoneInfo("Europe/Moscow")
|
||||||
_CACHE_TTL = 300.0
|
_CACHE_TTL = 300.0
|
||||||
_REQUEST_TIMEOUT = 10.0
|
_REQUEST_TIMEOUT = 10.0
|
||||||
_FAILURE_RETRY_DELAY = 30.0
|
_FAILURE_RETRY_DELAY = 30.0
|
||||||
_REQUEST_ATTEMPTS = 2
|
|
||||||
_cache: dict[str, tuple[float, list[dict[str, Any]]]] = {}
|
_cache: dict[str, tuple[float, list[dict[str, Any]]]] = {}
|
||||||
_failures: dict[str, tuple[float, str]] = {}
|
_failures: dict[str, tuple[float, str]] = {}
|
||||||
_cache_lock = threading.RLock()
|
_cache_lock = threading.RLock()
|
||||||
_direct_opener = build_opener(ProxyHandler({}))
|
|
||||||
|
|
||||||
|
|
||||||
class ScheduleAPIError(RuntimeError):
|
class ScheduleAPIError(RuntimeError):
|
||||||
|
|
@ -57,32 +55,19 @@ def _load_result(path: str) -> list[dict[str, Any]]:
|
||||||
return cached[1]
|
return cached[1]
|
||||||
raise ScheduleAPIError(failure[1])
|
raise ScheduleAPIError(failure[1])
|
||||||
|
|
||||||
url = _api_url(path)
|
request = Request(_api_url(path), headers={"Accept": "application/json"})
|
||||||
request = Request(url, headers={"Accept": "application/json"})
|
try:
|
||||||
error: Exception | None = None
|
with urlopen(request, timeout=_REQUEST_TIMEOUT) as response:
|
||||||
payload: Any = None
|
payload = json.load(response)
|
||||||
for _ in range(_REQUEST_ATTEMPTS):
|
except (HTTPError, URLError, TimeoutError, OSError, ValueError) as error:
|
||||||
try:
|
message = f"Не удалось получить {path}: {error}"
|
||||||
with _direct_opener.open(request, timeout=_REQUEST_TIMEOUT) as response:
|
|
||||||
payload = json.load(response)
|
|
||||||
error = None
|
|
||||||
break
|
|
||||||
except (HTTPError, URLError, TimeoutError, OSError, ValueError) as attempt_error:
|
|
||||||
error = attempt_error
|
|
||||||
|
|
||||||
if error is not None:
|
|
||||||
message = f"Не удалось получить {url}: {error}"
|
|
||||||
with _cache_lock:
|
with _cache_lock:
|
||||||
_failures[path] = (now + _FAILURE_RETRY_DELAY, message)
|
_failures[path] = (now + _FAILURE_RETRY_DELAY, message)
|
||||||
if cached:
|
if cached:
|
||||||
return cached[1]
|
return cached[1]
|
||||||
raise ScheduleAPIError(message) from error
|
raise ScheduleAPIError(message) from error
|
||||||
|
|
||||||
if (
|
if payload.get("ok") is not True or not isinstance(payload.get("result"), list):
|
||||||
not isinstance(payload, dict)
|
|
||||||
or payload.get("ok") is not True
|
|
||||||
or not isinstance(payload.get("result"), list)
|
|
||||||
):
|
|
||||||
if cached:
|
if cached:
|
||||||
return cached[1]
|
return cached[1]
|
||||||
raise ScheduleAPIError(f"Сервер вернул некорректный ответ для {path}")
|
raise ScheduleAPIError(f"Сервер вернул некорректный ответ для {path}")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue