from owsclient import OwsClient from pydantic import BaseModel, Field from fansifter_common.context import get_request_context GLOBAL_PARTICIPANT_BY_GP_ID_QUERY = """ query GlobalParticipantByGpId($id: ID!) { globalParticipantByGpId(id: $id) { id name imageUrl } } """ class GlobalParticipant(BaseModel): id: str name: str image_url: str | None = Field(default=None, alias="imageUrl") class GlobalParticipantData(BaseModel): global_participant_by_gp_id: list[GlobalParticipant] = Field( alias="globalParticipantByGpId" ) class GlobalParticipantResponse(BaseModel): data: GlobalParticipantData NOTSET_PROFILE_ID = 9999999999 NOTSET_PROFILE_TYPE = "AudienceProfile" NOTSET_IDENTITY_ID = "1234" class GraphqlRouterClient: service_name = "graphql-router" def __init__(self, ows_client: OwsClient) -> None: self.ows_client = ows_client def get_global_participant_by_gp_id( self, gp_id: str, / ) -> GlobalParticipant | None: profile_id = NOTSET_PROFILE_ID profile_type = NOTSET_PROFILE_TYPE identity_id = NOTSET_IDENTITY_ID request_context = get_request_context() if request_context: if request_context.profile_id: profile_id = request_context.profile_id if request_context.profile_type: profile_type = request_context.profile_type if request_context.identity_id: identity_id = request_context.identity_id response = self.ows_client.graphql_query( self.service_name, query=GLOBAL_PARTICIPANT_BY_GP_ID_QUERY, variables={"id": gp_id}, profile_id=profile_id, profile_type=profile_type, identity_id=identity_id, ) response.raise_for_status() response_obj = GlobalParticipantResponse.model_validate_json(response.content) try: return response_obj.data.global_participant_by_gp_id[0] except IndexError: return None