"""Connector to the ows-participant microservice.""" import logging from dataclasses import dataclass from typing import List, Optional from uuid import UUID import httpx from owsclient import AsyncOwsClient from pydantic import BaseModel logger = logging.getLogger(__name__) DEFAULT_OWS_PARTICIPANT_REQUEST_TIMEOUT = httpx.Timeout(10) class LookupParticipantsRequest(BaseModel): """Lookup label participant tenant hierarchy request schema.""" uuids: List[UUID] class LookupParticipant(BaseModel): """Label participant tenant hierarchy schema. { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": null, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", } """ uuid: UUID vendor_uuid: Optional[UUID] = None subaccount_uuid: Optional[UUID] = None company_brand_uuid: Optional[UUID] = None parent_company_uuid: Optional[UUID] = None class LookupParticipantsResponse(BaseModel): """Lookup label participant tenant hierarchy response schema.""" label_participants: List[Optional[LookupParticipant]] @dataclass class OwsParticipantClient: """Connector to ows-participant microservice.""" async_ows_client: AsyncOwsClient service_name = "ows-participant" async def lookup_participants_by_uuids( self, uuids: List[UUID] ) -> LookupParticipantsResponse: """Get Participant Hierarchy for one of more UUIDs. Callers should handle httpx exceptions. """ if not len(uuids): return LookupParticipantsResponse(label_participants=[]) response = await self.async_ows_client.post( self.service_name, path="/lookup/label-participants/uuids/tenant-hierarchy/", json={"uuids": [str(_uuid) for _uuid in uuids]}, timeout=DEFAULT_OWS_PARTICIPANT_REQUEST_TIMEOUT, ) response.raise_for_status() label_participants = LookupParticipantsResponse.model_validate_json( response.content ) # remove null entries return LookupParticipantsResponse( label_participants=[ lp for lp in label_participants.label_participants if lp ] )