adision de bases para trabajo completo de back-v1.0.0
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Body
|
||||
from app.modules.edos.service import XMACSVService
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
from app.helpers.extractCsv import CSVExtractor
|
||||
|
||||
router = APIRouter(prefix='/api/csv', tags=['CSV Import'])
|
||||
|
||||
@router.post('/import-from-url')
|
||||
async def import_csv(data: Dict[str, Any] = Body(..., example={"url": "http://example.com/data.csv"})):
|
||||
"""Endpoint to download CSV and process, write and see on DB"""
|
||||
|
||||
|
||||
csv_url = data.get('url')
|
||||
dry_run = data.get('dry_run', True)
|
||||
max_rows = data.get('max_rows')
|
||||
|
||||
if not csv_url:
|
||||
raise HTTPException(400, "url del CSV requerida")
|
||||
|
||||
if not isinstance(csv_url, str):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="la URL debe ser un texto valido"
|
||||
)
|
||||
|
||||
try:
|
||||
result = XMACSVService.process_csv_from_url(
|
||||
csv_url= csv_url,
|
||||
dry_run=dry_run,
|
||||
max_rows=max_rows,
|
||||
)
|
||||
|
||||
return {
|
||||
'status' : 'success',
|
||||
'message': 'Procesamiento completado',
|
||||
'data': result
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error on endpoint: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Procesing CSV error: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post('/import-from-url/v2')
|
||||
async def import_csv(data: dict):
|
||||
"""Endpoint simple para probar el extractor"""
|
||||
csv_url = data.get('url')
|
||||
|
||||
if not csv_url:
|
||||
raise HTTPException(400, "url del CSV requerida")
|
||||
|
||||
try:
|
||||
# Probar extractor
|
||||
content = CSVExtractor.download_csv(csv_url)
|
||||
rows = CSVExtractor.read_csv(content)
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'CSV procesado correctamente',
|
||||
'data': {
|
||||
'url': csv_url,
|
||||
'total_filas': len(rows),
|
||||
'primeras_filas': rows[:3] if rows else [],
|
||||
'columnas': list(rows[0].keys()) if rows else []
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(500, detail=str(e))
|
||||
Reference in New Issue
Block a user