feat: add visual settings and readiness gate
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import base64
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
@@ -8,25 +10,30 @@ from app.config import Settings
|
||||
class BaiduOcrAdapter:
|
||||
OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
|
||||
|
||||
def __init__(self, settings: Settings) -> None:
|
||||
def __init__(self, settings: Settings | Mapping[str, Any]) -> None:
|
||||
self.settings = settings
|
||||
|
||||
def _value(self, key: str, default: str | bool = "") -> str | bool:
|
||||
if isinstance(self.settings, Mapping):
|
||||
return self.settings.get(key, default)
|
||||
return getattr(self.settings, key, default)
|
||||
|
||||
@property
|
||||
def configured(self) -> bool:
|
||||
return bool(
|
||||
self.settings.baidu_ocr_enabled
|
||||
and self.settings.baidu_ocr_api_key
|
||||
and self.settings.baidu_ocr_secret_key
|
||||
self._value("baidu_ocr_enabled")
|
||||
and self._value("baidu_ocr_api_key")
|
||||
and self._value("baidu_ocr_secret_key")
|
||||
)
|
||||
|
||||
async def _access_token(self) -> str:
|
||||
async with httpx.AsyncClient(timeout=20) as client:
|
||||
response = await client.post(
|
||||
self.settings.baidu_ocr_token_url,
|
||||
str(self._value("baidu_ocr_token_url", "https://aip.baidubce.com/oauth/2.0/token")),
|
||||
params={
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": self.settings.baidu_ocr_api_key,
|
||||
"client_secret": self.settings.baidu_ocr_secret_key,
|
||||
"client_id": self._value("baidu_ocr_api_key"),
|
||||
"client_secret": self._value("baidu_ocr_secret_key"),
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
Reference in New Issue
Block a user