Добавлен POST /api/v1/delivery/suggest-address

This commit is contained in:
Раис Юсупалиев
2026-03-29 02:05:21 +03:00
parent 17772e5337
commit 4a3737999f
11 changed files with 666 additions and 23 deletions
+50 -2
View File
@@ -2,18 +2,22 @@
from fastapi import APIRouter, Depends, HTTPException, Request, status
from app.adapters.address_suggestions.dadata import DadataAddressSuggestionProvider
from app.adapters.delivery_providers.cdek import CDEKProvider
from app.config import Settings
from app.controllers.http_client import build_controller_http_client
from app.repositories.cache.redis_cache import PriceCache
from app.schemas.order import OrderCreateRequest, OrderCreateResponse
from app.schemas.request import DeliveryCalculationRequest
from app.schemas.response import DeliveryPrice
from app.schemas.request import AddressSuggestRequest, DeliveryCalculationRequest
from app.schemas.response import AddressSuggestion, DeliveryPrice
from app.services.aggregator import (
AddressSuggestionsUnavailableError,
AggregatorService,
AggregatorServiceError,
InvalidAddressSuggestRequestError,
InvalidDeliveryRequestError,
InvalidOrderCreateRequestError,
UnsupportedAddressSuggestionCountryError,
)
router = APIRouter(prefix="/delivery", tags=["delivery"])
@@ -30,12 +34,20 @@ def _build_aggregator_service(settings: Settings) -> AggregatorService:
http_client=http_client,
adapter_config=settings.adapter,
)
dadata_provider = DadataAddressSuggestionProvider.from_config(
http_client=http_client,
config=settings.address_suggestions.dadata,
)
providers = (cdek_provider,)
cache = PriceCache.from_repository_config(settings.repository)
service = AggregatorService(
providers=providers,
cache=cache,
order_adapter=cdek_provider,
address_suggestion_providers=(dadata_provider,),
address_suggestion_country_to_provider=(
settings.address_suggestions.country_to_provider
),
weight_round_scale=settings.business_logic.weight_round_scale,
provider_price_multiplier=settings.business_logic.provider_price_multiplier,
)
@@ -81,6 +93,42 @@ async def get_delivery_price(
) from exc
@router.post(
"/suggest-address",
response_model=list[AddressSuggestion],
)
async def suggest_addresses(
address_request: AddressSuggestRequest,
service: AggregatorService = Depends(get_aggregator_service),
) -> list[AddressSuggestion]:
try:
return await service.suggest_addresses(address_request)
except UnsupportedAddressSuggestionCountryError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"code": "unsupported_address_suggestion_country",
"message": "Address suggestions are not configured for the requested country.",
},
) from exc
except InvalidAddressSuggestRequestError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"code": "invalid_address_suggest_request",
"message": "Address suggestion request contains invalid or unsupported provider data.",
},
) from exc
except AddressSuggestionsUnavailableError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail={
"code": "address_suggestions_unavailable",
"message": "Address suggestions are temporarily unavailable.",
},
) from exc
@router.post(
"/order",
response_model=OrderCreateResponse,