🔧 fix(store.py): change list_components function name to query_components for better semantics and consistency
✨ feat(store.py): add `count_components` endpoint to get the count of components based on filter criteria 🔧 fix(service.py): modify `query_components` function to return a list of dictionaries when `count` parameter is True
This commit is contained in:
parent
fd4aa3648a
commit
c9b77ec9db
2 changed files with 39 additions and 10 deletions
|
|
@ -66,14 +66,35 @@ def list_components(
|
|||
):
|
||||
try:
|
||||
fields = ["id", "name", "description", "user_created.name", "is_component"]
|
||||
result = store_service.list_components(
|
||||
store_api_Key, page, limit, fields=fields, filter_by_user=filter_by_user
|
||||
result = store_service.query_components(
|
||||
store_api_Key,
|
||||
page,
|
||||
limit,
|
||||
fields=fields,
|
||||
filter_by_user=filter_by_user,
|
||||
)
|
||||
return result
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
|
||||
@router.get("/components/count", response_model=dict)
|
||||
def count_components(
|
||||
filter_by_user: bool = Query(False),
|
||||
store_service: StoreService = Depends(get_store_service),
|
||||
store_api_Key: str = Depends(get_optional_user_store_api_key),
|
||||
):
|
||||
try:
|
||||
result = store_service.query_components(
|
||||
store_api_Key,
|
||||
count=True,
|
||||
filter_by_user=filter_by_user,
|
||||
)
|
||||
return {"count": result[0].get("count", 0)}
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
|
||||
@router.get("/components/{component_id}", response_model=DownloadComponentResponse)
|
||||
def read_component(
|
||||
component_id: UUID,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from datetime import datetime
|
|||
import json
|
||||
from uuid import UUID
|
||||
from langflow.services.base import Service
|
||||
from typing import TYPE_CHECKING, List, Dict, Any, Optional
|
||||
from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union
|
||||
import httpx
|
||||
|
||||
from httpx import HTTPError
|
||||
|
|
@ -119,21 +119,27 @@ class StoreService(Service):
|
|||
results = self._get(self.components_url, api_key, params)
|
||||
return [ComponentResponse(**component) for component in results]
|
||||
|
||||
def list_components(
|
||||
def query_components(
|
||||
self,
|
||||
api_key: str,
|
||||
page: int = 1,
|
||||
limit: int = 15,
|
||||
fields: Optional[List[str]] = None,
|
||||
filter_by_user: bool = False,
|
||||
) -> List[ListComponentResponse]:
|
||||
count: bool = False,
|
||||
) -> Union[List[ListComponentResponse], List[Dict[str, int]]]:
|
||||
params = {"page": page, "limit": limit}
|
||||
# ?aggregate[count]=likes
|
||||
params["fields"] = (
|
||||
",".join(fields)
|
||||
if fields
|
||||
else ",".join(["id", "name", "description", "count(likes)", "is_component"])
|
||||
)
|
||||
if count:
|
||||
params["aggregate"] = json.dumps({"count": "*"})
|
||||
else:
|
||||
params["fields"] = (
|
||||
",".join(fields)
|
||||
if fields
|
||||
else ",".join(
|
||||
["id", "name", "description", "count(likes)", "is_component"]
|
||||
)
|
||||
)
|
||||
# Only public components or the ones created by the user
|
||||
# check for "public" or "Public"
|
||||
|
||||
|
|
@ -151,6 +157,8 @@ class StoreService(Service):
|
|||
)
|
||||
|
||||
results = self._get(self.components_url, api_key, params)
|
||||
if "count" in results[0]:
|
||||
return results
|
||||
return [ListComponentResponse(**component) for component in results]
|
||||
|
||||
def download(self, api_key: str, component_id: str) -> DownloadComponentResponse:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue