Skip to content

Commit 54def74

Browse files
committed
APIRouter will redirect trailing end slashes automatically
1 parent 6163699 commit 54def74

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

docat/docat/app.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import Optional
1818

1919
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
2121
from fastapi.staticfiles import StaticFiles
2222
from starlette.responses import JSONResponse
2323
from tinydb import Query, TinyDB
@@ -65,34 +65,29 @@ def get_db() -> TinyDB:
6565
redoc_url="/api/redoc",
6666
lifespan=lifespan,
6767
)
68+
router = APIRouter()
6869

6970

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)
7172
def get_stats():
7273
if not DOCAT_UPLOAD_FOLDER.exists():
7374
return Projects(projects=[])
7475
return get_system_stats(DOCAT_UPLOAD_FOLDER)
7576

7677

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)
7879
def get_projects(include_hidden: bool = False):
7980
if not DOCAT_UPLOAD_FOLDER.exists():
8081
return Projects(projects=[])
8182
return get_all_projects(DOCAT_UPLOAD_FOLDER, include_hidden)
8283

8384

84-
@app.get(
85+
@router.get(
8586
"/api/projects/{project}",
8687
response_model=ProjectDetail,
8788
status_code=status.HTTP_200_OK,
8889
responses={status.HTTP_404_NOT_FOUND: {"model": ApiResponse}},
8990
)
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-
)
9691
def get_project(project, include_hidden: bool = False):
9792
details = get_project_details(DOCAT_UPLOAD_FOLDER, project, include_hidden)
9893

@@ -102,8 +97,7 @@ def get_project(project, include_hidden: bool = False):
10297
return details
10398

10499

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)
107101
def upload_icon(
108102
project: str,
109103
response: Response,
@@ -147,8 +141,7 @@ def upload_icon(
147141
return ApiResponse(message="Icon successfully uploaded")
148142

149143

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)
152145
def hide_version(
153146
project: str,
154147
version: str,
@@ -183,8 +176,7 @@ def hide_version(
183176
return ApiResponse(message=f"Version {version} is now hidden")
184177

185178

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)
188180
def show_version(
189181
project: str,
190182
version: str,
@@ -218,8 +210,7 @@ def show_version(
218210
return ApiResponse(message=f"Version {version} is now shown")
219211

220212

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)
223214
def upload(
224215
project: str,
225216
version: str,
@@ -278,8 +269,7 @@ def upload(
278269
return ApiResponse(message="Documentation uploaded successfully")
279270

280271

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)
283273
def tag(project: str, version: str, new_tag: str, response: Response):
284274
destination = DOCAT_UPLOAD_FOLDER / project / new_tag
285275
source = DOCAT_UPLOAD_FOLDER / project / version
@@ -295,18 +285,12 @@ def tag(project: str, version: str, new_tag: str, response: Response):
295285
return ApiResponse(message=f"Tag {new_tag} -> {version} successfully created")
296286

297287

298-
@app.get(
288+
@router.get(
299289
"/api/{project}/claim",
300290
response_model=ClaimResponse,
301291
status_code=status.HTTP_201_CREATED,
302292
responses={status.HTTP_409_CONFLICT: {"model": ApiResponse}},
303293
)
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-
)
310294
def claim(project: str, db: TinyDB = Depends(get_db)):
311295
Project = Query()
312296
table = db.table("claims")
@@ -322,8 +306,7 @@ def claim(project: str, db: TinyDB = Depends(get_db)):
322306
return ClaimResponse(message=f"Project {project} successfully claimed", token=token)
323307

324308

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)
327310
def rename(
328311
project: str,
329312
new_project_name: str,
@@ -362,8 +345,7 @@ def rename(
362345
return ApiResponse(message=f"Successfully renamed project {project} to {new_project_name}")
363346

364347

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)
367349
def delete(
368350
project: str,
369351
version: str,
@@ -407,3 +389,5 @@ def check_token_for_project(db, token, project) -> TokenStatus:
407389
if os.environ.get("DOCAT_SERVE_FILES"):
408390
DOCAT_UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)
409391
app.mount("/doc", StaticFiles(directory=DOCAT_UPLOAD_FOLDER, html=True), name="docs")
392+
393+
app.include_router(router)

0 commit comments

Comments
 (0)