Add generic ENLYZE power meter pipeline

This commit is contained in:
admin committed 2026-10-01 11:59:34 +02:00
1 parent 4a3ed625ce
commit e9f9cf6f3c
15 files changed
+680 -4

No files matched your search

+53 -3
View File
@@ -21,12 +21,20 @@ class ConfigurationError(ExplorationError):
class AuthenticationError(ExplorationError):
"""Raised for HTTP 401/403 without exposing credentials or response bodies."""
"""Raised for HTTP 401/403 with a bounded, sanitized response body."""
def __init__(self, message: str, *, response_body: object | None = None) -> None:
super().__init__(message)
self.response_body = response_body
class HttpResponseError(ExplorationError):
"""Raised for a non-successful HTTP response."""
def __init__(self, message: str, *, response_body: object | None = None) -> None:
super().__init__(message)
self.response_body = response_body
class NonJsonResponseError(ExplorationError):
"""Raised when a successful response cannot be decoded as JSON."""
@@ -82,6 +90,30 @@ class ExplorationClient:
"""Issue a documented read-only POST operation with a JSON body."""
return self._request_json("POST", path, payload=payload)
def get_all_pages(
self, path: str, query: Mapping[str, str] | None = None,
) -> list[ExplorationResponse]:
"""Follow the ENLYZE cursor contract and return every response page."""
request_query = dict(query or {})
pages: list[ExplorationResponse] = []
followed: set[str] = set()
while True:
response = self.get(path, request_query)
pages.append(response)
body = response.body
if not isinstance(body, dict) or "metadata" not in body:
return pages
metadata = body["metadata"]
if not isinstance(metadata, dict) or "next_cursor" not in metadata:
raise ExplorationError("Invalid paginated response: metadata.next_cursor is missing.")
cursor = metadata["next_cursor"]
if cursor is None:
return pages
if not isinstance(cursor, str) or not cursor or cursor in followed:
raise ExplorationError("Invalid paginated response: repeated or invalid cursor.")
followed.add(cursor)
request_query["cursor"] = cursor
def _request_json(
self,
method: str,
@@ -118,9 +150,27 @@ class ExplorationClient:
status_code = response.status
response_headers = dict(response.headers.items())
except HTTPError as error:
response_body: object | None = None
try:
raw_error_body = error.read(8192)
try:
from production_analytics.enlyze.sanitize import sanitize
response_body = sanitize(json.loads(raw_error_body))
except (UnicodeDecodeError, json.JSONDecodeError):
response_body = raw_error_body.decode("utf-8", errors="replace")[:2000]
except OSError:
response_body = None
if error.code in {401, 403}:
raise AuthenticationError(f"Authentication or authorization failed (HTTP {error.code}).") from error
raise HttpResponseError(f"HTTP request failed with status {error.code}.") from error
suffix = f" Response: {response_body!r}" if response_body is not None else ""
raise AuthenticationError(
f"Authentication or authorization failed (HTTP {error.code}).{suffix}",
response_body=response_body,
) from error
suffix = f" Response: {response_body!r}" if response_body is not None else ""
raise HttpResponseError(
f"HTTP request failed with status {error.code}.{suffix}",
response_body=response_body,
) from error
except URLError as error:
raise HttpResponseError("HTTP request could not be completed.") from error