import httpx import pydantic from fansifter_common.adapters.ows_account import OwsAccountClient, Subaccount, Vendor from fansifter_common.auth.account import Account from fansifter_common.auth.exceptions import InvalidAccountError class AccountValidator: def __init__(self, ows_account_client: OwsAccountClient) -> None: self.ows_account_client = ows_account_client def validate_account( self, account: Account, *, allow_subaccount: bool = True ) -> Vendor | Subaccount: """Validate account.""" # Validate subaccount if account.is_subaccount: if not allow_subaccount: raise InvalidAccountError("Subaccount not allowed") try: subaccount = self.ows_account_client.get_subaccount( account.subaccount_id ) except (pydantic.ValidationError, httpx.HTTPError) as exc: raise InvalidAccountError("Invalid subaccount_id") from exc else: if subaccount.vendor_id != account.vendor_id: raise InvalidAccountError("Invalid vendor_id") return subaccount # Validate vendor try: return self.ows_account_client.get_vendor(account.vendor_id) except (pydantic.ValidationError, httpx.HTTPError) as exc: raise InvalidAccountError("Invalid vendor_id") from exc