from typing import Annotated from celery.result import AsyncResult from fastapi import FastAPI, Depends, HTTPException from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer from starlette import status from starlette.responses import JSONResponse import background.tasks from background.tasks import * from schemas.general import UpdateRequest, UpdateResponse auth_schema = HTTPBearer() async def check_auth(token: Annotated[HTTPAuthorizationCredentials, Depends(auth_schema)]): if token.credentials != 'vvHh1QNl7lS6c7OVwmxU1TVNd7DLlc9W810csZGf4rkqOrBy6fQwlhIDZsQZd9hQYZYK47yWv33aCq': raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Invalid credentials') app = FastAPI( dependencies=[Depends(check_auth)] ) @app.post( '/update', response_model=UpdateResponse ) async def update( request: UpdateRequest ): task = background.tasks.process_update.delay(request.product_ids) return UpdateResponse(task_id=task.id) @app.get("/tasks/{task_id}") def get_status(task_id): task_result = AsyncResult(task_id) result = { "task_id": task_id, "task_status": task_result.status, "task_result": task_result.result } return JSONResponse(result)