11import asyncio
22from abc import ABC , abstractmethod
33from pathlib import Path
4- from typing import Dict , List , Optional , Union
4+ from typing import Any , Dict , List , Optional , Union
55
6- from fastapi import APIRouter , Depends , Request , Response
6+ from fastapi import APIRouter , Depends , Request
77
88from jupyverse_api import Router
99
1010from ..app import App
1111from ..auth import Auth , User
12- from .models import Checkpoint , Content , SaveContent
12+ from .models import Checkpoint , Content , CopyContent , CreateContent , RenameContent , SaveContent
1313
1414
1515class FileIdManager (ABC ):
1616 stop_watching_files : asyncio .Event
1717 stopped_watching_files : asyncio .Event
18+ Change : Any
1819
1920 @abstractmethod
2021 async def get_path (self , file_id : str ) -> str :
@@ -31,82 +32,7 @@ def unwatch(self, path: str, watcher):
3132 ...
3233
3334
34- class Contents (Router , ABC ):
35- def __init__ (self , app : App , auth : Auth ):
36- super ().__init__ (app = app )
37-
38- router = APIRouter ()
39-
40- @router .post (
41- "/api/contents/{path:path}/checkpoints" ,
42- status_code = 201 ,
43- )
44- async def create_checkpoint (
45- path , user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]}))
46- ) -> Checkpoint :
47- return await self .create_checkpoint (path , user )
48-
49- @router .post (
50- "/api/contents{path:path}" ,
51- status_code = 201 ,
52- )
53- async def create_content (
54- path : Optional [str ],
55- request : Request ,
56- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
57- ) -> Content :
58- return await self .create_content (path , request , user )
59-
60- @router .get ("/api/contents" )
61- async def get_root_content (
62- content : int ,
63- user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
64- ) -> Content :
65- return await self .get_root_content (content , user )
66-
67- @router .get ("/api/contents/{path:path}/checkpoints" )
68- async def get_checkpoint (
69- path , user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]}))
70- ) -> List [Checkpoint ]:
71- return await self .get_checkpoint (path , user )
72-
73- @router .get ("/api/contents/{path:path}" )
74- async def get_content (
75- path : str ,
76- content : int = 0 ,
77- user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
78- ) -> Content :
79- return await self .get_content (path , content , user )
80-
81- @router .put ("/api/contents/{path:path}" )
82- async def save_content (
83- path ,
84- request : Request ,
85- response : Response ,
86- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
87- ) -> Content :
88- return await self .save_content (path , request , response , user )
89-
90- @router .delete (
91- "/api/contents/{path:path}" ,
92- status_code = 204 ,
93- )
94- async def delete_content (
95- path ,
96- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
97- ):
98- return await self .delete_content (path , user )
99-
100- @router .patch ("/api/contents/{path:path}" )
101- async def rename_content (
102- path ,
103- request : Request ,
104- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
105- ) -> Content :
106- return await self .rename_content (path , request , user )
107-
108- self .include_router (router )
109-
35+ class Contents (ABC ):
11036 @property
11137 @abstractmethod
11238 def file_id_manager (self ) -> FileIdManager :
@@ -130,15 +56,45 @@ async def create_checkpoint(
13056 ) -> Checkpoint :
13157 ...
13258
59+ @abstractmethod
60+ async def copy_content (
61+ self ,
62+ from_path : str ,
63+ to_path : str ,
64+ ) -> None :
65+ ...
66+
67+ @abstractmethod
68+ async def move_content (
69+ self ,
70+ from_path : str ,
71+ to_path : str ,
72+ ) -> None :
73+ ...
74+
13375 @abstractmethod
13476 async def create_content (
13577 self ,
13678 path : Optional [str ],
137- request : Request ,
79+ create_content : Union [ CreateContent , CopyContent ] ,
13880 user : User ,
13981 ) -> Content :
14082 ...
14183
84+ @abstractmethod
85+ async def create_file (
86+ self ,
87+ path : str ,
88+ ) -> None :
89+ ...
90+
91+ @abstractmethod
92+ async def create_directory (
93+ self ,
94+ path : str ,
95+ ) -> None :
96+ ...
97+
14298 @abstractmethod
14399 async def get_root_content (
144100 self ,
@@ -168,8 +124,7 @@ async def get_content(
168124 async def save_content (
169125 self ,
170126 path ,
171- request : Request ,
172- response : Response ,
127+ content : SaveContent ,
173128 user : User ,
174129 ) -> Content :
175130 ...
@@ -186,7 +141,93 @@ async def delete_content(
186141 async def rename_content (
187142 self ,
188143 path ,
189- request : Request ,
144+ rename_content : RenameContent ,
190145 user : User ,
191146 ) -> Content :
192147 ...
148+
149+
150+ class HTTPContents (Router , ABC ):
151+ contents : Contents
152+
153+ def __init__ (self , app : App , auth : Auth ):
154+ super ().__init__ (app = app )
155+
156+ router = APIRouter ()
157+
158+ @router .post (
159+ "/api/contents/{path:path}/checkpoints" ,
160+ status_code = 201 ,
161+ )
162+ async def create_checkpoint (
163+ path , user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]}))
164+ ) -> Checkpoint :
165+ return await self .contents .create_checkpoint (path , user )
166+
167+ @router .post (
168+ "/api/contents{path:path}" ,
169+ status_code = 201 ,
170+ )
171+ async def create_content (
172+ path : Optional [str ],
173+ request : Request ,
174+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
175+ ) -> Content :
176+ r = await request .json ()
177+ create_content : Union [CreateContent , CopyContent ]
178+ try :
179+ create_content = CreateContent (** r )
180+ except Exception :
181+ create_content = CopyContent (** r )
182+ return await self .contents .create_content (path , create_content , user )
183+
184+ @router .get ("/api/contents" )
185+ async def get_root_content (
186+ content : int ,
187+ user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
188+ ) -> Content :
189+ return await self .contents .get_root_content (content , user )
190+
191+ @router .get ("/api/contents/{path:path}/checkpoints" )
192+ async def get_checkpoint (
193+ path , user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]}))
194+ ) -> List [Checkpoint ]:
195+ return await self .contents .get_checkpoint (path , user )
196+
197+ @router .get ("/api/contents/{path:path}" )
198+ async def get_content (
199+ path : str ,
200+ content : int = 0 ,
201+ user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
202+ ) -> Content :
203+ return await self .contents .get_content (path , content , user )
204+
205+ @router .put ("/api/contents/{path:path}" )
206+ async def save_content (
207+ path ,
208+ request : Request ,
209+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
210+ ) -> Content :
211+ content = SaveContent (** (await request .json ()))
212+ return await self .contents .save_content (path , content , user )
213+
214+ @router .delete (
215+ "/api/contents/{path:path}" ,
216+ status_code = 204 ,
217+ )
218+ async def delete_content (
219+ path ,
220+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
221+ ):
222+ return await self .contents .delete_content (path , user )
223+
224+ @router .patch ("/api/contents/{path:path}" )
225+ async def rename_content (
226+ path ,
227+ request : Request ,
228+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
229+ ) -> Content :
230+ rename_content = RenameContent (** (await request .json ()))
231+ return await self .contents .rename_content (path , rename_content , user )
232+
233+ self .include_router (router )
0 commit comments