from anydi import singleton from fansifter_common.identifiers.types import FansifterProfileIdentifier from httpx import HTTPError from owsclient import OwsClient from pydantic import BaseModel, ConfigDict, Field from ows_text_campaigns.adapters.exceptions import PreferenceCenterError class FanProfile(BaseModel): model_config = ConfigDict(populate_by_name=True) profile_id: str = Field(alias="profileId") @property def identifier(self) -> FansifterProfileIdentifier: profile_identifier = FansifterProfileIdentifier.model_construct( profile_id=self.profile_id ) return profile_identifier @singleton class OwsPreferenceCenterClient: service_name = "ows-preference-center" def __init__(self, ows_client: OwsClient) -> None: self.ows_client = ows_client def get_fan_profile(self, fan_phone_number: str) -> FanProfile: path = f"/profile/by-phone-number/{fan_phone_number}" try: response = self.ows_client.get(self.service_name, path) response.raise_for_status() fan_profile = FanProfile.model_validate_json(response.content) return fan_profile except HTTPError as exc: raise PreferenceCenterError("Failed to retrieve fan profile") from exc