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/migrate…
Browse files Browse the repository at this point in the history
…-info-v0-endpoint-to-version-v1
  • Loading branch information
jfcalvo committed May 13, 2024
2 parents d30a781 + 7945b9b commit 924e8d1
Show file tree
Hide file tree
Showing 23 changed files with 1,368 additions and 62 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ 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 `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 `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` endpoin to get the current Argilla version. ([#162](https://github.com/argilla-io/argilla-server/pull/162))

## [Unreleased]()
Expand Down
11 changes: 6 additions & 5 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)
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
60 changes: 58 additions & 2 deletions src/argilla_server/apis/v1/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException, Request, Security, status
Expand All @@ -20,9 +21,9 @@
from argilla_server import models, telemetry
from argilla_server.contexts import accounts
from argilla_server.database import get_async_db
from argilla_server.models import User
from argilla_server.errors.future import NotUniqueError
from argilla_server.policies import UserPolicyV1, authorize
from argilla_server.schemas.v1.users import User
from argilla_server.schemas.v1.users import User, UserCreate, Users
from argilla_server.schemas.v1.workspaces import Workspaces
from argilla_server.security import auth

Expand All @@ -36,6 +37,61 @@ async def get_current_user(request: Request, current_user: models.User = Securit
return current_user


@router.get("/users", response_model=Users)
async def list_users(
*,
db: AsyncSession = Depends(get_async_db),
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, UserPolicyV1.list)

users = await accounts.list_users(db)

return Users(items=users)


@router.post("/users", status_code=status.HTTP_201_CREATED, response_model=User)
async def create_user(
*,
db: AsyncSession = Depends(get_async_db),
user_create: UserCreate,
current_user: models.User = Security(auth.get_current_user),
):
await authorize(current_user, UserPolicyV1.create)

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))

return user


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

await authorize(current_user, UserPolicyV1.delete)

await accounts.delete_user(db, user)

return user


@router.get("/users/{user_id}/workspaces", response_model=Workspaces)
async def list_user_workspaces(
*,
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 924e8d1

Please sign in to comment.