"""HTTP client for ows-contributor integration tests.""" import os from typing import Any import requests from contributor.api.schemas.global_participants import GlobalParticipant from contributor.api.schemas.products import ( ProductContributors, ProductParticipationsResponse, ) from contributor.api.schemas.tracks import TrackParticipations from contributor.api.schemas.participation_roles import ( ParticipationRole, ParticipationRoleCategory, ParticipationRolesByCategory, ) class OwsContributor: def __init__(self, *, jwt: str): self.jwt = jwt self.base_url = os.environ.get("QA_BASE_URL", "http://localhost:5000") @property def _headers(self) -> dict[str, str]: return {"Authorization": f"Bearer {self.jwt}"} def get_contributor( self, *, uuid: str, expected_status_code: int = 200 ) -> dict[str, Any]: response = requests.get( f"{self.base_url}/contributors/{uuid}", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /contributors/{uuid} → {response.status_code}: {response.text}" ) return response.json() def get_contributors( self, *, uuids: list[str], expected_status_code: int = 200 ) -> list[dict[str, Any]]: response = requests.post( f"{self.base_url}/contributors/dataloader", headers=self._headers, json={"contributors": [{"uuid": uuid} for uuid in uuids]}, ) assert response.status_code == expected_status_code, ( f"POST /contributors/dataloader → {response.status_code}: {response.text}" ) return response.json() def update_contributor( self, *, uuid: str, body: dict[str, Any], expected_status_code: int = 200, ) -> dict[str, Any]: response = requests.patch( f"{self.base_url}/contributors/{uuid}", headers=self._headers, json=body, ) assert response.status_code == expected_status_code, ( f"PATCH /contributors/{uuid} → {response.status_code}: {response.text}" ) return response.json() def create_contributor( self, *, body: dict[str, Any], expected_status_code: int = 201, ) -> dict[str, Any]: response = requests.post( f"{self.base_url}/contributors", headers=self._headers, json=body, ) assert response.status_code == expected_status_code, ( f"POST /contributors → {response.status_code}: {response.text}" ) return response.json() def get_global_participant( self, *, id: str, expected_status_code: int = 200 ) -> dict[str, Any]: response = requests.get( f"{self.base_url}/global-participants/{id}", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /global-participants/{id} → {response.status_code}: {response.text}" ) return response.json() def get_global_participants( self, *, ids: list[str], expected_status_code: int = 200 ) -> list[GlobalParticipant]: response = requests.post( f"{self.base_url}/global-participants/dataloader", headers=self._headers, json={"global_participants": [{"id": id} for id in ids]}, ) assert response.status_code == expected_status_code, ( f"POST /global-participants/dataloader → {response.status_code}: {response.text}" ) return [ GlobalParticipant.model_validate(global_participant) for global_participant in response.json() ] def get_track_participations( self, *, uuid: str, expected_status_code: int = 200 ) -> dict[str, Any]: response = requests.get( f"{self.base_url}/contributors/{uuid}/track-participations", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /contributors/{uuid}/track-participations → {response.status_code}: {response.text}" ) return response.json() def get_product_participations( self, *, contributor_uuid: str, expected_status_code: int = 200 ) -> ProductParticipationsResponse: response = requests.get( f"{self.base_url}/contributors/{contributor_uuid}/product-participations", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /contributors/{contributor_uuid}/product-participations → {response.status_code}: {response.text}" ) return ProductParticipationsResponse.model_validate(response.json()) def get_sound_recording_participations( self, *, uuid: str, expected_status_code: int = 200 ) -> dict[str, Any]: response = requests.get( f"{self.base_url}/contributors/{uuid}/sound-recording-participations", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /contributors/{uuid}/sound-recording-participations → {response.status_code}: {response.text}" ) return response.json() def get_contributor_projects( self, *, uuid: str, expected_status_code: int = 200 ) -> dict[str, Any]: response = requests.get( f"{self.base_url}/contributors/{uuid}/projects", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /contributors/{uuid}/projects → {response.status_code}: {response.text}" ) return response.json() def get_product_contributors( self, *, product_id: int, expected_status_code: int = 200, ) -> ProductContributors | None: response = requests.get( f"{self.base_url}/products/{product_id}", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /products/{product_id} → {response.status_code}: {response.text}" ) if response: return ProductContributors.model_validate(response.json()) def get_bulk_product_contributors( self, *, product_ids: list[int], expected_status_code: int = 200, ) -> list[ProductContributors]: response = requests.post( f"{self.base_url}/products/dataloader", headers=self._headers, json={ "products": [{"product_id": product_id} for product_id in product_ids] }, ) assert response.status_code == expected_status_code, ( f"POST /products/dataloader → {response.status_code}: {response.text}" ) return [ ProductContributors.model_validate(product_contributor) for product_contributor in response.json() ] def get_role_categories( self, *, expected_status_code: int = 200, ) -> list[ParticipationRoleCategory]: response = requests.get( f"{self.base_url}/participation-roles/categories", headers=self._headers, ) assert response.status_code == expected_status_code, ( "GET /participation-roles/categories " f"→ {response.status_code}: {response.text}" ) return [ ParticipationRoleCategory.model_validate(category) for category in response.json() ] def get_bulk_roles_by_category_uuid( self, *, category_uuids: list[str], expected_status_code: int = 200, ) -> list[ParticipationRolesByCategory]: response = requests.post( f"{self.base_url}/participation-roles/categories/roles/dataloader", headers=self._headers, json={"categories": [{"uuid": uuid} for uuid in category_uuids]}, ) assert response.status_code == expected_status_code, ( "POST /participation-roles/categories/roles/dataloader " f"→ {response.status_code}: {response.text}" ) return [ ParticipationRolesByCategory.model_validate(result) for result in response.json() ] def get_roles_by_category_uuid( self, *, uuid: str, expected_status_code: int = 200, ) -> ParticipationRolesByCategory: response = requests.get( f"{self.base_url}/participation-roles/categories/{uuid}/roles", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /participation-roles/categories/{uuid}/roles " f"→ {response.status_code}: {response.text}" ) return ParticipationRolesByCategory.model_validate(response.json()) def get_role_by_uuid( self, *, uuid: str, expected_status_code: int = 200, ) -> ParticipationRole | None: response = requests.get( f"{self.base_url}/participation-roles/{uuid}", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /participation-roles/{uuid} → {response.status_code}: {response.text}" ) if response: return ParticipationRole.model_validate(response.json()) return None def get_track_participations_for_track( self, *, tuid: int, expected_status_code: int = 200 ) -> TrackParticipations: response = requests.get( f"{self.base_url}/tracks/{tuid}", headers=self._headers, ) assert response.status_code == expected_status_code, ( f"GET /tracks/{tuid} → {response.status_code}: {response.text}" ) return TrackParticipations.model_validate(response.json()) def get_bulk_track_participations( self, *, tuids: list[int], expected_status_code: int = 200, ) -> list[TrackParticipations]: response = requests.post( f"{self.base_url}/tracks/dataloader", headers=self._headers, json={"tracks": [{"tuid": tuid} for tuid in tuids]}, ) assert response.status_code == expected_status_code, ( f"POST /tracks/dataloader → {response.status_code}: {response.text}" ) return [TrackParticipations.model_validate(track) for track in response.json()]