4
4
from typing import Dict , List , Optional , Union
5
5
6
6
from fastapi import APIRouter , Depends , Request , Response
7
- from jupyverse_api import Router
7
+ from jupyverse_api import Router , Config
8
8
9
9
from .models import Checkpoint , Content , SaveContent
10
10
from ..auth import Auth , User
@@ -30,81 +30,87 @@ def unwatch(self, path: str, watcher):
30
30
...
31
31
32
32
33
+ class ContentsConfig (Config ):
34
+ include_router : bool = True
35
+ prefix : str = ""
36
+
37
+
33
38
class Contents (Router , ABC ):
34
- def __init__ (self , app : App , auth : Auth ):
39
+ def __init__ (self , app : App , auth : Auth , contents_config : ContentsConfig ):
35
40
super ().__init__ (app = app )
36
41
37
- router = APIRouter ()
38
-
39
- @router .post (
40
- "/api/contents/{path:path}/checkpoints" ,
41
- status_code = 201 ,
42
- )
43
- async def create_checkpoint (
44
- path , user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]}))
45
- ) -> Checkpoint :
46
- return await self .create_checkpoint (path , user )
47
-
48
- @router .post (
49
- "/api/contents{path:path}" ,
50
- status_code = 201 ,
51
- )
52
- async def create_content (
53
- path : Optional [str ],
54
- request : Request ,
55
- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
56
- ) -> Content :
57
- return await self .create_content (path , request , user )
58
-
59
- @router .get ("/api/contents" )
60
- async def get_root_content (
61
- content : int ,
62
- user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
63
- ) -> Content :
64
- return await self .get_root_content (content , user )
65
-
66
- @router .get ("/api/contents/{path:path}/checkpoints" )
67
- async def get_checkpoint (
68
- path , user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]}))
69
- ) -> List [Checkpoint ]:
70
- return await self .get_checkpoint (path , user )
71
-
72
- @router .get ("/api/contents/{path:path}" )
73
- async def get_content (
74
- path : str ,
75
- content : int = 0 ,
76
- user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
77
- ) -> Content :
78
- return await self .get_content (path , content , user )
79
-
80
- @router .put ("/api/contents/{path:path}" )
81
- async def save_content (
82
- path ,
83
- request : Request ,
84
- response : Response ,
85
- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
86
- ) -> Content :
87
- return await self .save_content (path , request , response , user )
88
-
89
- @router .delete (
90
- "/api/contents/{path:path}" ,
91
- status_code = 204 ,
92
- )
93
- async def delete_content (
94
- path ,
95
- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
96
- ):
97
- return await self .delete_content (path , user )
98
-
99
- @router .patch ("/api/contents/{path:path}" )
100
- async def rename_content (
101
- path ,
102
- request : Request ,
103
- user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
104
- ) -> Content :
105
- return await self .rename_content (path , request , user )
106
-
107
- self .include_router (router )
42
+ if contents_config .include_router :
43
+ router = APIRouter ()
44
+
45
+ @router .post (
46
+ "/api/contents/{path:path}/checkpoints" ,
47
+ status_code = 201 ,
48
+ )
49
+ async def create_checkpoint (
50
+ path , user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]}))
51
+ ) -> Checkpoint :
52
+ return await self .create_checkpoint (path , user )
53
+
54
+ @router .post (
55
+ "/api/contents{path:path}" ,
56
+ status_code = 201 ,
57
+ )
58
+ async def create_content (
59
+ path : Optional [str ],
60
+ request : Request ,
61
+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
62
+ ) -> Content :
63
+ return await self .create_content (path , request , user )
64
+
65
+ @router .get ("/api/contents" )
66
+ async def get_root_content (
67
+ content : int ,
68
+ user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
69
+ ) -> Content :
70
+ return await self .get_root_content (content , user )
71
+
72
+ @router .get ("/api/contents/{path:path}/checkpoints" )
73
+ async def get_checkpoint (
74
+ path , user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]}))
75
+ ) -> List [Checkpoint ]:
76
+ return await self .get_checkpoint (path , user )
77
+
78
+ @router .get ("/api/contents/{path:path}" )
79
+ async def get_content (
80
+ path : str ,
81
+ content : int = 0 ,
82
+ user : User = Depends (auth .current_user (permissions = {"contents" : ["read" ]})),
83
+ ) -> Content :
84
+ return await self .get_content (path , content , user )
85
+
86
+ @router .put ("/api/contents/{path:path}" )
87
+ async def save_content (
88
+ path ,
89
+ request : Request ,
90
+ response : Response ,
91
+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
92
+ ) -> Content :
93
+ return await self .save_content (path , request , response , user )
94
+
95
+ @router .delete (
96
+ "/api/contents/{path:path}" ,
97
+ status_code = 204 ,
98
+ )
99
+ async def delete_content (
100
+ path ,
101
+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
102
+ ):
103
+ return await self .delete_content (path , user )
104
+
105
+ @router .patch ("/api/contents/{path:path}" )
106
+ async def rename_content (
107
+ path ,
108
+ request : Request ,
109
+ user : User = Depends (auth .current_user (permissions = {"contents" : ["write" ]})),
110
+ ) -> Content :
111
+ return await self .rename_content (path , request , user )
112
+
113
+ self .include_router (router , prefix = contents_config .prefix )
108
114
109
115
@property
110
116
@abstractmethod
0 commit comments