Spaces:
Sleeping
Sleeping
Upload app/api/external.py
Browse files- app/api/external.py +51 -0
app/api/external.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, Query
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
+
from app.db.base import get_db
|
| 5 |
+
from app.services.external_catalog import ExternalCatalogService
|
| 6 |
+
from app.schemas.auth import AuthResponse
|
| 7 |
+
|
| 8 |
+
router = APIRouter(prefix="/external", tags=["External Data"])
|
| 9 |
+
|
| 10 |
+
@router.get("/search", response_model=AuthResponse)
|
| 11 |
+
async def search_external_products(
|
| 12 |
+
query: str = Query(..., description="The search term for products"),
|
| 13 |
+
category_id: int | None = None,
|
| 14 |
+
db: Session = Depends(get_db)
|
| 15 |
+
):
|
| 16 |
+
"""
|
| 17 |
+
Search for products in external catalogs (simulated search-assistant logic).
|
| 18 |
+
"""
|
| 19 |
+
service = ExternalCatalogService(db)
|
| 20 |
+
try:
|
| 21 |
+
# In a real scenario, this would trigger an infsh or MCP call
|
| 22 |
+
results = await service.fetch_real_products(query)
|
| 23 |
+
return AuthResponse(
|
| 24 |
+
isSuccess=True,
|
| 25 |
+
value={"results": results, "query": query},
|
| 26 |
+
statusCode=200
|
| 27 |
+
)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 30 |
+
|
| 31 |
+
@router.post("/import", response_model=AuthResponse)
|
| 32 |
+
async def import_product(
|
| 33 |
+
external_data: Dict[str, Any],
|
| 34 |
+
category_id: int,
|
| 35 |
+
db: Session = Depends(get_db)
|
| 36 |
+
):
|
| 37 |
+
"""
|
| 38 |
+
Import a product from external JSON data.
|
| 39 |
+
"""
|
| 40 |
+
service = ExternalCatalogService(db)
|
| 41 |
+
try:
|
| 42 |
+
product = await service.create_product_from_external(external_data, category_id)
|
| 43 |
+
db.commit()
|
| 44 |
+
return AuthResponse(
|
| 45 |
+
isSuccess=True,
|
| 46 |
+
value={"message": "Product imported successfully", "product_id": product.id},
|
| 47 |
+
statusCode=201
|
| 48 |
+
)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
db.rollback()
|
| 51 |
+
raise HTTPException(status_code=500, detail=str(e))
|