From 0d8ad42ea0adf56fdbdf366a7b788c9dc072cd1c Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 16 Nov 2023 12:13:04 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(store.py):=20add=20use=5Fapi?= =?UTF-8?q?=5Fkey=20parameter=20to=20count=5Fcomponents=20method=20to=20co?= =?UTF-8?q?rrectly=20handle=20API=20key=20usage=20=F0=9F=90=9B=20fix(store?= =?UTF-8?q?.py):=20fix=20incorrect=20type=20hint=20for=20get=5Fliked=5Fby?= =?UTF-8?q?=5Fuser=5Fcomponents=20method=20=F0=9F=90=9B=20fix(service.py):?= =?UTF-8?q?=20fix=20incorrect=20method=20name=20in=20upload=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/store.py | 2 +- src/backend/langflow/services/store/service.py | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/backend/langflow/api/v1/store.py b/src/backend/langflow/api/v1/store.py index 5b79ff2f6..19cabe3df 100644 --- a/src/backend/langflow/api/v1/store.py +++ b/src/backend/langflow/api/v1/store.py @@ -4,7 +4,6 @@ from uuid import UUID from fastapi import APIRouter, Depends, HTTPException, Query from httpx import HTTPStatusError - from langflow.services.auth import utils as auth_utils from langflow.services.database.models.user.user import User from langflow.services.deps import get_settings_service, get_store_service @@ -141,6 +140,7 @@ async def get_components( comp_count = await store_service.count_components( api_key=store_api_Key, filter_conditions=filter_conditions, + use_api_key=liked or filter_by_user, ) else: comp_count = len(result) diff --git a/src/backend/langflow/services/store/service.py b/src/backend/langflow/services/store/service.py index 1128af0c8..66fd0895d 100644 --- a/src/backend/langflow/services/store/service.py +++ b/src/backend/langflow/services/store/service.py @@ -117,11 +117,14 @@ class StoreService(Service): self, filter_conditions: List[Dict[str, Any]], api_key: Optional[str] = None, + use_api_key: Optional[bool] = False, ) -> int: params = {"aggregate": json.dumps({"count": "*"})} if filter_conditions: params["filter"] = json.dumps({"_and": filter_conditions}) + api_key = api_key if use_api_key else None + results = await self._get(self.components_url, api_key, params) return int(results[0].get("count", 0)) @@ -209,11 +212,11 @@ class StoreService(Service): if filter_conditions: params["filter"] = json.dumps({"_and": filter_conditions}) - if not use_api_key: - # If not liked, this means we are getting public components - # so we don't need to risk passing an invalid api_key - # and getting 401 - api_key = None + + # If not liked, this means we are getting public components + # so we don't need to risk passing an invalid api_key + # and getting 401 + api_key = api_key if use_api_key else None results = await self._get(self.components_url, api_key, params) if isinstance(results, dict): results = [results] @@ -224,7 +227,7 @@ class StoreService(Service): # component.tags = [tags_id.tags_id for tags_id in component.tags] return results_objects - async def get_liked_by_user_components(self, component_ids: List[UUID], api_key: str) -> List[UUID]: + async def get_liked_by_user_components(self, component_ids: List[UUID], api_key: str) -> List[str]: # Get fields id # filter should be "id is in component_ids AND liked_by directus_users_id token is api_key" # return the ids @@ -275,7 +278,7 @@ class StoreService(Service): async def upload(self, api_key: str, component_data: StoreComponentCreate) -> CreateComponentResponse: headers = {"Authorization": f"Bearer {api_key}"} - component_dict = component_data.dict(exclude_unset=True) + component_dict = component_data.model_dump(exclude_unset=True) # Parent is a UUID, but the store expects a string response = None if component_dict.get("parent"):