You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
|
|
from .client import ServiceClient
|
|
from .config import ServiceConfig
|
|
from .errors import ServiceConfigError
|
|
|
|
|
|
def init_service_clients(app, configs: dict[str, dict[str, Any]] | None = None) -> None:
|
|
clients: dict[str, ServiceClient] = {}
|
|
for name, value in (configs or {}).items():
|
|
if "base_url" not in value:
|
|
raise ServiceConfigError(f"service {name} missing base_url")
|
|
clients[name] = ServiceClient(ServiceConfig.from_mapping(name, value))
|
|
app.state.iti_service_clients = clients
|
|
|
|
|
|
def register_service_client(
|
|
app,
|
|
name: str,
|
|
config: dict[str, Any],
|
|
*,
|
|
transport: httpx.BaseTransport | None = None,
|
|
) -> ServiceClient:
|
|
client = ServiceClient(ServiceConfig.from_mapping(name, config), transport=transport)
|
|
clients = getattr(app.state, "iti_service_clients", {})
|
|
clients[name] = client
|
|
app.state.iti_service_clients = clients
|
|
return client
|
|
|
|
|
|
def service_client(app, name: str) -> ServiceClient:
|
|
clients = getattr(app.state, "iti_service_clients", {})
|
|
client = clients.get(name)
|
|
if client is None:
|
|
raise ServiceConfigError(f"service client not configured: {name}")
|
|
return client
|