Refactor store service and API router

Add error handling for unauthorized access to resources
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-16 10:55:26 -03:00
commit ce1a8d6835
2 changed files with 6 additions and 7 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
@ -127,6 +126,8 @@ async def get_components(
except HTTPStatusError as exc: except HTTPStatusError as exc:
if exc.response.status_code == 403: if exc.response.status_code == 403:
raise ValueError("You are not authorized to access this public resource") 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: try:
if result: if result:
if len(result) >= limit: if len(result) >= limit:
@ -187,10 +188,9 @@ async def download_component(
@router.get("/tags", response_model=List[TagResponse]) @router.get("/tags", response_model=List[TagResponse])
async def get_tags( async def get_tags(
store_service: StoreService = Depends(get_store_service), store_service: StoreService = Depends(get_store_service),
store_api_Key: str = Depends(get_optional_user_store_api_key),
): ):
try: try:
return await store_service.get_tags(store_api_Key) return await store_service.get_tags()
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) raise HTTPException(status_code=500, detail=str(exc))

View file

@ -4,8 +4,6 @@ from uuid import UUID
import httpx import httpx
from httpx import HTTPError, HTTPStatusError from httpx import HTTPError, HTTPStatusError
from loguru import logger
from langflow.services.base import Service from langflow.services.base import Service
from langflow.services.store.schema import ( from langflow.services.store.schema import (
CreateComponentResponse, CreateComponentResponse,
@ -14,6 +12,7 @@ from langflow.services.store.schema import (
StoreComponentCreate, StoreComponentCreate,
) )
from langflow.services.store.utils import process_tags_for_post from langflow.services.store.utils import process_tags_for_post
from loguru import logger
if TYPE_CHECKING: if TYPE_CHECKING:
from langflow.services.settings.service import SettingsService from langflow.services.settings.service import SettingsService
@ -285,10 +284,10 @@ class StoreService(Service):
pass pass
raise ValueError(f"Upload failed: {exc}") raise ValueError(f"Upload failed: {exc}")
async def get_tags(self, api_key: str) -> List[Dict[str, Any]]: async def get_tags(self) -> List[Dict[str, Any]]:
url = f"{self.base_url}/items/tags" url = f"{self.base_url}/items/tags"
params = {"fields": ",".join(["id", "name"])} params = {"fields": ",".join(["id", "name"])}
tags = await self._get(url, api_key, params) tags = await self._get(url, api_key=None, params=params)
return tags return tags
async def get_user_likes(self, api_key: str) -> List[Dict[str, Any]]: async def get_user_likes(self, api_key: str) -> List[Dict[str, Any]]: