🔧 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:
Gabriel Luiz Freitas Almeida 2023-10-25 21:47:39 -03:00
commit c9b77ec9db
2 changed files with 39 additions and 10 deletions

View file

@ -66,14 +66,35 @@ def list_components(
): ):
try: try:
fields = ["id", "name", "description", "user_created.name", "is_component"] fields = ["id", "name", "description", "user_created.name", "is_component"]
result = store_service.list_components( result = store_service.query_components(
store_api_Key, page, limit, fields=fields, filter_by_user=filter_by_user store_api_Key,
page,
limit,
fields=fields,
filter_by_user=filter_by_user,
) )
return result return result
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(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) @router.get("/components/{component_id}", response_model=DownloadComponentResponse)
def read_component( def read_component(
component_id: UUID, component_id: UUID,

View file

@ -2,7 +2,7 @@ from datetime import datetime
import json import json
from uuid import UUID from uuid import UUID
from langflow.services.base import Service 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 import httpx
from httpx import HTTPError from httpx import HTTPError
@ -119,21 +119,27 @@ class StoreService(Service):
results = self._get(self.components_url, api_key, params) results = self._get(self.components_url, api_key, params)
return [ComponentResponse(**component) for component in results] return [ComponentResponse(**component) for component in results]
def list_components( def query_components(
self, self,
api_key: str, api_key: str,
page: int = 1, page: int = 1,
limit: int = 15, limit: int = 15,
fields: Optional[List[str]] = None, fields: Optional[List[str]] = None,
filter_by_user: bool = False, filter_by_user: bool = False,
) -> List[ListComponentResponse]: count: bool = False,
) -> Union[List[ListComponentResponse], List[Dict[str, int]]]:
params = {"page": page, "limit": limit} params = {"page": page, "limit": limit}
# ?aggregate[count]=likes # ?aggregate[count]=likes
params["fields"] = ( if count:
",".join(fields) params["aggregate"] = json.dumps({"count": "*"})
if fields else:
else ",".join(["id", "name", "description", "count(likes)", "is_component"]) 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 # Only public components or the ones created by the user
# check for "public" or "Public" # check for "public" or "Public"
@ -151,6 +157,8 @@ class StoreService(Service):
) )
results = self._get(self.components_url, api_key, params) results = self._get(self.components_url, api_key, params)
if "count" in results[0]:
return results
return [ListComponentResponse(**component) for component in results] return [ListComponentResponse(**component) for component in results]
def download(self, api_key: str, component_id: str) -> DownloadComponentResponse: def download(self, api_key: str, component_id: str) -> DownloadComponentResponse: