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