Skip to content

Commit

Permalink
Add YDrive
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 11, 2024
1 parent 5e7a982 commit 4fe6bb2
Show file tree
Hide file tree
Showing 13 changed files with 972 additions and 384 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ jobs:
python3 -m pip install --upgrade pip
python3 -m pip install hatch
- name: Create jupyterlab-auth dev environment
run: hatch env create dev.jupyterlab-auth
- name: Create jupyterlab-auth and jupyterlab-noauth dev environments
run: |
hatch env create dev.jupyterlab-auth
hatch env create dev.jupyterlab-noauth
- name: Run tests
run: hatch run dev.jupyterlab-auth:test
run: |
hatch run dev.jupyterlab-noauth:pytest plugins/yjs/tests -v --color=yes
hatch run dev.jupyterlab-auth:test
207 changes: 124 additions & 83 deletions jupyverse_api/jupyverse_api/contents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import asyncio
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union

from fastapi import APIRouter, Depends, Request, Response
from fastapi import APIRouter, Depends, Request

from jupyverse_api import Router

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


class FileIdManager(ABC):
stop_watching_files: asyncio.Event
stopped_watching_files: asyncio.Event
Change: Any

@abstractmethod
async def get_path(self, file_id: str) -> str:
Expand All @@ -31,82 +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:
return await self.create_content(path, request, 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 All @@ -130,15 +56,45 @@ async def create_checkpoint(
) -> Checkpoint:
...

@abstractmethod
async def copy_content(
self,
from_path: str,
to_path: str,
) -> None:
...

@abstractmethod
async def move_content(
self,
from_path: str,
to_path: str,
) -> None:
...

@abstractmethod
async def create_content(
self,
path: Optional[str],
request: Request,
create_content: Union[CreateContent, CopyContent],
user: User,
) -> Content:
...

@abstractmethod
async def create_file(
self,
path: str,
) -> None:
...

@abstractmethod
async def create_directory(
self,
path: str,
) -> None:
...

@abstractmethod
async def get_root_content(
self,
Expand Down Expand Up @@ -168,8 +124,7 @@ async def get_content(
async def save_content(
self,
path,
request: Request,
response: Response,
content: SaveContent,
user: User,
) -> Content:
...
Expand All @@ -186,7 +141,93 @@ async def delete_content(
async def rename_content(
self,
path,
request: Request,
rename_content: RenameContent,
user: User,
) -> Content:
...


class HTTPContents(Router, ABC):
contents: Contents

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.contents.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:
r = await request.json()
create_content: Union[CreateContent, CopyContent]
try:
create_content = CreateContent(**r)
except Exception:
create_content = CopyContent(**r)
return await self.contents.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.contents.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.contents.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.contents.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.contents.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.contents.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.contents.rename_content(path, rename_content, user)

self.include_router(router)
4 changes: 4 additions & 0 deletions jupyverse_api/jupyverse_api/contents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class CreateContent(BaseModel):
type: str


class CopyContent(BaseModel):
copy_from: str


class SaveContent(BaseModel):
content: Optional[Union[str, Dict]] = None
format: str
Expand Down
Loading

0 comments on commit 4fe6bb2

Please sign in to comment.