from dataclasses import dataclass from typing import Literal, cast import pydantic from audience_common.owsclient import AsyncOwsClient AnyVendorId = Literal["*"] VendorType = Literal["Vendor"] SubaccountType = Literal["Subaccount"] @dataclass(frozen=True) class AnyVendor: id: AnyVendorId type: VendorType = "Vendor" @dataclass(frozen=True) class Vendor: id: int type: VendorType = "Vendor" @dataclass(frozen=True) class Subaccount: id: int vendor_id: int type: SubaccountType = "Subaccount" AnyResource = AnyVendor | Vendor | Subaccount Resource = Vendor | Subaccount @dataclass class OwsPermissionsClient: ows_client: AsyncOwsClient async def get_resources( self, profile_id: int, profile_type: str ) -> list[AnyResource]: response = await self.ows_client.get( "ows-permissions", f"/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all", timeout=5, ) response.raise_for_status() any_resources = [] for item in response.json()["items"]: try: any_resource = pydantic.parse_obj_as(AnyResource, item) # type: ignore except pydantic.ValidationError: continue any_resources.append(any_resource) return cast(list[AnyResource], any_resources)