♻️ refactor: Migrando a integração de dados fictícios para FakerAPI e ajustando a chamada das tools no Vertex AI.
parent
37fa127a80
commit
e68b32a177
@ -0,0 +1,57 @@
|
|||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from app.core.settings import settings
|
||||||
|
|
||||||
|
|
||||||
|
class FakerApiClient:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
base_url: Optional[str] = None,
|
||||||
|
locale: Optional[str] = None,
|
||||||
|
seed: Optional[int] = None,
|
||||||
|
):
|
||||||
|
self.base_url = (base_url or settings.fakerapi_base_url).rstrip("/")
|
||||||
|
self.locale = locale or settings.fakerapi_locale
|
||||||
|
self.seed = settings.fakerapi_seed if seed is None else seed
|
||||||
|
|
||||||
|
async def fetch_resource(
|
||||||
|
self,
|
||||||
|
resource: str,
|
||||||
|
quantity: int,
|
||||||
|
extra_params: Optional[Dict[str, Any]] = None,
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
url = f"{self.base_url}/{resource.lstrip('/')}"
|
||||||
|
params: Dict[str, Any] = {
|
||||||
|
"_quantity": quantity,
|
||||||
|
"_locale": self.locale,
|
||||||
|
"_seed": self.seed,
|
||||||
|
}
|
||||||
|
if extra_params:
|
||||||
|
params.update(extra_params)
|
||||||
|
|
||||||
|
timeout = httpx.Timeout(connect=5.0, read=15.0, write=10.0, pool=5.0)
|
||||||
|
headers = {
|
||||||
|
"Accept": "application/json",
|
||||||
|
"User-Agent": "orquestrador-fakerapi-client/1.0",
|
||||||
|
}
|
||||||
|
async with httpx.AsyncClient(timeout=timeout, headers=headers) as client:
|
||||||
|
try:
|
||||||
|
response = await client.get(url, params=params)
|
||||||
|
response.raise_for_status()
|
||||||
|
payload = response.json()
|
||||||
|
except httpx.ReadTimeout:
|
||||||
|
# Retry once with smaller payload to reduce timeout risk in free/public APIs.
|
||||||
|
reduced_quantity = min(quantity, 20)
|
||||||
|
retry_params = dict(params)
|
||||||
|
retry_params["_quantity"] = reduced_quantity
|
||||||
|
response = await client.get(url, params=retry_params)
|
||||||
|
response.raise_for_status()
|
||||||
|
payload = response.json()
|
||||||
|
|
||||||
|
if isinstance(payload, dict) and isinstance(payload.get("data"), list):
|
||||||
|
return payload["data"]
|
||||||
|
if isinstance(payload, list):
|
||||||
|
return payload
|
||||||
|
return []
|
||||||
@ -1,60 +0,0 @@
|
|||||||
from typing import Any, Dict, List, Optional
|
|
||||||
|
|
||||||
import httpx
|
|
||||||
|
|
||||||
from app.core.settings import settings
|
|
||||||
|
|
||||||
class MockarooClient:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
api_key: Optional[str] = None,
|
|
||||||
base_url: Optional[str] = None,
|
|
||||||
):
|
|
||||||
self.api_key = api_key or settings.mockaroo_api_key
|
|
||||||
self.base_url = (base_url or settings.mockaroo_base_url).rstrip("/")
|
|
||||||
|
|
||||||
async def fetch_schema_data(
|
|
||||||
self,
|
|
||||||
schema_name: str,
|
|
||||||
count: int = 100,
|
|
||||||
extra_params: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> List[Dict[str, Any]]:
|
|
||||||
url = f"{self.base_url}/{schema_name}"
|
|
||||||
params: Dict[str, Any] = {
|
|
||||||
"key": self.api_key,
|
|
||||||
"count": count,
|
|
||||||
}
|
|
||||||
if extra_params:
|
|
||||||
params.update(extra_params)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
response = await client.get(url, params=params)
|
|
||||||
response.raise_for_status()
|
|
||||||
data = response.json()
|
|
||||||
if isinstance(data, list):
|
|
||||||
return data
|
|
||||||
return [data]
|
|
||||||
|
|
||||||
async def post_json(self, route: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
|
||||||
url = f"{self.base_url}/{route}"
|
|
||||||
params = {"key": self.api_key}
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
response = await client.post(url, params=params, json=payload)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
async def put_json(self, route: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
|
||||||
url = f"{self.base_url}/{route}"
|
|
||||||
params = {"key": self.api_key}
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
response = await client.put(url, params=params, json=payload)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
async def delete_json(self, route: str, payload: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
||||||
url = f"{self.base_url}/{route}"
|
|
||||||
params = {"key": self.api_key}
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
response = await client.delete(url, params=params, json=payload or {})
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
Loading…
Reference in New Issue