From 50ededfd3e0a8e825fac3443e41ac641a0a55124 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 16 Nov 2023 15:14:53 -0300 Subject: [PATCH] Refactor store service and list components endpoint --- src/backend/langflow/api/v1/store.py | 78 ++++--------------- .../langflow/services/store/service.py | 76 +++++++++++++++++- 2 files changed, 88 insertions(+), 66 deletions(-) diff --git a/src/backend/langflow/api/v1/store.py b/src/backend/langflow/api/v1/store.py index 429b7736f..65f004fa5 100644 --- a/src/backend/langflow/api/v1/store.py +++ b/src/backend/langflow/api/v1/store.py @@ -1,24 +1,22 @@ import warnings -from typing import Annotated, Any, Dict, List, Optional, Union +from typing import Annotated, List, Optional, Union 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 from langflow.services.store.schema import ( CreateComponentResponse, DownloadComponentResponse, - ListComponentResponse, ListComponentResponseModel, StoreComponentCreate, TagResponse, UsersLikesResponse, ) -from langflow.services.store.service import StoreService, user_data_context -from langflow.services.store.utils import get_lf_version_from_pypi, update_components_with_user_data +from langflow.services.store.service import StoreService +from langflow.services.store.utils import get_lf_version_from_pypi router = APIRouter(prefix="/store", tags=["Components Store"]) @@ -110,64 +108,18 @@ async def get_components( store_api_Key: Optional[str] = Depends(get_optional_user_store_api_key), ): try: - async with user_data_context(api_key=store_api_Key, store_service=store_service): - filter_conditions: List[Dict[str, Any]] = store_service.build_filter_conditions( - search=search, - status=status, - tags=tags, - is_component=is_component, - filter_by_user=filter_by_user, - liked=liked, - store_api_Key=store_api_Key, - ) - - result: List[ListComponentResponse] = [] - authorized = False - try: - result = await store_service.query_components( - api_key=store_api_Key, - page=page, - limit=limit, - sort=sort, - filter_conditions=filter_conditions, - use_api_key=liked or filter_by_user, - ) - except HTTPStatusError as exc: - if exc.response.status_code == 403: - raise ValueError("You are not authorized to access this public resource") - elif exc.response.status_code == 401: - raise ValueError("You are not authorized to access this resource. Please check your API key.") - try: - if result: - if len(result) >= limit: - 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) - else: - comp_count = 0 - except HTTPStatusError as exc: - if exc.response.status_code == 403: - raise ValueError("You are not authorized to access this public resource") - elif exc.response.status_code == 401: - raise ValueError("You are not authorized to access this resource. Please check your API key.") - - if store_api_Key and result: - # Now, from the result, we need to get the components - # the user likes and set the liked_by_user to True - try: - updated_result = await update_components_with_user_data( - result, store_service, store_api_Key, liked=liked - ) - authorized = True - result = updated_result - except Exception: - # If we get an error here, it means the user is not authorized - authorized = False - return ListComponentResponseModel(results=result, authorized=authorized, count=comp_count) + return await store_service.get_list_component_response_model( + search=search, + status=status, + is_component=is_component, + tags=tags, + sort=sort, + liked=liked, + filter_by_user=filter_by_user, + page=page, + limit=limit, + store_api_Key=store_api_Key, + ) except Exception as exc: if isinstance(exc, HTTPStatusError): if exc.response.status_code == 403: diff --git a/src/backend/langflow/services/store/service.py b/src/backend/langflow/services/store/service.py index 2d4ae8582..bc7af5ce0 100644 --- a/src/backend/langflow/services/store/service.py +++ b/src/backend/langflow/services/store/service.py @@ -4,16 +4,16 @@ from uuid import UUID import httpx from httpx import HTTPError, HTTPStatusError -from loguru import logger - from langflow.services.base import Service from langflow.services.store.schema import ( CreateComponentResponse, DownloadComponentResponse, ListComponentResponse, + ListComponentResponseModel, StoreComponentCreate, ) -from langflow.services.store.utils import process_tags_for_post +from langflow.services.store.utils import process_tags_for_post, update_components_with_user_data +from loguru import logger if TYPE_CHECKING: from langflow.services.settings.service import SettingsService @@ -368,3 +368,73 @@ class StoreService(Service): raise ValueError(f"Unexpected result: {result}") else: raise ValueError(f"Unexpected status code: {response.status_code}") + + async def get_list_component_response_model( + self, + search: Optional[str] = None, + status: Optional[str] = None, + tags: Optional[List[str]] = None, + is_component: Optional[bool] = None, + filter_by_user: Optional[bool] = False, + liked: Optional[bool] = False, + store_api_Key: Optional[str] = None, + sort: Optional[List[str]] = None, + page: int = 1, + limit: int = 15, + ): + async with user_data_context(api_key=store_api_Key, store_service=self): + filter_conditions: List[Dict[str, Any]] = self.build_filter_conditions( + search=search, + status=status, + tags=tags, + is_component=is_component, + filter_by_user=filter_by_user, + liked=liked, + store_api_Key=store_api_Key, + ) + + result: List[ListComponentResponse] = [] + authorized = False + try: + result = await self.query_components( + api_key=store_api_Key, + page=page, + limit=limit, + sort=sort, + filter_conditions=filter_conditions, + use_api_key=liked or filter_by_user, + ) + except HTTPStatusError as exc: + if exc.response.status_code == 403: + raise ValueError("You are not authorized to access this public resource") + elif exc.response.status_code == 401: + raise ValueError("You are not authorized to access this resource. Please check your API key.") + try: + if result: + if len(result) >= limit: + comp_count = await self.count_components( + api_key=store_api_Key, + filter_conditions=filter_conditions, + use_api_key=liked or filter_by_user, + ) + else: + comp_count = len(result) + else: + comp_count = 0 + except HTTPStatusError as exc: + if exc.response.status_code == 403: + raise ValueError("You are not authorized to access this public resource") + elif exc.response.status_code == 401: + raise ValueError("You are not authorized to access this resource. Please check your API key.") + + if store_api_Key and result: + # Now, from the result, we need to get the components + # the user likes and set the liked_by_user to True + try: + updated_result = await update_components_with_user_data(result, self, store_api_Key, liked=liked) + authorized = True + result = updated_result + except Exception: + # If we get an error here, it means the user is not authorized + authorized = False + return ListComponentResponseModel(results=result, authorized=authorized, count=comp_count)