diff --git a/backend/main.py b/backend/main.py index 5a11513..cbef52d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -73,6 +73,7 @@ async def lifespan(app: FastAPI): task = asyncio.create_task(refresh_loop()) yield task.cancel() + await odoo_client.close() app = FastAPI(title="TV Dashboard API", lifespan=lifespan) diff --git a/backend/modules/odoo_client.py b/backend/modules/odoo_client.py index 7e2e1b0..279cb11 100644 --- a/backend/modules/odoo_client.py +++ b/backend/modules/odoo_client.py @@ -10,6 +10,16 @@ class OdooClient: self.username = username self.password = password self.uid: int | None = None + self._client: httpx.AsyncClient | None = None + + async def _get_client(self) -> httpx.AsyncClient: + if self._client is None or self._client.is_closed: + self._client = httpx.AsyncClient(timeout=30.0) + return self._client + + async def close(self) -> None: + if self._client and not self._client.is_closed: + await self._client.aclose() async def _jsonrpc(self, endpoint: str, params: dict[str, Any]) -> Any: payload = { @@ -18,24 +28,27 @@ class OdooClient: "params": params, "id": 1, } - async with httpx.AsyncClient() as client: - response = await client.post( - f"{self.url}{endpoint}", - json=payload, - timeout=30.0, - ) - response.raise_for_status() - result = response.json() - if "error" in result: - raise Exception(f"Odoo error: {result['error']}") - return result.get("result") + client = await self._get_client() + response = await client.post( + f"{self.url}{endpoint}", + json=payload, + ) + response.raise_for_status() + result = response.json() + if "error" in result: + raise Exception(f"Odoo error: {result['error']}") + return result.get("result") async def authenticate(self) -> int: - self.uid = await self._jsonrpc("/web/session/authenticate", { + result = await self._jsonrpc("/web/session/authenticate", { "db": self.database, "login": self.username, "password": self.password, }) + if isinstance(result, dict): + self.uid = result.get("uid") + else: + self.uid = result return self.uid async def search_read( @@ -46,7 +59,7 @@ class OdooClient: limit: int = 0, order: str = "", ) -> list[dict[str, Any]]: - return await self._jsonrpc("/web/dataset/call_kw", { + return await self._jsonrpc(f"/web/dataset/call_kw/{model}/search_read", { "model": model, "method": "search_read", "args": [domain or []], @@ -73,7 +86,7 @@ class OdooClient: domain=domain, fields=[ "name", "project_id", "stage_id", "user_ids", - "priority", "date_deadline", "kanban_state", + "priority", "date_deadline", ], )