17
17
from typing import Optional
18
18
19
19
import magic
20
- from fastapi import Depends , FastAPI , File , Header , Response , UploadFile , status
20
+ from fastapi import APIRouter , Depends , FastAPI , File , Header , Response , UploadFile , status
21
21
from fastapi .staticfiles import StaticFiles
22
22
from starlette .responses import JSONResponse
23
23
from tinydb import Query , TinyDB
@@ -65,34 +65,29 @@ def get_db() -> TinyDB:
65
65
redoc_url = "/api/redoc" ,
66
66
lifespan = lifespan ,
67
67
)
68
+ router = APIRouter ()
68
69
69
70
70
- @app .get ("/api/stats" , response_model = Stats , status_code = status .HTTP_200_OK )
71
+ @router .get ("/api/stats" , response_model = Stats , status_code = status .HTTP_200_OK )
71
72
def get_stats ():
72
73
if not DOCAT_UPLOAD_FOLDER .exists ():
73
74
return Projects (projects = [])
74
75
return get_system_stats (DOCAT_UPLOAD_FOLDER )
75
76
76
77
77
- @app .get ("/api/projects" , response_model = Projects , status_code = status .HTTP_200_OK )
78
+ @router .get ("/api/projects" , response_model = Projects , status_code = status .HTTP_200_OK )
78
79
def get_projects (include_hidden : bool = False ):
79
80
if not DOCAT_UPLOAD_FOLDER .exists ():
80
81
return Projects (projects = [])
81
82
return get_all_projects (DOCAT_UPLOAD_FOLDER , include_hidden )
82
83
83
84
84
- @app .get (
85
+ @router .get (
85
86
"/api/projects/{project}" ,
86
87
response_model = ProjectDetail ,
87
88
status_code = status .HTTP_200_OK ,
88
89
responses = {status .HTTP_404_NOT_FOUND : {"model" : ApiResponse }},
89
90
)
90
- @app .get (
91
- "/api/projects/{project}/" ,
92
- response_model = ProjectDetail ,
93
- status_code = status .HTTP_200_OK ,
94
- responses = {status .HTTP_404_NOT_FOUND : {"model" : ApiResponse }},
95
- )
96
91
def get_project (project , include_hidden : bool = False ):
97
92
details = get_project_details (DOCAT_UPLOAD_FOLDER , project , include_hidden )
98
93
@@ -102,8 +97,7 @@ def get_project(project, include_hidden: bool = False):
102
97
return details
103
98
104
99
105
- @app .post ("/api/{project}/icon" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
106
- @app .post ("/api/{project}/icon/" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
100
+ @router .post ("/api/{project}/icon" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
107
101
def upload_icon (
108
102
project : str ,
109
103
response : Response ,
@@ -147,8 +141,7 @@ def upload_icon(
147
141
return ApiResponse (message = "Icon successfully uploaded" )
148
142
149
143
150
- @app .post ("/api/{project}/{version}/hide" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
151
- @app .post ("/api/{project}/{version}/hide/" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
144
+ @router .post ("/api/{project}/{version}/hide" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
152
145
def hide_version (
153
146
project : str ,
154
147
version : str ,
@@ -183,8 +176,7 @@ def hide_version(
183
176
return ApiResponse (message = f"Version { version } is now hidden" )
184
177
185
178
186
- @app .post ("/api/{project}/{version}/show" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
187
- @app .post ("/api/{project}/{version}/show/" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
179
+ @router .post ("/api/{project}/{version}/show" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
188
180
def show_version (
189
181
project : str ,
190
182
version : str ,
@@ -218,8 +210,7 @@ def show_version(
218
210
return ApiResponse (message = f"Version { version } is now shown" )
219
211
220
212
221
- @app .post ("/api/{project}/{version}" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
222
- @app .post ("/api/{project}/{version}/" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
213
+ @router .post ("/api/{project}/{version}" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
223
214
def upload (
224
215
project : str ,
225
216
version : str ,
@@ -278,8 +269,7 @@ def upload(
278
269
return ApiResponse (message = "Documentation uploaded successfully" )
279
270
280
271
281
- @app .put ("/api/{project}/{version}/tags/{new_tag}" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
282
- @app .put ("/api/{project}/{version}/tags/{new_tag}/" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
272
+ @router .put ("/api/{project}/{version}/tags/{new_tag}" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
283
273
def tag (project : str , version : str , new_tag : str , response : Response ):
284
274
destination = DOCAT_UPLOAD_FOLDER / project / new_tag
285
275
source = DOCAT_UPLOAD_FOLDER / project / version
@@ -295,18 +285,12 @@ def tag(project: str, version: str, new_tag: str, response: Response):
295
285
return ApiResponse (message = f"Tag { new_tag } -> { version } successfully created" )
296
286
297
287
298
- @app .get (
288
+ @router .get (
299
289
"/api/{project}/claim" ,
300
290
response_model = ClaimResponse ,
301
291
status_code = status .HTTP_201_CREATED ,
302
292
responses = {status .HTTP_409_CONFLICT : {"model" : ApiResponse }},
303
293
)
304
- @app .get (
305
- "/api/{project}/claim/" ,
306
- response_model = ClaimResponse ,
307
- status_code = status .HTTP_201_CREATED ,
308
- responses = {status .HTTP_409_CONFLICT : {"model" : ApiResponse }},
309
- )
310
294
def claim (project : str , db : TinyDB = Depends (get_db )):
311
295
Project = Query ()
312
296
table = db .table ("claims" )
@@ -322,8 +306,7 @@ def claim(project: str, db: TinyDB = Depends(get_db)):
322
306
return ClaimResponse (message = f"Project { project } successfully claimed" , token = token )
323
307
324
308
325
- @app .put ("/api/{project}/rename/{new_project_name}" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
326
- @app .put ("/api/{project}/rename/{new_project_name}/" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
309
+ @router .put ("/api/{project}/rename/{new_project_name}" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
327
310
def rename (
328
311
project : str ,
329
312
new_project_name : str ,
@@ -362,8 +345,7 @@ def rename(
362
345
return ApiResponse (message = f"Successfully renamed project { project } to { new_project_name } " )
363
346
364
347
365
- @app .delete ("/api/{project}/{version}" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
366
- @app .delete ("/api/{project}/{version}/" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
348
+ @router .delete ("/api/{project}/{version}" , response_model = ApiResponse , status_code = status .HTTP_200_OK )
367
349
def delete (
368
350
project : str ,
369
351
version : str ,
@@ -407,3 +389,5 @@ def check_token_for_project(db, token, project) -> TokenStatus:
407
389
if os .environ .get ("DOCAT_SERVE_FILES" ):
408
390
DOCAT_UPLOAD_FOLDER .mkdir (parents = True , exist_ok = True )
409
391
app .mount ("/doc" , StaticFiles (directory = DOCAT_UPLOAD_FOLDER , html = True ), name = "docs" )
392
+
393
+ app .include_router (router )
0 commit comments