From 57f322a0e630b3b9ea90cdca47b687128a304f5c Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 21 Jun 2023 10:33:33 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20refactor(endpoints.py):=20rename?= =?UTF-8?q?=20client=5Fid=20to=20flow=5Fid=20in=20create=5Fupload=5Ffile?= =?UTF-8?q?=20endpoint=20=E2=9C=A8=20feat(endpoints.py):=20add=20UploadFil?= =?UTF-8?q?eResponse=20schema=20to=20standardize=20upload=20file=20respons?= =?UTF-8?q?e=20The=20client=5Fid=20parameter=20in=20the=20create=5Fupload?= =?UTF-8?q?=5Ffile=20endpoint=20has=20been=20renamed=20to=20flow=5Fid=20to?= =?UTF-8?q?=20improve=20semantics.=20The=20UploadFileResponse=20schema=20h?= =?UTF-8?q?as=20been=20added=20to=20standardize=20the=20response=20of=20th?= =?UTF-8?q?e=20create=5Fupload=5Ffile=20endpoint.=20The=20response=20now?= =?UTF-8?q?=20includes=20the=20flowId=20and=20file=5Fpath=20fields.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/endpoints.py | 12 ++++++++---- src/backend/langflow/api/v1/schemas.py | 8 ++++++++ src/frontend/src/types/api/index.ts | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index 4a1cf8eae..10f2f4692 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -8,6 +8,7 @@ from fastapi import APIRouter, Depends, HTTPException, UploadFile from langflow.api.v1.schemas import ( PredictRequest, PredictResponse, + UploadFileResponse, ) from langflow.interface.types import build_langchain_types_dict @@ -57,13 +58,16 @@ async def predict_flow( raise HTTPException(status_code=500, detail=str(e)) from e -@router.post("/upload/{client_id}", response_model=dict, status_code=201) -async def create_upload_file(file: UploadFile, client_id: str): +@router.post("/upload/{flow_id}", response_model=UploadFileResponse, status_code=201) +async def create_upload_file(file: UploadFile, flow_id: str): # Cache file try: - file_path = save_uploaded_file(file.file, file_name=client_id) + file_path = save_uploaded_file(file.file, file_name=flow_id) - return {"file_path": file_path} + return UploadFileResponse( + flowId=flow_id, + file_path=file_path, + ) except Exception as exc: logger.error(f"Error saving file: {exc}") raise HTTPException(status_code=500, detail=str(exc)) from exc diff --git a/src/backend/langflow/api/v1/schemas.py b/src/backend/langflow/api/v1/schemas.py index 714f0df7f..747057a27 100644 --- a/src/backend/langflow/api/v1/schemas.py +++ b/src/backend/langflow/api/v1/schemas.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import Any, Dict, List, Optional, Union from langflow.database.models.flow import FlowCreate, FlowRead from pydantic import BaseModel, Field, validator @@ -101,3 +102,10 @@ class InitResponse(BaseModel): class BuiltResponse(BaseModel): built: bool + + +class UploadFileResponse(BaseModel): + """Upload file response schema.""" + + flowId: str + file_path: Path diff --git a/src/frontend/src/types/api/index.ts b/src/frontend/src/types/api/index.ts index 9bf66e3da..e736d4275 100644 --- a/src/frontend/src/types/api/index.ts +++ b/src/frontend/src/types/api/index.ts @@ -49,4 +49,5 @@ export type InitTypeAPI = { export type UploadFileTypeAPI = { file_path: string; + flowId: string; };