from httpx import HTTPStatusError from delivery_metadata.api.app import app from delivery_metadata.constants import PricingFamily from delivery_metadata.exceptions import PricingDataNotFound from delivery_metadata.models.schemas import ( CustomPricing, ReleasePricing, TerritoryCodeA2, TrackReleasePricing, ) async def get_product_pricing(product_id: int, store_id: int) -> set[ReleasePricing]: response = await app.state.ows_connector.get( service_name="ows-pricing", path=f"/pricing_family/{PricingFamily.MUSIC_RELEASE.value}/product/{product_id}/store/{store_id}/pricing", ) data = {} try: response.raise_for_status() data = response.json() except HTTPStatusError as e: if e.response.status_code != 404: raise if not data or not data.get("items"): raise PricingDataNotFound( f"No product pricing data found for product ID: {product_id} for store: {store_id}." ) # ows-pricing returns one territory per item even though it is plural territories :facepalm: territory_pricing: set[ReleasePricing] = set() for row in data["items"]: if row.get("custom_price") and not row.get("custom_currency_code"): raise PricingDataNotFound( f"Custom price provided without a custom currency code for product {product_id}." ) for country_code in row["territories"]: territory_pricing.add( ReleasePricing( price_code=row["price_code"], country_code=TerritoryCodeA2(country_code=country_code), custom_pricing=CustomPricing( custom_currency_code=row["custom_currency_code"], custom_price=row["custom_price"], ) if row.get("custom_price") and row.get("custom_currency_code") else None, start_date=row.get("start_date"), end_date=row.get("end_date"), ) ) return territory_pricing async def get_track_pricing( product_id: int, store_id: int, is_video: bool = False ) -> set[TrackReleasePricing]: pricing_family = ( PricingFamily.MUSIC_VIDEO.value if is_video else PricingFamily.MUSIC_TRACK.value ) response = await app.state.ows_connector.get( service_name="ows-pricing", path=f"/pricing_family/{pricing_family}/product/{product_id}/store/{store_id}/pricing", ) data = {} try: response.raise_for_status() data = response.json() except HTTPStatusError as e: if e.response.status_code != 404: raise if not data or not data.get("items"): raise PricingDataNotFound( f"No track pricing data found for product ID: {product_id} for store: {store_id}." ) territory_pricing: set[TrackReleasePricing] = set() for row in data["items"]: if row.get("custom_price") and not row.get("custom_currency_code"): raise PricingDataNotFound( f"Custom price provided without a custom currency code for product {product_id}." ) for country_code in row["territories"]: territory_pricing.add( TrackReleasePricing( price_code=row["price_code"], country_code=TerritoryCodeA2(country_code=country_code), track_ids=row["track_ids"], custom_pricing=CustomPricing( custom_currency_code=row["custom_currency_code"], custom_price=row["custom_price"], ) if row.get("custom_price") and row.get("custom_currency_code") else None, start_date=row.get("start_date"), end_date=row.get("end_date"), ) ) return territory_pricing