🐛 fix(store.py): add use_api_key parameter to count_components method to correctly handle API key usage

🐛 fix(store.py): fix incorrect type hint for get_liked_by_user_components method
🐛 fix(service.py): fix incorrect method name in upload method
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-16 12:13:04 -03:00
commit 0d8ad42ea0
2 changed files with 11 additions and 8 deletions

View file

@ -4,7 +4,6 @@ from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, Query from fastapi import APIRouter, Depends, HTTPException, Query
from httpx import HTTPStatusError from httpx import HTTPStatusError
from langflow.services.auth import utils as auth_utils from langflow.services.auth import utils as auth_utils
from langflow.services.database.models.user.user import User from langflow.services.database.models.user.user import User
from langflow.services.deps import get_settings_service, get_store_service 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( comp_count = await store_service.count_components(
api_key=store_api_Key, api_key=store_api_Key,
filter_conditions=filter_conditions, filter_conditions=filter_conditions,
use_api_key=liked or filter_by_user,
) )
else: else:
comp_count = len(result) comp_count = len(result)

View file

@ -117,11 +117,14 @@ class StoreService(Service):
self, self,
filter_conditions: List[Dict[str, Any]], filter_conditions: List[Dict[str, Any]],
api_key: Optional[str] = None, api_key: Optional[str] = None,
use_api_key: Optional[bool] = False,
) -> int: ) -> int:
params = {"aggregate": json.dumps({"count": "*"})} params = {"aggregate": json.dumps({"count": "*"})}
if filter_conditions: if filter_conditions:
params["filter"] = json.dumps({"_and": 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) results = await self._get(self.components_url, api_key, params)
return int(results[0].get("count", 0)) return int(results[0].get("count", 0))
@ -209,11 +212,11 @@ class StoreService(Service):
if filter_conditions: if filter_conditions:
params["filter"] = json.dumps({"_and": filter_conditions}) params["filter"] = json.dumps({"_and": filter_conditions})
if not use_api_key:
# If not liked, this means we are getting public components # If not liked, this means we are getting public components
# so we don't need to risk passing an invalid api_key # so we don't need to risk passing an invalid api_key
# and getting 401 # and getting 401
api_key = None api_key = api_key if use_api_key else None
results = await self._get(self.components_url, api_key, params) results = await self._get(self.components_url, api_key, params)
if isinstance(results, dict): if isinstance(results, dict):
results = [results] results = [results]
@ -224,7 +227,7 @@ class StoreService(Service):
# component.tags = [tags_id.tags_id for tags_id in component.tags] # component.tags = [tags_id.tags_id for tags_id in component.tags]
return results_objects 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 # Get fields id
# filter should be "id is in component_ids AND liked_by directus_users_id token is api_key" # filter should be "id is in component_ids AND liked_by directus_users_id token is api_key"
# return the ids # return the ids
@ -275,7 +278,7 @@ class StoreService(Service):
async def upload(self, api_key: str, component_data: StoreComponentCreate) -> CreateComponentResponse: async def upload(self, api_key: str, component_data: StoreComponentCreate) -> CreateComponentResponse:
headers = {"Authorization": f"Bearer {api_key}"} 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 # Parent is a UUID, but the store expects a string
response = None response = None
if component_dict.get("parent"): if component_dict.get("parent"):