013 add price multiplier
This commit is contained in:
@@ -3,11 +3,13 @@
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
from collections.abc import Callable, Iterable, Sequence
|
||||
from collections.abc import Iterable, Sequence
|
||||
from decimal import Decimal
|
||||
from typing import Protocol
|
||||
|
||||
from app.adapters.delivery_providers.base import DeliveryProvider, ProviderRequestError
|
||||
from app.domain.price import (
|
||||
DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
DEFAULT_WEIGHT_ROUND_SCALE,
|
||||
NormalizedDeliveryRequest,
|
||||
filter_and_sort_prices,
|
||||
@@ -31,6 +33,15 @@ class PriceCacheProtocol(Protocol):
|
||||
async def set(self, key: str, value: object, ttl: int | None = None) -> None: ...
|
||||
|
||||
|
||||
class FilterAndSortPricesFn(Protocol):
|
||||
def __call__(
|
||||
self,
|
||||
prices: Iterable[object],
|
||||
*,
|
||||
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
) -> list[object]: ...
|
||||
|
||||
|
||||
class AggregatorService:
|
||||
def __init__(
|
||||
self,
|
||||
@@ -38,13 +49,13 @@ class AggregatorService:
|
||||
cache: PriceCacheProtocol | None = None,
|
||||
*,
|
||||
weight_round_scale: int = DEFAULT_WEIGHT_ROUND_SCALE,
|
||||
filter_and_sort_prices_fn: Callable[[Iterable[object]], list[object]] = (
|
||||
filter_and_sort_prices
|
||||
),
|
||||
provider_price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
filter_and_sort_prices_fn: FilterAndSortPricesFn = filter_and_sort_prices,
|
||||
) -> None:
|
||||
self._providers = tuple(providers)
|
||||
self._cache = cache
|
||||
self._weight_round_scale = weight_round_scale
|
||||
self._provider_price_multiplier = provider_price_multiplier
|
||||
self._filter_and_sort_prices = filter_and_sort_prices_fn
|
||||
|
||||
async def get_all_prices(self, request: DeliveryRequest) -> list[DeliveryPrice]:
|
||||
@@ -86,7 +97,10 @@ class AggregatorService:
|
||||
raise InvalidDeliveryRequestError(
|
||||
"Delivery request is invalid for configured providers."
|
||||
)
|
||||
filtered_and_sorted = self._filter_and_sort_prices(successful_results)
|
||||
filtered_and_sorted = self._filter_and_sort_prices(
|
||||
successful_results,
|
||||
price_multiplier=self._provider_price_multiplier,
|
||||
)
|
||||
return [self._coerce_delivery_price(price) for price in filtered_and_sorted]
|
||||
|
||||
async def _get_provider_price(
|
||||
|
||||
Reference in New Issue
Block a user