Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
Merge branch 'feat/move-api-v0-functionality-to-v1' into feat/api-v1-…
Browse files Browse the repository at this point in the history
…get-user-endpoint
  • Loading branch information
jfcalvo committed May 13, 2024
2 parents 79367ae + 630ca2c commit 1d60c2c
Show file tree
Hide file tree
Showing 31 changed files with 969 additions and 62 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ These are the section headers that we use:
- Added `POST /api/v1/token` endpoint to generate a new API token for a user. ([#138](https://github.com/argilla-io/argilla-server/pull/138))
- Added `GET /api/v1/me` endpoint to get the current user information. ([#140](https://github.com/argilla-io/argilla-server/pull/140))
- Added `GET /api/v1/users` endpoint to get a list of all users. ([#142](https://github.com/argilla-io/argilla-server/pull/142))
- Added `GET /api/v1/users/:user_id` endpoint to get a specific user. ([#166](https://github.com/argilla-io/argilla-server/pull/166))
- Added `POST /api/v1/users` endpoint to create a new user. ([#146](https://github.com/argilla-io/argilla-server/pull/146))
- Added `DELETE /api/v1/users` endpoint to delete a user. ([#148](https://github.com/argilla-io/argilla-server/pull/148))
- Added `GET /api/v1/users/:user_id` endpoint to get a specific user. ([#166](https://github.com/argilla-io/argilla-server/pull/166))
- Added `POST /api/v1/workspaces` endpoint to create a new workspace. ([#150](https://github.com/argilla-io/argilla-server/pull/150))
- Added `GET /api/v1/workspaces/:workspace_id/users` endpoint to get the users of a workspace. ([#153](https://github.com/argilla-io/argilla-server/pull/153))
- Added `POST /api/v1/workspaces/:workspace_id/users` endpoind to add a user to a workspace. ([#156](https://github.com/argilla-io/argilla-server/pull/156))
- Added `DELETE /api/v1/workspaces/:workspace_id/users/:user_id` endpoint to remove a user from a workspace. ([#158](https://github.com/argilla-io/argilla-server/pull/158))
- Added `GET /api/v1/version` endpoint to get the current Argilla version. ([#162](https://github.com/argilla-io/argilla-server/pull/162))
- Added `GET /api/v1/status` endpoint to get Argilla service status. ([#165](https://github.com/argilla-io/argilla-server/pull/165))

## [Unreleased]()

Expand Down
2 changes: 2 additions & 0 deletions src/argilla_server/apis/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from argilla_server.apis.v1.handlers import (
fields as fields_v1,
)
from argilla_server.apis.v1.handlers import info as info_v1
from argilla_server.apis.v1.handlers import (
metadata_properties as metadata_properties_v1,
)
Expand Down Expand Up @@ -114,6 +115,7 @@ def create_api_v1():
APIErrorHandler.configure_app(api_v1)

for router in [
info_v1.router,
authentication_v1.router,
datasets_v1.router,
fields_v1.router,
Expand Down
9 changes: 5 additions & 4 deletions src/argilla_server/apis/v0/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from argilla_server.contexts import accounts
from argilla_server.database import get_async_db
from argilla_server.errors import EntityAlreadyExistsError, EntityNotFoundError
from argilla_server.errors.future import NotUniqueError
from argilla_server.policies import UserPolicy, authorize
from argilla_server.pydantic_v1 import parse_obj_as
from argilla_server.schemas.v0.users import User, UserCreate
Expand Down Expand Up @@ -90,17 +91,17 @@ async def create_user(
):
await authorize(current_user, UserPolicy.create)

user = await accounts.get_user_by_username(db, user_create.username)
if user is not None:
raise EntityAlreadyExistsError(name=user_create.username, type=User)

try:
user = await accounts.create_user(db, user_create.dict(), user_create.workspaces)

telemetry.track_user_created(user)
except NotUniqueError:
raise EntityAlreadyExistsError(name=user_create.username, type=User)
except Exception as e:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(e))

await user.awaitable_attrs.workspaces

return User.from_orm(user)


Expand Down
17 changes: 8 additions & 9 deletions src/argilla_server/apis/v0/handlers/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
from argilla_server.contexts import accounts
from argilla_server.database import get_async_db
from argilla_server.errors import EntityAlreadyExistsError, EntityNotFoundError
from argilla_server.errors.future import NotUniqueError
from argilla_server.policies import WorkspacePolicy, WorkspaceUserPolicy, authorize
from argilla_server.pydantic_v1 import parse_obj_as
from argilla_server.schemas.v0.users import User
from argilla_server.schemas.v0.workspaces import Workspace, WorkspaceCreate, WorkspaceUserCreate
from argilla_server.schemas.v0.workspaces import Workspace, WorkspaceCreate
from argilla_server.security import auth

router = APIRouter(tags=["workspaces"])
Expand All @@ -39,11 +40,11 @@ async def create_workspace(
):
await authorize(current_user, WorkspacePolicy.create)

if await accounts.get_workspace_by_name(db, workspace_create.name):
try:
workspace = await accounts.create_workspace(db, workspace_create.dict())
except NotUniqueError:
raise EntityAlreadyExistsError(name=workspace_create.name, type=Workspace)

workspace = await accounts.create_workspace(db, workspace_create)

return Workspace.from_orm(workspace)


Expand Down Expand Up @@ -84,13 +85,11 @@ async def create_workspace_user(
if not user:
raise EntityNotFoundError(name=str(user_id), type=User)

workspace_user = await accounts.get_workspace_user_by_workspace_id_and_user_id(db, workspace_id, user_id)
if workspace_user is not None:
try:
workspace_user = await accounts.create_workspace_user(db, {"workspace_id": workspace_id, "user_id": user_id})
except NotUniqueError:
raise EntityAlreadyExistsError(name=str(user_id), type=User)

workspace_user = await accounts.create_workspace_user(
db, WorkspaceUserCreate(workspace_id=workspace_id, user_id=user_id)
)
await db.refresh(user, attribute_names=["workspaces"])

return User.from_orm(workspace_user.user)
Expand Down
4 changes: 2 additions & 2 deletions src/argilla_server/apis/v1/handlers/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from typing import Annotated

from fastapi import APIRouter, Depends, Form
from fastapi import APIRouter, Depends, Form, status
from sqlalchemy.ext.asyncio import AsyncSession

from argilla_server.contexts import accounts
Expand All @@ -25,7 +25,7 @@
router = APIRouter(tags=["Authentication"])


@router.post("/token", response_model=Token)
@router.post("/token", status_code=status.HTTP_201_CREATED, response_model=Token)
async def create_token(
*,
db: AsyncSession = Depends(get_async_db),
Expand Down
35 changes: 35 additions & 0 deletions src/argilla_server/apis/v1/handlers/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from fastapi import APIRouter, Depends

from argilla_server.contexts import info
from argilla_server.schemas.v1.info import Status, Version
from argilla_server.search_engine import SearchEngine, get_search_engine

router = APIRouter(tags=["info"])


@router.get("/version", response_model=Version)
async def get_version():
return Version(version=info.argilla_version())


@router.get("/status", response_model=Status)
async def get_status(search_engine: SearchEngine = Depends(get_search_engine)):
return Status(
version=info.argilla_version(),
search_engine=await search_engine.info(),
memory=info.memory_status(),
)
18 changes: 8 additions & 10 deletions src/argilla_server/apis/v1/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from argilla_server.contexts import accounts
from argilla_server.database import get_async_db
from argilla_server.errors import EntityAlreadyExistsError, EntityNotFoundError
from argilla_server.errors.future import NotUniqueError
from argilla_server.policies import UserPolicyV1, authorize
from argilla_server.schemas.v1.users import User, UserCreate, Users
from argilla_server.schemas.v1.workspaces import Workspaces
Expand Down Expand Up @@ -69,7 +70,7 @@ async def list_users(
return Users(items=users)


@router.post("/users", response_model=User)
@router.post("/users", status_code=status.HTTP_201_CREATED, response_model=User)
async def create_user(
*,
db: AsyncSession = Depends(get_async_db),
Expand All @@ -78,14 +79,12 @@ async def create_user(
):
await authorize(current_user, UserPolicyV1.create)

user = await accounts.get_user_by_username(db, user_create.username)
if user is not None:
raise EntityAlreadyExistsError(name=user_create.username, type=User)

try:
user = await accounts.create_user(db, user_create.dict())

telemetry.track_user_created(user)
except NotUniqueError as e:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e))
except Exception as e:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(e))

Expand All @@ -101,11 +100,10 @@ async def delete_user(
):
user = await accounts.get_user_by_id(db, user_id)
if user is None:
# TODO: Forcing here user_id to be an string.
# Not casting it is causing a `Object of type UUID is not JSON serializable`.
# Possible solution redefining JSONEncoder.default here:
# https://github.com/jazzband/django-push-notifications/issues/586
raise EntityNotFoundError(name=str(user_id), type=User)
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User with id `{user_id}` not found",
)

await authorize(current_user, UserPolicyV1.delete)

Expand Down
108 changes: 103 additions & 5 deletions src/argilla_server/apis/v1/handlers/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
from fastapi import APIRouter, Depends, HTTPException, Security, status
from sqlalchemy.ext.asyncio import AsyncSession

from argilla_server import models
from argilla_server.contexts import accounts, datasets
from argilla_server.database import get_async_db
from argilla_server.errors import EntityAlreadyExistsError
from argilla_server.errors.future import NotUniqueError
from argilla_server.models import User
from argilla_server.policies import WorkspacePolicyV1, authorize
from argilla_server.schemas.v1.workspaces import Workspace, Workspaces
from argilla_server.policies import WorkspacePolicyV1, WorkspaceUserPolicyV1, authorize
from argilla_server.schemas.v1.users import User, Users
from argilla_server.schemas.v1.workspaces import Workspace, WorkspaceCreate, Workspaces, WorkspaceUserCreate
from argilla_server.security import auth
from argilla_server.services.datasets import DatasetsService

Expand All @@ -33,7 +37,7 @@ async def get_workspace(
*,
db: AsyncSession = Depends(get_async_db),
workspace_id: UUID,
current_user: User = Security(auth.get_current_user),
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, WorkspacePolicyV1.get(workspace_id))

Expand All @@ -47,13 +51,30 @@ async def get_workspace(
return workspace


@router.post("/workspaces", status_code=status.HTTP_201_CREATED, response_model=Workspace)
async def create_workspace(
*,
db: AsyncSession = Depends(get_async_db),
workspace_create: WorkspaceCreate,
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, WorkspacePolicyV1.create)

try:
workspace = await accounts.create_workspace(db, workspace_create.dict())
except NotUniqueError as e:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e))

return workspace


@router.delete("/workspaces/{workspace_id}", response_model=Workspace)
async def delete_workspace(
*,
db: AsyncSession = Depends(get_async_db),
datasets_service: DatasetsService = Depends(DatasetsService.get_instance),
workspace_id: UUID,
current_user: User = Security(auth.get_current_user),
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, WorkspacePolicyV1.delete)

Expand Down Expand Up @@ -81,7 +102,9 @@ async def delete_workspace(

@router.get("/me/workspaces", response_model=Workspaces)
async def list_workspaces_me(
*, db: AsyncSession = Depends(get_async_db), current_user: User = Security(auth.get_current_user)
*,
db: AsyncSession = Depends(get_async_db),
current_user: models.User = Security(auth.get_current_user),
) -> Workspaces:
await authorize(current_user, WorkspacePolicyV1.list_workspaces_me)

Expand All @@ -91,3 +114,78 @@ async def list_workspaces_me(
workspaces = await accounts.list_workspaces_by_user_id(db, current_user.id)

return Workspaces(items=workspaces)


@router.get("/workspaces/{workspace_id}/users", response_model=Users)
async def list_workspace_users(
*,
db: AsyncSession = Depends(get_async_db),
workspace_id: UUID,
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, WorkspaceUserPolicyV1.list(workspace_id))

workspace = await accounts.get_workspace_by_id(db, workspace_id)
if workspace is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace with id `{workspace_id}` not found",
)

await workspace.awaitable_attrs.users

return Users(items=workspace.users)


@router.post("/workspaces/{workspace_id}/users", status_code=status.HTTP_201_CREATED, response_model=User)
async def create_workspace_user(
*,
db: AsyncSession = Depends(get_async_db),
workspace_id: UUID,
workspace_user_create: WorkspaceUserCreate,
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, WorkspaceUserPolicyV1.create)

workspace = await accounts.get_workspace_by_id(db, workspace_id)
if workspace is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace with id `{workspace_id}` not found",
)

user = await accounts.get_user_by_id(db, workspace_user_create.user_id)
if user is None:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"User with id `{workspace_user_create.user_id}` not found",
)

try:
workspace_user = await accounts.create_workspace_user(db, {"workspace_id": workspace.id, "user_id": user.id})
except NotUniqueError as e:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e))

return workspace_user.user


@router.delete("/workspaces/{workspace_id}/users/{user_id}", response_model=User)
async def delete_workspace_user(
*,
db: AsyncSession = Depends(get_async_db),
workspace_id: UUID,
user_id: UUID,
current_user: models.User = Security(auth.get_current_user),
):
workspace_user = await accounts.get_workspace_user_by_workspace_id_and_user_id(db, workspace_id, user_id)
if workspace_user is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User with id `{user_id}` not found in workspace with id `{workspace_id}`",
)

await authorize(current_user, WorkspaceUserPolicyV1.delete(workspace_user))

await accounts.delete_workspace_user(db, workspace_user)

return await workspace_user.awaitable_attrs.user
Loading

0 comments on commit 1d60c2c

Please sign in to comment.