|
| 1 | +from abc import abstractmethod |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from .base import DEFAULT_SULU_CE_ENDPOINT, DEFAULT_SULU_EXTRA_CE_ENDPOINT |
| 7 | + |
| 8 | + |
| 9 | +class BaseSuluClient: |
| 10 | + |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + *, |
| 14 | + endpoint: Union[str, None] = None, |
| 15 | + auth_token: Union[str, None] = None, |
| 16 | + ): |
| 17 | + if endpoint is None: |
| 18 | + endpoint = self.default_endpoint |
| 19 | + self.endpoint = endpoint |
| 20 | + self.auth_token = auth_token |
| 21 | + self._session = requests.Session() |
| 22 | + |
| 23 | + def get_about(self) -> dict: |
| 24 | + # TODO: Potentially think about caching the successful return. |
| 25 | + headers = {"Authorization": f"Bearer {self.auth_token}"} |
| 26 | + r = requests.get(f"{self.endpoint}/about", headers=headers) |
| 27 | + r.raise_for_status() |
| 28 | + self._session.headers.update(headers) |
| 29 | + return r.json() |
| 30 | + |
| 31 | + def get_config_info(self) -> dict: |
| 32 | + # TODO: Potentially think about caching the successful return. |
| 33 | + headers = {"Authorization": f"Bearer {self.auth_token}"} |
| 34 | + r = requests.get(f"{self.endpoint}/config_info", headers=headers) |
| 35 | + r.raise_for_status() |
| 36 | + self._session.headers.update(headers) |
| 37 | + return r.json() |
| 38 | + |
| 39 | + def get_statuses(self) -> list[dict]: |
| 40 | + # TODO: Potentially think about caching the successful return. |
| 41 | + # TODO: Add docs about Status enum. |
| 42 | + headers = {"Authorization": f"Bearer {self.auth_token}"} |
| 43 | + r = requests.get(f"{self.endpoint}/statuses", headers=headers) |
| 44 | + r.raise_for_status() |
| 45 | + self._session.headers.update(headers) |
| 46 | + return r.json() |
| 47 | + |
| 48 | + def get_languages(self, *, language_id: Union[int, None] = None) -> list[dict]: |
| 49 | + # TODO: Potentially think about caching the successful return. |
| 50 | + headers = {"Authorization": f"Bearer {self.auth_token}"} |
| 51 | + request_url = f"{self.endpoint}/languages" |
| 52 | + if language_id is not None: |
| 53 | + request_url = f"{request_url}/{language_id}" |
| 54 | + r = requests.get(request_url, headers=headers) |
| 55 | + r.raise_for_status() |
| 56 | + self._session.headers.update(headers) |
| 57 | + return r.json() |
| 58 | + |
| 59 | + @property |
| 60 | + @abstractmethod |
| 61 | + def default_endpoint(self) -> str: |
| 62 | + raise NotImplementedError("Subclasses must define a default endpoint property.") |
| 63 | + |
| 64 | + |
| 65 | +class SuluCEClient(BaseSuluClient): |
| 66 | + |
| 67 | + @property |
| 68 | + def default_endpoint(self): |
| 69 | + return DEFAULT_SULU_CE_ENDPOINT |
| 70 | + |
| 71 | + |
| 72 | +class SuluExtraCEClient(BaseSuluClient): |
| 73 | + |
| 74 | + @property |
| 75 | + def default_endpoint(self): |
| 76 | + return DEFAULT_SULU_EXTRA_CE_ENDPOINT |
0 commit comments