fix country code
This commit is contained in:
@@ -48,8 +48,8 @@ class CDEKClient:
|
||||
self._http_client = http_client
|
||||
self._auth_client = auth_client
|
||||
normalized_base_url = base_url.rstrip("/")
|
||||
self._city_lookup_url = f"{normalized_base_url}/location/cities"
|
||||
self._tariff_url = f"{base_url.rstrip('/')}/calculator/tarifflist"
|
||||
self._city_lookup_url = f"{normalized_base_url}/location/suggest/cities"
|
||||
self._tariff_url = f"{normalized_base_url}/calculator/tarifflist"
|
||||
self._timeout_seconds = timeout_seconds
|
||||
self._retry_attempts = retry_attempts
|
||||
self._retry_backoff_seconds = retry_backoff_seconds
|
||||
@@ -103,8 +103,14 @@ class CDEKClient:
|
||||
return status_code == 429 or status_code >= 500
|
||||
|
||||
async def _build_payload(self, request: DeliveryRequest) -> dict[str, Any]:
|
||||
from_city_code = await self._resolve_city_code(request.from_city)
|
||||
to_city_code = await self._resolve_city_code(request.to_city)
|
||||
from_city_code = await self._resolve_city_code(
|
||||
request.from_city,
|
||||
country_code=request.country_code,
|
||||
)
|
||||
to_city_code = await self._resolve_city_code(
|
||||
request.to_city,
|
||||
country_code=request.country_code,
|
||||
)
|
||||
return {
|
||||
"from_location": {"code": from_city_code},
|
||||
"to_location": {"code": to_city_code},
|
||||
@@ -118,21 +124,29 @@ class CDEKClient:
|
||||
],
|
||||
}
|
||||
|
||||
async def _resolve_city_code(self, city: str) -> int:
|
||||
async def _resolve_city_code(self, city: str, *, country_code: str | None) -> int:
|
||||
normalized_city = city.strip().casefold()
|
||||
cached_code = self._city_code_cache.get(normalized_city)
|
||||
normalized_country_code = (
|
||||
country_code.strip().upper() if country_code is not None else ""
|
||||
)
|
||||
cache_key = f"{normalized_country_code}:{normalized_city}"
|
||||
cached_code = self._city_code_cache.get(cache_key)
|
||||
if cached_code is not None:
|
||||
return cached_code
|
||||
|
||||
for attempt in range(self._retry_attempts + 1):
|
||||
try:
|
||||
token = await self._auth_client.get_access_token()
|
||||
params = {"name": city}
|
||||
if normalized_country_code:
|
||||
params["country_code"] = normalized_country_code
|
||||
response = await self._http_client.get(
|
||||
self._city_lookup_url,
|
||||
params={"city": city, "country_codes": "RU", "size": 1},
|
||||
params=params,
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=self._timeout_seconds,
|
||||
)
|
||||
log.info(response.json())
|
||||
except (httpx.TimeoutException, httpx.TransportError) as exc:
|
||||
if attempt < self._retry_attempts:
|
||||
await self._sleep(self._retry_delay(attempt))
|
||||
@@ -148,7 +162,6 @@ class CDEKClient:
|
||||
raise CDEKClientError(
|
||||
f"CDEK city lookup failed with status {response.status_code}."
|
||||
)
|
||||
log.info(response.json())
|
||||
try:
|
||||
response.raise_for_status()
|
||||
body = response.json()
|
||||
@@ -158,11 +171,17 @@ class CDEKClient:
|
||||
else:
|
||||
raise CDEKClientError("CDEK city lookup failed unexpectedly.")
|
||||
|
||||
if not isinstance(body, list) or not body:
|
||||
if isinstance(body, dict):
|
||||
first_item = body
|
||||
elif isinstance(body, list) and body:
|
||||
first_item = body[0]
|
||||
elif isinstance(body, list):
|
||||
raise CDEKRequestError(
|
||||
f"CDEK city lookup returned no matches for '{city}'."
|
||||
)
|
||||
first_item = body[0]
|
||||
else:
|
||||
raise CDEKClientError("CDEK city lookup response must be an object or array.")
|
||||
|
||||
if not isinstance(first_item, dict):
|
||||
raise CDEKClientError("CDEK city lookup response entry must be an object.")
|
||||
raw_city_code = first_item.get("code")
|
||||
@@ -173,7 +192,7 @@ class CDEKClient:
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise CDEKClientError("CDEK city lookup response city code is invalid.") from exc
|
||||
|
||||
self._city_code_cache[normalized_city] = city_code
|
||||
self._city_code_cache[cache_key] = city_code
|
||||
return city_code
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user