ReMe/reme2/component/client/http_client.py
jinli.yl ca5970c6b0 refactor(component): refactor base components and file store implementations
- Remove abstract methods from base component start/close
- Update BaseJob to remove name parameter and simplify initialization
- Change file modification time field from mtime_ms to modified_time in seconds
- Add type checking imports and improve typing annotations
- Implement LocalFileStore with JSONL persistence for file chunks
- Add MdFileParser with markdown and frontmatter support
- Simplify HttpClient call method with proper kwargs handling
- Remove unused ReMe class methods and create backup version
- Update StreamJob to use step_components instead of steps attribute
2026-04-23 17:44:25 +08:00

62 lines
1.7 KiB
Python

"""HTTP client for ReMe services."""
import json
import os
import httpx
from .base_client import BaseClient
from ..component_registry import R
from ...constants import REME_SERVICE_INFO, REME_DEFAULT_HOST, REME_DEFAULT_PORT
@R.register("http")
class HttpClient(BaseClient):
"""HTTP client for ReMe service."""
def __init__(
self,
action: str,
host: str | None = None,
port: int | None = None,
timeout: float = 30.0,
**kwargs,
):
super().__init__(**kwargs)
if host is not None and port is not None:
pass
elif service_info := os.environ.get(REME_SERVICE_INFO):
try:
data = json.loads(service_info)
host = data.get("host", host)
port = data.get("port", port)
except Exception:
pass
else:
host = REME_DEFAULT_HOST
port = REME_DEFAULT_PORT
self.action = action
self.base_url = f"http://{host}:{port}"
self.timeout = timeout
async def _start(self) -> None:
"""Initialize the HTTP client."""
if self.client is None:
self.client = httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
)
async def __call__(self) -> dict:
if self.client is None:
await self._start()
response = await self.client.post(f"/{self.action}", json=self.kwargs)
response.raise_for_status()
return response.json()
async def _close(self) -> None:
if self.client is not None:
await self.client.aclose()
self.client = None