Добавлена валидация цены
This commit is contained in:
@@ -19,6 +19,24 @@ def map_cdek_response(payload: dict[str, Any]) -> list[DeliveryPrice]:
|
||||
]
|
||||
|
||||
|
||||
def map_cdek_response_for_tariff_code(
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
tariff_code: int,
|
||||
) -> DeliveryPrice | None:
|
||||
tariff_codes = payload.get("tariff_codes")
|
||||
if not isinstance(tariff_codes, list):
|
||||
raise CDEKMappingError("CDEK response must include tariff_codes.")
|
||||
|
||||
payload_currency = payload.get("currency")
|
||||
for tariff in tariff_codes:
|
||||
if not isinstance(tariff, dict):
|
||||
raise CDEKMappingError("CDEK tariff entry must be an object.")
|
||||
if _tariff_code_matches(tariff.get("tariff_code"), tariff_code):
|
||||
return _map_tariff(tariff, payload_currency=payload_currency)
|
||||
return None
|
||||
|
||||
|
||||
def _map_tariff(
|
||||
tariff: dict[str, Any],
|
||||
*,
|
||||
@@ -47,6 +65,8 @@ def _map_tariff(
|
||||
currency=str(raw_currency).upper(),
|
||||
delivery_days_min=int(period_min),
|
||||
delivery_days_max=int(period_max),
|
||||
tariff_code=_extract_tariff_code(tariff.get("tariff_code")),
|
||||
bypass_parcel_type_filter=True,
|
||||
)
|
||||
except (ArithmeticError, TypeError, ValueError) as exc:
|
||||
raise CDEKMappingError("CDEK response fields have invalid values.") from exc
|
||||
@@ -62,3 +82,21 @@ def _get_tariffs(payload: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
raise CDEKMappingError("CDEK tariff entry must be an object.")
|
||||
tariffs.append(tariff)
|
||||
return tariffs
|
||||
|
||||
|
||||
def _tariff_code_matches(value: object, expected_tariff_code: int) -> bool:
|
||||
if isinstance(value, bool) or value is None:
|
||||
return False
|
||||
try:
|
||||
return int(value) == expected_tariff_code
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def _extract_tariff_code(value: object) -> int | None:
|
||||
if isinstance(value, bool) or value is None:
|
||||
return None
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user