|
1 | 1 | import mimetypes
|
2 | 2 | import uuid
|
3 | 3 | from collections.abc import AsyncGenerator
|
4 |
| -from contextlib import asynccontextmanager |
| 4 | +from contextlib import asynccontextmanager, suppress |
5 | 5 | from logging import getLogger
|
6 | 6 | from pathlib import Path
|
7 | 7 |
|
|
10 | 10 | from pydantic import BaseModel
|
11 | 11 |
|
12 | 12 | from dive_mcp_host.host.store.base import StoreProtocol
|
| 13 | +from dive_mcp_host.oap_plugin.models import TokenNotSetError |
13 | 14 |
|
14 | 15 | logger = getLogger(__name__)
|
15 | 16 |
|
@@ -37,7 +38,7 @@ def __init__(self) -> None:
|
37 | 38 | async def _get_http_client(self) -> AsyncGenerator[httpx.AsyncClient, None]:
|
38 | 39 | """Get the HTTP client."""
|
39 | 40 | if self._token is None:
|
40 |
| - raise RuntimeError("Token is not set") |
| 41 | + raise TokenNotSetError |
41 | 42 |
|
42 | 43 | async with httpx.AsyncClient(
|
43 | 44 | headers={"Authorization": f"Bearer {self._token}"},
|
@@ -75,21 +76,24 @@ async def save_file(self, file: UploadFile | str) -> str | None:
|
75 | 76 | logger.debug("Saving as file %s", new_name)
|
76 | 77 | files = {"file": (new_name, file.file, file.content_type)}
|
77 | 78 |
|
78 |
| - async with self._get_http_client() as client: |
79 |
| - response = await client.post( |
80 |
| - f"{self._store_url}/upload_file", |
81 |
| - files=files, |
82 |
| - ) |
83 |
| - if not response.is_success: |
84 |
| - logger.error("Failed to save file to the OAP store: %s", response.text) |
85 |
| - return None |
86 |
| - |
87 |
| - json_resp = response.json() |
88 |
| - result = UploadFileResponse.model_validate(json_resp) |
89 |
| - if result.result: |
90 |
| - logger.debug("File saved to the OAP store: %s", result.url) |
91 |
| - return result.url |
92 |
| - return None |
| 79 | + with suppress(TokenNotSetError): |
| 80 | + async with self._get_http_client() as client: |
| 81 | + response = await client.post( |
| 82 | + f"{self._store_url}/upload_file", |
| 83 | + files=files, |
| 84 | + ) |
| 85 | + if not response.is_success: |
| 86 | + logger.error( |
| 87 | + "Failed to save file to the OAP store: %s", response.text |
| 88 | + ) |
| 89 | + return None |
| 90 | + |
| 91 | + json_resp = response.json() |
| 92 | + result = UploadFileResponse.model_validate(json_resp) |
| 93 | + if result.result: |
| 94 | + logger.debug("File saved to the OAP store: %s", result.url) |
| 95 | + return result.url |
| 96 | + return None |
93 | 97 |
|
94 | 98 | async def get_file(self, file_path: str) -> bytes:
|
95 | 99 | """Get file from the store."""
|
|
0 commit comments