Skip to content

Commit

Permalink
Fixes up some typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stumpylog committed Oct 12, 2023
1 parent aa4c388 commit 4d2bbed
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/gotenberg_client/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import datetime
import enum
import re
from typing import Dict
from typing import Optional
from typing import TypedDict
from typing import no_type_check

from gotenberg_client.base import BaseApi

Expand All @@ -20,6 +21,21 @@
)


class _ModuleStatusType(TypedDict):
status: str
timestamp: str


class _AllModulesType(TypedDict):
chromium: _ModuleStatusType
uno: _ModuleStatusType


class _HealthCheckApiResponseType(TypedDict):
status: str
details: _AllModulesType


class StatusOptions(str, enum.Enum):
Up = "up"
Down = "down"
Expand All @@ -37,7 +53,7 @@ class ModuleStatus:


class HealthStatus:
def __init__(self, data: Dict[str, str]) -> None:
def __init__(self, data: _HealthCheckApiResponseType) -> None:
self.data = data
self.overall = StatusOptions(data["status"])

Expand All @@ -51,14 +67,19 @@ def __init__(self, data: Dict[str, str]) -> None:

def _extract_status(self, module: ModuleOptions) -> ModuleStatus:
status = StatusOptions(self.data["details"][module.value]["status"])
timestamp = self._extract_datetime(self.data["details"][module.value]["timestamp"])
return ModuleStatus(status, timestamp)

# mypy is quite wrong here, it's clearly marked as a datetime.datetime, not Any
timestamp = self._extract_datetime(self.data["details"][module.value]["timestamp"]) # type: ignore
# Also wrong here
return ModuleStatus(status, timestamp) # type: ignore

@staticmethod
@no_type_check
def _extract_datetime(timestamp: str) -> datetime.datetime:
m = _TIME_RE.match(timestamp)
if not m:
return None
msg = f"Unable to parse {timestamp}"
raise ValueError(msg)

(year, month, day, hour, minute, second, frac_sec, timezone_str) = m.groups()

Expand Down Expand Up @@ -91,5 +112,5 @@ class HealthCheckApi(BaseApi):

def health(self) -> HealthStatus:
resp = self._client.get(self._HEALTH_ENDPOINT, headers={"Accept": "application/json"})
json_data: Dict[str, str] = resp.raise_for_status().json()
json_data: _HealthCheckApiResponseType = resp.raise_for_status().json()
return HealthStatus(json_data)

0 comments on commit 4d2bbed

Please sign in to comment.