1
1
import asyncio
2
2
from abc import ABC , abstractmethod
3
3
from pathlib import Path
4
- from typing import Dict , List , Optional , Union
4
+ from typing import Any , Dict , List , Optional , Union
5
5
6
- from fastapi import APIRouter , Depends , Request , Response
6
+ from fastapi import APIRouter , Depends , Request
7
7
8
8
from jupyverse_api import Router
9
9
10
10
from ..app import App
11
11
from ..auth import Auth , User
12
- from .models import Checkpoint , Content , SaveContent
12
+ from .models import Checkpoint , Content , CopyContent , CreateContent , RenameContent , SaveContent
13
13
14
14
15
15
class FileIdManager (ABC ):
16
16
stop_watching_files : asyncio .Event
17
17
stopped_watching_files : asyncio .Event
18
+ Change : Any
18
19
19
20
@abstractmethod
20
21
async def get_path (self , file_id : str ) -> str :
@@ -31,82 +32,7 @@ def unwatch(self, path: str, watcher):
31
32
...
32
33
33
34
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 ):
110
36
@property
111
37
@abstractmethod
112
38
def file_id_manager (self ) -> FileIdManager :
@@ -130,15 +56,45 @@ async def create_checkpoint(
130
56
) -> Checkpoint :
131
57
...
132
58
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
+
133
75
@abstractmethod
134
76
async def create_content (
135
77
self ,
136
78
path : Optional [str ],
137
- request : Request ,
79
+ create_content : Union [ CreateContent , CopyContent ] ,
138
80
user : User ,
139
81
) -> Content :
140
82
...
141
83
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
+
142
98
@abstractmethod
143
99
async def get_root_content (
144
100
self ,
@@ -168,8 +124,7 @@ async def get_content(
168
124
async def save_content (
169
125
self ,
170
126
path ,
171
- request : Request ,
172
- response : Response ,
127
+ content : SaveContent ,
173
128
user : User ,
174
129
) -> Content :
175
130
...
@@ -186,7 +141,93 @@ async def delete_content(
186
141
async def rename_content (
187
142
self ,
188
143
path ,
189
- request : Request ,
144
+ rename_content : RenameContent ,
190
145
user : User ,
191
146
) -> Content :
192
147
...
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