feat: endpoint basic examples add cache Increase interface speed (#7124)

* Update py_autofix.yml

* feat: endpoint basic_examples add cache

* Revert "Update py_autofix.yml"

This reverts commit 7aed45e98602f57c2d6d33532080bad30cf02507.

* resolve conflicts

* refactor: update response handling in read_basic_examples function

---------

Co-authored-by: tianzhipeng <tzpabc@gmail.com>
Co-authored-by: caojianwei-jk <caojianwei-jk@360shuke.com>
This commit is contained in:
jeevi cao 2025-05-13 14:35:23 +08:00 committed by GitHub
commit 4ce5a4228c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,7 +11,7 @@ from uuid import UUID
import orjson
from aiofile import async_open
from anyio import Path
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
from fastapi import APIRouter, Depends, File, HTTPException, Response, UploadFile
from fastapi.encoders import jsonable_encoder
from fastapi.responses import StreamingResponse
from fastapi_pagination import Page, Params
@ -526,6 +526,9 @@ async def download_multiple_file(
return flows_without_api_keys[0]
all_starter_folder_flows_response: Response | None = None
@router.get("/basic_examples/", response_model=list[FlowRead], status_code=200)
async def read_basic_examples(
*,
@ -540,17 +543,24 @@ async def read_basic_examples(
list[FlowRead]: A list of basic example flows.
"""
try:
# Get the starter project
global all_starter_folder_flows_response # noqa: PLW0603
if all_starter_folder_flows_response:
return all_starter_folder_flows_response
# Get the starter folder
starter_folder = (await session.exec(select(Folder).where(Folder.name == STARTER_FOLDER_NAME))).first()
if not starter_folder:
return []
# Get all flows in the starter project
flows = (await session.exec(select(Flow).where(Flow.folder_id == starter_folder.id))).all()
# Get all flows in the starter folder
all_starter_folder_flows = (await session.exec(select(Flow).where(Flow.folder_id == starter_folder.id))).all()
flow_reads = [FlowRead.model_validate(flow, from_attributes=True) for flow in all_starter_folder_flows]
all_starter_folder_flows_response = compress_response(flow_reads)
# Return compressed response using our utility function
return compress_response(flows)
return all_starter_folder_flows_response # noqa: TRY300
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e