Feat: Desacoplamento

This commit is contained in:
2026-03-12 15:02:26 -03:00
parent 08be8e314d
commit 8c3c23c65f
11 changed files with 231 additions and 163 deletions

View File

@@ -0,0 +1,29 @@
import asyncio
import json
from datetime import datetime
import boto3
from utils.config import AWS_REGION, OUTPUT_BUCKET, API_VERSION
_s3_output = boto3.client("s3", region_name=AWS_REGION)
async def save_results(results: list[dict]) -> None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
async def _save_one(guia_result: dict):
numero_guia = guia_result.get("guia", {}).get("codigoGuiaLocal", "unknown")
key = f"{API_VERSION}/{numero_guia}_{timestamp}.json"
await asyncio.to_thread(
_s3_output.put_object,
Bucket=OUTPUT_BUCKET,
Key=key,
Body=json.dumps(guia_result, ensure_ascii=False),
ContentType="application/json",
)
try:
await asyncio.gather(*[_save_one(g) for g in results])
except Exception:
pass