"""Interface for the ows-carveouts-python microservice.""" from typing import Any, Dict, List import asyncio from owsclient import AsyncOwsClient import config from src.utils import constants from lambdacommon.common_config import logger from httpx import Timeout client = AsyncOwsClient( environment=config.ENVIRONMENT, service_name=config.APPLICATION_NAME, timeout=Timeout(30.0) ) async def get_carveouts(release_ids: List[int]) -> Dict[int, Any]: """Fetch carveouts for a list of release IDs asynchronously.""" base_url = '/delivery-restrictions/product/{release_id}' semaphore = asyncio.Semaphore(config.MAX_CONCURRENT_REQUESTS) try: async def fetch(release_id): async with semaphore: response = await client.get( constants.OWS_CARVEOUTS_PYTHON, path=base_url.format(release_id=release_id) ) if response.status_code >= 400: raise Exception( f'Failed for release {release_id} with status {response.status_code}: {response.text}' ) return release_id, response.json() tasks = [fetch(release_id) for release_id in release_ids] responses = await asyncio.gather(*tasks) return dict(responses) except Exception as e: logger.exception(f'Error while fetching Carveouts: {e}') raise