Skip to content

Commit

Permalink
Merge pull request #246 from pepkit/favorites_api
Browse files Browse the repository at this point in the history
stars api
  • Loading branch information
nleroy917 authored Dec 14, 2023
2 parents 3b8ac74 + fd82c09 commit 3b3d16b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
92 changes: 90 additions & 2 deletions pephub/routers/api/v1/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pepdbagent import PEPDatabaseAgent
from pepdbagent.exceptions import ProjectUniqueNameError
from pepdbagent.const import DEFAULT_LIMIT_INFO
from pepdbagent.models import ListOfNamespaceInfo, Namespace
from pepdbagent.models import ListOfNamespaceInfo, Namespace, AnnotationList
from typing import Literal
from typing_extensions import Annotated

Expand All @@ -32,7 +32,7 @@
BLANK_PEP_SAMPLE_TABLE,
DEFAULT_PEP_SCHEMA,
)
from ...models import ProjectRawModel, ProjectJsonRequest
from ...models import ProjectRawModel, ProjectJsonRequest, FavoriteRequest

from dotenv import load_dotenv
from .base import api
Expand Down Expand Up @@ -327,6 +327,94 @@ async def upload_raw_pep(
)


# favorites endpoints
@namespace.get(
"/stars",
summary="Get information about user favorite projects.",
dependencies=[Depends(verify_user_can_write_namespace)],
response_model=AnnotationList,
)
async def get_user_stars(
namespace: str,
agent: PEPDatabaseAgent = Depends(get_db),
):
"""
Get information about user favorite projects.
"""
return agent.user.get_favorites(namespace=namespace)


@namespace.post(
"/stars",
summary="Add project to favorites.",
dependencies=[
Depends(verify_user_can_write_namespace),
],
)
async def add_to_stars(
project: FavoriteRequest,
namespace: str,
agent: PEPDatabaseAgent = Depends(get_db),
):
"""
Add project to favorites
"""
try:
agent.user.add_to_favorites(
namespace=namespace,
project_namespace=project.namespace,
project_name=project.name,
project_tag=project.tag,
)
return JSONResponse(
content={
"namespace": namespace,
"registry_path": f"{project.namespace}/{project.name}:{project.tag}",
"message": "PEP was added to favorites.",
},
status_code=202,
)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Could not add PEP to favorites. Server error: {e}",
)


@namespace.delete(
"/stars",
summary="Delete project from favorites.",
dependencies=[Depends(verify_user_can_write_namespace)],
)
async def remove_from_stars(
project: FavoriteRequest,
namespace: str,
agent: PEPDatabaseAgent = Depends(get_db),
):
"""
Add project to favorites
"""
try:
agent.user.remove_from_favorites(
namespace=namespace,
project_namespace=project.namespace,
project_name=project.name,
project_tag=project.tag,
)
return JSONResponse(
content={
"message": "PEP was removed from favorites.",
"registry": f"{project.namespace}/{project.name}:{project.tag}",
},
status_code=202,
)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Could remove pep from favorites. Server error: {e}",
)


@api.get(
"/namespace/info",
summary="Get information list of biggest namespaces",
Expand Down
6 changes: 6 additions & 0 deletions pephub/routers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class ForkRequest(BaseModel):
fork_description: Optional[str] = None


class FavoriteRequest(BaseModel):
namespace: str
name: str
tag: Optional[str] = DEFAULT_TAG


class InitializeDeviceCodeResponse(BaseModel):
device_code: str
auth_url: str
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ python-multipart>=0.0.5
tqdm
uvicorn
python-dotenv
# pepdbagent>=0.6.0
pepdbagent>=0.6.0a2
pepdbagent @ git+https://github.com/pepkit/pepdbagent@dev#egg=pepdbagent
peppy>=0.40.0a5
qdrant-client
Expand Down

0 comments on commit 3b3d16b

Please sign in to comment.