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