src.awards.http_awards_client_adapter
HTTP adapter for the awards service.
1"""HTTP adapter for the awards service.""" 2 3from typing import override 4 5import msgspec 6 7from src.lib.http_client import HttpClient, RequestsHttpClient 8 9from .awards_client_port import ( 10 AwardForbidden, 11 AwardNotFound, 12 AwardsClientError, 13 AwardsClientPort, 14) 15from .models import Award 16 17 18class HttpAwardsClientAdapter(AwardsClientPort): 19 """ 20 Adapter for the awards HTTP service. This maps to the stoplight mock. Not 21 yet to the STRAPI API!, that would make this a "StrapiAwardsClientAdapter". 22 """ 23 24 _awards_service_base_url: str 25 _http_client: HttpClient 26 27 def __init__( 28 self, 29 awards_service_url: str, 30 http_client: HttpClient | None = None, 31 ) -> None: 32 """Initialize the adapter. 33 34 Args: 35 awards_service_url: The base URL of the awards service. 36 http_client: The HTTP client to use. Defaults to requests. 37 """ 38 self._awards_service_base_url = awards_service_url.rstrip("/") 39 if http_client is not None: 40 self._http_client = http_client 41 else: 42 self._http_client = RequestsHttpClient() 43 44 @override 45 def get(self, award_id: str) -> Award: 46 """Fetch an award by ID from the awards HTTP service. 47 48 Args: 49 award_id: The unique award identifier. 50 51 Returns: 52 The matching Award. 53 54 Raises: 55 AwardNotFound: On 404. 56 AwardForbidden: On 403. 57 AwardsClientError: On other errors or invalid response. 58 """ 59 response = self._http_client.get( 60 f"{self._awards_service_base_url}/awards/{award_id}" 61 ) 62 63 if response.status_code == 404: 64 raise AwardNotFound(f"Award {award_id} not found") 65 66 if response.status_code == 403: 67 raise AwardForbidden(f"Access to award {award_id} denied") 68 69 if 400 <= response.status_code < 600: 70 raise AwardsClientError( 71 f"Upstream error: {response.status_code} - {response.content.decode()}" 72 ) 73 74 try: 75 return msgspec.json.decode(response.content, type=Award) 76 except msgspec.DecodeError as e: 77 raise AwardsClientError(f"Invalid response from awards service: {e}") from e
19class HttpAwardsClientAdapter(AwardsClientPort): 20 """ 21 Adapter for the awards HTTP service. This maps to the stoplight mock. Not 22 yet to the STRAPI API!, that would make this a "StrapiAwardsClientAdapter". 23 """ 24 25 _awards_service_base_url: str 26 _http_client: HttpClient 27 28 def __init__( 29 self, 30 awards_service_url: str, 31 http_client: HttpClient | None = None, 32 ) -> None: 33 """Initialize the adapter. 34 35 Args: 36 awards_service_url: The base URL of the awards service. 37 http_client: The HTTP client to use. Defaults to requests. 38 """ 39 self._awards_service_base_url = awards_service_url.rstrip("/") 40 if http_client is not None: 41 self._http_client = http_client 42 else: 43 self._http_client = RequestsHttpClient() 44 45 @override 46 def get(self, award_id: str) -> Award: 47 """Fetch an award by ID from the awards HTTP service. 48 49 Args: 50 award_id: The unique award identifier. 51 52 Returns: 53 The matching Award. 54 55 Raises: 56 AwardNotFound: On 404. 57 AwardForbidden: On 403. 58 AwardsClientError: On other errors or invalid response. 59 """ 60 response = self._http_client.get( 61 f"{self._awards_service_base_url}/awards/{award_id}" 62 ) 63 64 if response.status_code == 404: 65 raise AwardNotFound(f"Award {award_id} not found") 66 67 if response.status_code == 403: 68 raise AwardForbidden(f"Access to award {award_id} denied") 69 70 if 400 <= response.status_code < 600: 71 raise AwardsClientError( 72 f"Upstream error: {response.status_code} - {response.content.decode()}" 73 ) 74 75 try: 76 return msgspec.json.decode(response.content, type=Award) 77 except msgspec.DecodeError as e: 78 raise AwardsClientError(f"Invalid response from awards service: {e}") from e
Adapter for the awards HTTP service. This maps to the stoplight mock. Not yet to the STRAPI API!, that would make this a "StrapiAwardsClientAdapter".
HttpAwardsClientAdapter( awards_service_url: str, http_client: src.lib.http_client.HttpClient | None = None)
28 def __init__( 29 self, 30 awards_service_url: str, 31 http_client: HttpClient | None = None, 32 ) -> None: 33 """Initialize the adapter. 34 35 Args: 36 awards_service_url: The base URL of the awards service. 37 http_client: The HTTP client to use. Defaults to requests. 38 """ 39 self._awards_service_base_url = awards_service_url.rstrip("/") 40 if http_client is not None: 41 self._http_client = http_client 42 else: 43 self._http_client = RequestsHttpClient()
Initialize the adapter.
Args: awards_service_url: The base URL of the awards service. http_client: The HTTP client to use. Defaults to requests.
45 @override 46 def get(self, award_id: str) -> Award: 47 """Fetch an award by ID from the awards HTTP service. 48 49 Args: 50 award_id: The unique award identifier. 51 52 Returns: 53 The matching Award. 54 55 Raises: 56 AwardNotFound: On 404. 57 AwardForbidden: On 403. 58 AwardsClientError: On other errors or invalid response. 59 """ 60 response = self._http_client.get( 61 f"{self._awards_service_base_url}/awards/{award_id}" 62 ) 63 64 if response.status_code == 404: 65 raise AwardNotFound(f"Award {award_id} not found") 66 67 if response.status_code == 403: 68 raise AwardForbidden(f"Access to award {award_id} denied") 69 70 if 400 <= response.status_code < 600: 71 raise AwardsClientError( 72 f"Upstream error: {response.status_code} - {response.content.decode()}" 73 ) 74 75 try: 76 return msgspec.json.decode(response.content, type=Award) 77 except msgspec.DecodeError as e: 78 raise AwardsClientError(f"Invalid response from awards service: {e}") from e
Fetch an award by ID from the awards HTTP service.
Args: award_id: The unique award identifier.
Returns: The matching Award.
Raises: AwardNotFound: On 404. AwardForbidden: On 403. AwardsClientError: On other errors or invalid response.