Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 8, 2024
1 parent 8faf8ef commit 26ee5aa
Show file tree
Hide file tree
Showing 6 changed files with 459 additions and 418 deletions.
164 changes: 83 additions & 81 deletions jupyverse_api/jupyverse_api/contents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ..app import App
from ..auth import Auth, User
from .models import Checkpoint, Content, CreateContent, SaveContent
from .models import Checkpoint, Content, CreateContent, RenameContent, SaveContent


class FileIdManager(ABC):
Expand All @@ -32,83 +32,7 @@ def unwatch(self, path: str, watcher):
...


class Contents(Router, ABC):
def __init__(self, app: App, auth: Auth):
super().__init__(app=app)

router = APIRouter()

@router.post(
"/api/contents/{path:path}/checkpoints",
status_code=201,
)
async def create_checkpoint(
path, user: User = Depends(auth.current_user(permissions={"contents": ["write"]}))
) -> Checkpoint:
return await self.create_checkpoint(path, user)

@router.post(
"/api/contents{path:path}",
status_code=201,
)
async def create_content(
path: Optional[str],
request: Request,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
create_content = CreateContent(**(await request.json()))
return await self.create_content(path, create_content, user)

@router.get("/api/contents")
async def get_root_content(
content: int,
user: User = Depends(auth.current_user(permissions={"contents": ["read"]})),
) -> Content:
return await self.get_root_content(content, user)

@router.get("/api/contents/{path:path}/checkpoints")
async def get_checkpoint(
path, user: User = Depends(auth.current_user(permissions={"contents": ["read"]}))
) -> List[Checkpoint]:
return await self.get_checkpoint(path, user)

@router.get("/api/contents/{path:path}")
async def get_content(
path: str,
content: int = 0,
user: User = Depends(auth.current_user(permissions={"contents": ["read"]})),
) -> Content:
return await self.get_content(path, content, user)

@router.put("/api/contents/{path:path}")
async def save_content(
path,
request: Request,
response: Response,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
return await self.save_content(path, request, response, user)

@router.delete(
"/api/contents/{path:path}",
status_code=204,
)
async def delete_content(
path,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
):
return await self.delete_content(path, user)

@router.patch("/api/contents/{path:path}")
async def rename_content(
path,
request: Request,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
return await self.rename_content(path, request, user)

self.include_router(router)

class Contents(ABC):
@property
@abstractmethod
def file_id_manager(self) -> FileIdManager:
Expand Down Expand Up @@ -184,8 +108,7 @@ async def get_content(
async def save_content(
self,
path,
request: Request,
response: Response,
content: SaveContent,
user: User,
) -> Content:
...
Expand All @@ -202,7 +125,86 @@ async def delete_content(
async def rename_content(
self,
path,
request: Request,
rename_content: RenameContent,
user: User,
) -> Content:
...


class HTTPContents(Router, Contents, ABC):
def __init__(self, app: App, auth: Auth):
super().__init__(app=app)

router = APIRouter()

@router.post(
"/api/contents/{path:path}/checkpoints",
status_code=201,
)
async def create_checkpoint(
path, user: User = Depends(auth.current_user(permissions={"contents": ["write"]}))
) -> Checkpoint:
return await self.create_checkpoint(path, user)

@router.post(
"/api/contents{path:path}",
status_code=201,
)
async def create_content(
path: Optional[str],
request: Request,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
create_content = CreateContent(**(await request.json()))
return await self.create_content(path, create_content, user)

@router.get("/api/contents")
async def get_root_content(
content: int,
user: User = Depends(auth.current_user(permissions={"contents": ["read"]})),
) -> Content:
return await self.get_root_content(content, user)

@router.get("/api/contents/{path:path}/checkpoints")
async def get_checkpoint(
path, user: User = Depends(auth.current_user(permissions={"contents": ["read"]}))
) -> List[Checkpoint]:
return await self.get_checkpoint(path, user)

@router.get("/api/contents/{path:path}")
async def get_content(
path: str,
content: int = 0,
user: User = Depends(auth.current_user(permissions={"contents": ["read"]})),
) -> Content:
return await self.get_content(path, content, user)

@router.put("/api/contents/{path:path}")
async def save_content(
path,
request: Request,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
content = SaveContent(**(await request.json()))
return await self.save_content(path, content, user)

@router.delete(
"/api/contents/{path:path}",
status_code=204,
)
async def delete_content(
path,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
):
return await self.delete_content(path, user)

@router.patch("/api/contents/{path:path}")
async def rename_content(
path,
request: Request,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
rename_content = RenameContent(**(await request.json()))
return await self.rename_content(path, rename_content, user)

self.include_router(router)
Loading

0 comments on commit 26ee5aa

Please sign in to comment.