Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit 048a03b

Browse files
committed
🎈 perf: update OpenAPI doc, remove unused import, fix #1
1 parent 15dbb0d commit 048a03b

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

‎pastemc/main.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class AppInfo(BaseModel):
1111
title: str = __package__
12+
description: str = "paste, share, even analyze: The pastebin design for Minecraft"
1213
version: str = metadata.version(__package__)
1314
license_info: dict = {
1415
"name": "GNU Affero General Public License v3.0 or later",

‎pastemc/models/common.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
from datetime import datetime
2-
from importlib import metadata
32
from typing import Annotated
43

54
from annotated_types import Len
6-
from pydantic import AnyHttpUrl, BaseModel, BeforeValidator, Field, computed_field
5+
from pydantic import (
6+
AnyHttpUrl,
7+
BaseModel,
8+
BeforeValidator,
9+
Field,
10+
validate_call,
11+
)
712
from ulid import ULID
813

914
from pastemc.utils.s3api import get_object_url
1015

1116
FileId = Annotated[
1217
str,
1318
BeforeValidator(lambda v: str(v)),
14-
Len(26),
19+
Len(26, 26),
1520
Field(
1621
examples=["01HRKVWPKNYNQKB5F209DZ85B7"], description="ULID of the uploaded file"
1722
),
@@ -41,10 +46,15 @@ class FileObjectResponse(BaseModel):
4146
file_id: FileId
4247
url: FileUrl
4348
last_modified: Annotated[
44-
datetime, Field(examples=["2024-03-10T17:42:50.229000+08:00"])
49+
datetime,
50+
Field(
51+
examples=["2024-03-10T17:42:50.229000+08:00"],
52+
description="ISO 8601 datetime with server timezone",
53+
),
4554
]
4655

4756
@classmethod
57+
@validate_call
4858
def public(cls, file_id: FileId):
4959
return cls(
5060
file_id=file_id,

‎pastemc/modules/fetch.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from typing import Annotated
22

33
from annotated_types import Len
4-
from fastapi import APIRouter, status
5-
from fastapi.responses import JSONResponse
4+
from fastapi import APIRouter, Path, Response, status
65

76
from pastemc.models.common import FileObjectResponse, ObjectNotFound
87
from pastemc.utils.s3api import list_objects
@@ -16,13 +15,22 @@
1615
summary="Get File URL by file_id",
1716
responses={404: {"model": ObjectNotFound}},
1817
)
19-
async def fetch_file(file_id: Annotated[str, Len(10, 26)]):
18+
async def fetch_file(
19+
response: Response,
20+
file_id: Annotated[
21+
str,
22+
Len(10, 26),
23+
Path(
24+
description="this field accepts:\n- full ULID (26 digits)\n- partial ULID, with timestamp (at least 10 digits)",
25+
examples=["01HRKVWPKNYNQKB5F209DZ85B7", "01HRKVWPKN"],
26+
),
27+
],
28+
):
2029
if len(file_id) == 26: # full ULID
2130
return FileObjectResponse.public(file_id=file_id)
22-
else: # partial ULID, with timestamp (1-10 digits)
31+
else: # partial ULID, with timestamp (at least 10 digits)
2332
if objects_list := await list_objects(prefix=file_id):
2433
return FileObjectResponse.public(file_id=objects_list[0].object_name)
2534
else:
26-
return JSONResponse(
27-
ObjectNotFound.make(file_id), status_code=status.HTTP_404_NOT_FOUND
28-
)
35+
response.code = status.HTTP_404_NOT_FOUND
36+
return ObjectNotFound.make(file_id)

‎pastemc/modules/upload.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from fastapi import APIRouter, File, Form, UploadFile, status
2-
from fastapi.responses import JSONResponse
1+
from fastapi import APIRouter, File, Form, Response, UploadFile, status
32
from loguru import logger
43

54
from pastemc.models.common import FileObjectResponse
@@ -16,7 +15,6 @@
1615
responses={
1716
201: {
1817
"model": FileObjectResponse,
19-
"description": "`file_id` of the uploaded file",
2018
"headers": {
2119
"Location": {
2220
"$ref": "#/components/schemas/FileObjectResponse/properties/url"
@@ -26,6 +24,7 @@
2624
},
2725
)
2826
async def upload_file(
27+
response: Response,
2928
file: UploadFile = File(description="the (Minecraft log) file"),
3029
format_type: str = Form(
3130
description="the format of this file, used for highlight rendering"
@@ -51,8 +50,6 @@ async def upload_file(
5150
logger.info(f"file uploaded, {file.filename=}, {file_id=}, {length=}")
5251

5352
# return 201 created
54-
return JSONResponse(
55-
FileObjectResponse.public(file_id=file_id).model_dump(),
56-
status_code=status.HTTP_201_CREATED,
57-
headers={"Location": get_object_url(file_id)},
58-
)
53+
response.status_code = status.HTTP_201_CREATED
54+
response.headers["Location"] = get_object_url(file_id)
55+
return FileObjectResponse.public(file_id=file_id)

0 commit comments

Comments
 (0)