refactor(photon): use TYPE_CHECKING for httpx import and fix client ref

This commit is contained in:
underthestars-zhy
2026-06-08 21:03:58 -07:00
committed by Teknium
parent 4e4d27875f
commit b3aef57f21
2 changed files with 17 additions and 9 deletions
+16 -8
View File
@@ -36,14 +36,21 @@ import sys
import time import time
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Optional from typing import TYPE_CHECKING, Any, Dict, Optional
try: if TYPE_CHECKING:
# Type checkers see ``httpx`` as the always-imported module, so every use
# site type-checks cleanly. The runtime fallback below keeps the optional
# dependency truly optional (each use site is guarded by HTTPX_AVAILABLE).
import httpx import httpx
HTTPX_AVAILABLE = True HTTPX_AVAILABLE = True
except ImportError: # pragma: no cover - httpx is already a Hermes dep else:
HTTPX_AVAILABLE = False try:
httpx = None # type: ignore[assignment] import httpx
HTTPX_AVAILABLE = True
except ImportError: # pragma: no cover - httpx is already a Hermes dep
HTTPX_AVAILABLE = False
httpx = None
from gateway.config import Platform, PlatformConfig from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import ( from gateway.platforms.base import (
@@ -281,7 +288,8 @@ class PhotonAdapter(BasePlatformAdapter):
) )
return False return False
self._http_client = httpx.AsyncClient(timeout=30.0) client = httpx.AsyncClient(timeout=30.0)
self._http_client = client
# The sidecar holds the gRPC stream for BOTH directions, so it is # The sidecar holds the gRPC stream for BOTH directions, so it is
# required now (not just for outbound). # required now (not just for outbound).
@@ -294,7 +302,7 @@ class PhotonAdapter(BasePlatformAdapter):
f"failed to start Photon sidecar: {e}", f"failed to start Photon sidecar: {e}",
retryable=True, retryable=True,
) )
await self._http_client.aclose() await client.aclose()
self._http_client = None self._http_client = None
return False return False
else: else:
@@ -903,7 +911,7 @@ def register(ctx) -> None:
ctx.register_platform( ctx.register_platform(
name="photon", name="photon",
label="Photon iMessage", label="iMessage via Photon",
adapter_factory=lambda cfg: PhotonAdapter(cfg), adapter_factory=lambda cfg: PhotonAdapter(cfg),
check_fn=check_requirements, check_fn=check_requirements,
validate_config=validate_config, validate_config=validate_config,
+1 -1
View File
@@ -1,5 +1,5 @@
name: photon-platform name: photon-platform
label: Photon iMessage label: iMessage via Photon
kind: platform kind: platform
version: 0.2.0 version: 0.2.0
description: > description: >