017 fix returning all tariffs
This commit is contained in:
@@ -10,9 +10,20 @@ class CDEKMappingError(ValueError):
|
||||
"""Raised when CDEK response cannot be mapped."""
|
||||
|
||||
|
||||
def map_cdek_response(payload: dict[str, Any]) -> DeliveryPrice:
|
||||
tariff = _get_first_tariff(payload)
|
||||
def map_cdek_response(payload: dict[str, Any]) -> list[DeliveryPrice]:
|
||||
tariffs = _get_tariffs(payload)
|
||||
payload_currency = payload.get("currency")
|
||||
return [
|
||||
_map_tariff(tariff, payload_currency=payload_currency)
|
||||
for tariff in tariffs
|
||||
]
|
||||
|
||||
|
||||
def _map_tariff(
|
||||
tariff: dict[str, Any],
|
||||
*,
|
||||
payload_currency: object,
|
||||
) -> DeliveryPrice:
|
||||
service_name = tariff.get("tariff_name") or tariff.get("tariff_code")
|
||||
if service_name is None:
|
||||
raise CDEKMappingError("CDEK tariff_name is missing.")
|
||||
@@ -26,7 +37,7 @@ def map_cdek_response(payload: dict[str, Any]) -> DeliveryPrice:
|
||||
raise CDEKMappingError("CDEK period_min is missing.")
|
||||
period_max = tariff.get("period_max", period_min)
|
||||
|
||||
raw_currency = tariff.get("currency") or payload.get("currency") or "RUB"
|
||||
raw_currency = tariff.get("currency") or payload_currency or "RUB"
|
||||
|
||||
try:
|
||||
return DeliveryPrice(
|
||||
@@ -41,11 +52,13 @@ def map_cdek_response(payload: dict[str, Any]) -> DeliveryPrice:
|
||||
raise CDEKMappingError("CDEK response fields have invalid values.") from exc
|
||||
|
||||
|
||||
def _get_first_tariff(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
def _get_tariffs(payload: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
tariff_codes = payload.get("tariff_codes")
|
||||
if not isinstance(tariff_codes, list) or not tariff_codes:
|
||||
raise CDEKMappingError("CDEK response must include non-empty tariff_codes.")
|
||||
tariff = tariff_codes[0]
|
||||
if not isinstance(tariff, dict):
|
||||
raise CDEKMappingError("CDEK tariff entry must be an object.")
|
||||
return tariff
|
||||
tariffs: list[dict[str, Any]] = []
|
||||
for tariff in tariff_codes:
|
||||
if not isinstance(tariff, dict):
|
||||
raise CDEKMappingError("CDEK tariff entry must be an object.")
|
||||
tariffs.append(tariff)
|
||||
return tariffs
|
||||
|
||||
Reference in New Issue
Block a user