🐛 fix(store.py): change check_if_store_has_api_key function to be async and handle exceptions properly
✨ feat(store.py): add support for checking if store has a valid API key using the StoreService 🐛 fix(service.py): remove unused import and fix formatting ✨ feat(service.py): add check_api_key method to StoreService to check if an API key is valid
This commit is contained in:
parent
fa68c4e678
commit
159f825a2b
2 changed files with 30 additions and 7 deletions
|
|
@ -51,12 +51,19 @@ def check_if_store_is_enabled(
|
||||||
|
|
||||||
|
|
||||||
@router.get("/check/api_key")
|
@router.get("/check/api_key")
|
||||||
def check_if_store_has_api_key(
|
async def check_if_store_has_api_key(
|
||||||
api_key=Depends(get_optional_user_store_api_key),
|
api_key: Optional[str] = Depends(get_optional_user_store_api_key),
|
||||||
|
store_service: StoreService = Depends(get_store_service),
|
||||||
):
|
):
|
||||||
return {
|
if api_key is None:
|
||||||
"has_api_key": api_key is not None,
|
return {"has_api_key": False}
|
||||||
}
|
|
||||||
|
try:
|
||||||
|
is_valid = await store_service.check_api_key(api_key)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
|
|
||||||
|
return {"has_api_key": is_valid}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/components/", response_model=CreateComponentResponse, status_code=201)
|
@router.post("/components/", response_model=CreateComponentResponse, status_code=201)
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
@ -15,6 +13,7 @@ from langflow.services.store.schema import (
|
||||||
StoreComponentCreate,
|
StoreComponentCreate,
|
||||||
)
|
)
|
||||||
from langflow.services.store.utils import process_tags_for_post, update_components_with_user_data
|
from langflow.services.store.utils import process_tags_for_post, update_components_with_user_data
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from langflow.services.settings.service import SettingsService
|
from langflow.services.settings.service import SettingsService
|
||||||
|
|
@ -78,6 +77,23 @@ class StoreService(Service):
|
||||||
# will make a property return that data
|
# will make a property return that data
|
||||||
# Without making the request multiple times
|
# Without making the request multiple times
|
||||||
|
|
||||||
|
async def check_api_key(self, api_key: str):
|
||||||
|
# Check if the api key is valid
|
||||||
|
# If it is, return True
|
||||||
|
# If it is not, return False
|
||||||
|
try:
|
||||||
|
user_data, _ = await self._get(f"{self.base_url}/users/me", api_key, params={"fields": "id"})
|
||||||
|
if isinstance(user_data, list):
|
||||||
|
user_data = user_data[0]
|
||||||
|
return "id" in user_data
|
||||||
|
except HTTPStatusError as exc:
|
||||||
|
if exc.response.status_code in [403, 401]:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unexpected status code: {exc.response.status_code}")
|
||||||
|
except Exception as exc:
|
||||||
|
raise ValueError(f"Unexpected error: {exc}")
|
||||||
|
|
||||||
async def _get(
|
async def _get(
|
||||||
self, url: str, api_key: Optional[str] = None, params: Optional[Dict[str, Any]] = None
|
self, url: str, api_key: Optional[str] = None, params: Optional[Dict[str, Any]] = None
|
||||||
) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
|
) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue