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 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
@ -127,6 +126,8 @@ async def get_components(
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:
@ -187,10 +188,9 @@ async def download_component(
@router.get("/tags", response_model=List[TagResponse])
async def get_tags(
store_service: StoreService = Depends(get_store_service),
store_api_Key: str = Depends(get_optional_user_store_api_key),
):
try:
return await store_service.get_tags(store_api_Key)
return await store_service.get_tags()
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc))

View file

@ -4,8 +4,6 @@ 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,
@ -14,6 +12,7 @@ from langflow.services.store.schema import (
StoreComponentCreate,
)
from langflow.services.store.utils import process_tags_for_post
from loguru import logger
if TYPE_CHECKING:
from langflow.services.settings.service import SettingsService
@ -285,10 +284,10 @@ class StoreService(Service):
pass
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"
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
async def get_user_likes(self, api_key: str) -> List[Dict[str, Any]]: