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 :
@@ -134,11 +60,25 @@ async def create_checkpoint(
13460 async def create_content (
13561 self ,
13662 path : Optional [str ],
137- request : Request ,
63+ create_content : Union [ CreateContent , CopyContent ] ,
13864 user : User ,
13965 ) -> Content :
14066 ...
14167
68+ @abstractmethod
69+ async def create_file (
70+ self ,
71+ path : str ,
72+ ) -> None :
73+ ...
74+
75+ @abstractmethod
76+ async def create_directory (
77+ self ,
78+ path : str ,
79+ ) -> None :
80+ ...
81+
14282 @abstractmethod
14383 async def get_root_content (
14484 self ,
@@ -168,8 +108,7 @@ async def get_content(
168108 async def save_content (
169109 self ,
170110 path ,
171- request : Request ,
172- response : Response ,
111+ content : SaveContent ,
173112 user : User ,
174113 ) -> Content :
175114 ...
@@ -186,7 +125,93 @@ async def delete_content(
186125 async def rename_content (
187126 self ,
188127 path ,
189- request : Request ,
128+ rename_content : RenameContent ,
190129 user : User ,
191130 ) -> Content :
192131 ...
132+
133+
134+ class HTTPContents (Router , ABC ):
135+ contents : Contents
136+
137+ def __init__ (self , app : App , auth : Auth ):
138+ super ().__init__ (app = app )
139+
140+ router = APIRouter ()
141+
142+ @router .post (
143+ "/api/contents/{path:path}/checkpoints" ,
144+ status_code = 201 ,
145+ )
146+ async def create_checkpoint (
147+ path , user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]}))
148+ ) -> Checkpoint :
149+ return await self .contents .create_checkpoint (path , user )
150+
151+ @router .post (
152+ "/api/contents{path:path}" ,
153+ status_code = 201 ,
154+ )
155+ async def create_content (
156+ path : Optional [str ],
157+ request : Request ,
158+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
159+ ) -> Content :
160+ r = await request .json ()
161+ create_content : Union [CreateContent , CopyContent ]
162+ try :
163+ create_content = CreateContent (** r )
164+ except Exception :
165+ create_content = CopyContent (** r )
166+ return await self .contents .create_content (path , create_content , user )
167+
168+ @router .get ("/api/contents" )
169+ async def get_root_content (
170+ content : int ,
171+ user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
172+ ) -> Content :
173+ return await self .contents .get_root_content (content , user )
174+
175+ @router .get ("/api/contents/{path:path}/checkpoints" )
176+ async def get_checkpoint (
177+ path , user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]}))
178+ ) -> List [Checkpoint ]:
179+ return await self .contents .get_checkpoint (path , user )
180+
181+ @router .get ("/api/contents/{path:path}" )
182+ async def get_content (
183+ path : str ,
184+ content : int = 0 ,
185+ user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
186+ ) -> Content :
187+ return await self .contents .get_content (path , content , user )
188+
189+ @router .put ("/api/contents/{path:path}" )
190+ async def save_content (
191+ path ,
192+ request : Request ,
193+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
194+ ) -> Content :
195+ content = SaveContent (** (await request .json ()))
196+ return await self .contents .save_content (path , content , user )
197+
198+ @router .delete (
199+ "/api/contents/{path:path}" ,
200+ status_code = 204 ,
201+ )
202+ async def delete_content (
203+ path ,
204+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
205+ ):
206+ return await self .contents .delete_content (path , user )
207+
208+ @router .patch ("/api/contents/{path:path}" )
209+ async def rename_content (
210+ path ,
211+ request : Request ,
212+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
213+ ) -> Content :
214+ rename_content = RenameContent (** (await request .json ()))
215+ return await self .contents .rename_content (path , rename_content , user )
216+
217+ self .include_router (router )
0 commit comments