"""Interface for the ows-contract 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_oa_active_contracts(vendor_ids: List[int]) -> Dict[int, Any]: """Fetch OA active contracts for a list of vendor IDs asynchronously.""" base_url = 'vendor/{vendor_id}/mechadmin_for_account' semaphore = asyncio.Semaphore(config.MAX_CONCURRENT_REQUESTS) try: async def fetch(vendor_id): async with semaphore: response = await client.get( constants.OWS_CONTRACTS, path=base_url.format(vendor_id=vendor_id) ) if response.status_code >= 400: raise Exception( f'Failed for vendor {vendor_id} with status {response.status_code}: {response.text}' ) return vendor_id, response.json() tasks = [fetch(vendor_id) for vendor_id in vendor_ids] responses = await asyncio.gather(*tasks) return dict(responses) except Exception as e: logger.exception(f'Error while fetching OA Active Contracts: {e}') raise