FastAPI-fastkit 1.0.0__py3-none-any.whl → 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_fastkit/__init__.py +10 -1
- fastapi_fastkit/backend/main.py +482 -482
- fastapi_fastkit/backend/transducer.py +106 -106
- fastapi_fastkit/cli.py +664 -631
- fastapi_fastkit/core/settings.py +155 -155
- fastapi_fastkit/fastapi_project_template/README.md +90 -90
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/.gitignore-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/README.md-tpl +107 -107
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/requirements.txt-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/test.sh-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/setup.cfg-tpl +14 -14
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/setup.py-tpl +25 -25
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/api/api.py-tpl +9 -9
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/api/routes/items.py-tpl +57 -57
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/core/config.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/crud/items.py-tpl +21 -21
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/main.py-tpl +26 -26
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/schemas/items.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/tests/conftest.py-tpl +42 -42
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/tests/test_items.py-tpl +48 -48
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/.gitignore-tpl +191 -191
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/README.md-tpl +156 -156
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/requirements.txt-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/test.sh-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/setup.cfg-tpl +12 -12
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/setup.py-tpl +27 -27
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/.DS_Store +0 -0
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/api/api.py-tpl +9 -9
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/api/routes/items.py-tpl +172 -172
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/core/config.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/crud/items.py-tpl +21 -21
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/exceptions.py-tpl +103 -103
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/pagination.py-tpl +72 -72
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/main.py-tpl +39 -39
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/schemas/base.py-tpl +49 -49
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/schemas/items.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/utils/documents.py-tpl +20 -20
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/tests/conftest.py-tpl +42 -42
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/tests/test_items.py-tpl +61 -61
- fastapi_fastkit/fastapi_project_template/fastapi-default/README.md-tpl +101 -101
- fastapi_fastkit/fastapi_project_template/fastapi-default/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-default/setup.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-default/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-default/tests/conftest.py-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-default/tests/test_items.py-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/.gitignore-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/Dockerfile-tpl +23 -23
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/README.md-tpl +119 -119
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/requirements.txt-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/test.sh-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/setup.cfg-tpl +13 -13
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/setup.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/api/api.py-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/api/routes/items.py-tpl +54 -54
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/core/config.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/crud/items.py-tpl +15 -15
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/main.py-tpl +26 -26
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/schemas/items.py-tpl +19 -19
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/tests/conftest.py-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/tests/test_items.py-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/fastapi-empty/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-empty/setup.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-empty/src/core/config.py-tpl +75 -75
- fastapi_fastkit/fastapi_project_template/fastapi-empty/src/main.py-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/.env-tpl +9 -9
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/.gitignore-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/Dockerfile-tpl +23 -23
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/README.md-tpl +144 -144
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/alembic.ini-tpl +119 -119
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/docker-compose.yml-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/requirements.txt-tpl +41 -41
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/pre-start.sh-tpl +16 -16
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/test.sh-tpl +10 -10
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/setup.cfg-tpl +13 -13
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/setup.py-tpl +25 -25
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/README-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/env.py-tpl +85 -85
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/script.py.mako-tpl +26 -26
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/versions/bedcdc35b64a_first_alembic.py-tpl +37 -37
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/api.py-tpl +11 -11
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/deps.py-tpl +19 -19
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/routes/items.py-tpl +109 -109
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/core/config.py-tpl +88 -88
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/core/db.py-tpl +53 -53
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/crud/items.py-tpl +102 -102
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/main.py-tpl +45 -45
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/schemas/items.py-tpl +54 -54
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/backend_pre_start.py-tpl +39 -39
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/init_data.py-tpl +23 -23
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/tests_pre_start.py-tpl +39 -39
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/tests/conftest.py-tpl +63 -63
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/tests/test_items.py-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/modules/api/routes/new_route.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/modules/crud/new_route.py-tpl +29 -29
- fastapi_fastkit/fastapi_project_template/modules/schemas/new_route.py-tpl +23 -23
- fastapi_fastkit/logs/fastkit_debug_20250702_161423.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_162706.log +20 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_163010.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_163526.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_164053.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_165220.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_165647.log +21 -0
- fastapi_fastkit/utils/logging.py +112 -1
- fastapi_fastkit/utils/main.py +140 -129
- fastapi_fastkit-1.0.2.dist-info/METADATA +320 -0
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/RECORD +127 -119
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/WHEEL +1 -1
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/licenses/LICENSE +21 -21
- fastapi_fastkit-1.0.0.dist-info/METADATA +0 -208
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/entry_points.txt +0 -0
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# Configuration module
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
import secrets
|
|
5
|
-
import warnings
|
|
6
|
-
from typing import Annotated, Any, Literal
|
|
7
|
-
|
|
8
|
-
from pydantic import AnyUrl, BeforeValidator, computed_field, model_validator
|
|
9
|
-
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
10
|
-
from typing_extensions import Self
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def parse_cors(v: Any) -> list[str] | str:
|
|
14
|
-
if isinstance(v, str) and not v.startswith("["):
|
|
15
|
-
return [i.strip() for i in v.split(",")]
|
|
16
|
-
elif isinstance(v, list | str):
|
|
17
|
-
return v
|
|
18
|
-
raise ValueError(v)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class Settings(BaseSettings):
|
|
22
|
-
model_config = SettingsConfigDict(
|
|
23
|
-
env_file=".env",
|
|
24
|
-
env_ignore_empty=True,
|
|
25
|
-
extra="ignore",
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
SECRET_KEY: str = secrets.token_urlsafe(32)
|
|
29
|
-
ENVIRONMENT: Literal["development", "production"] = "development"
|
|
30
|
-
|
|
31
|
-
CLIENT_ORIGIN: str = ""
|
|
32
|
-
|
|
33
|
-
BACKEND_CORS_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors)] = (
|
|
34
|
-
[]
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
@computed_field # type: ignore[prop-decorator]
|
|
38
|
-
@property
|
|
39
|
-
def all_cors_origins(self) -> list[str]:
|
|
40
|
-
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
|
|
41
|
-
self.CLIENT_ORIGIN
|
|
42
|
-
]
|
|
43
|
-
|
|
44
|
-
PROJECT_NAME: str = "<project_name>"
|
|
45
|
-
|
|
46
|
-
def _check_default_secret(self, var_name: str, value: str | None) -> None:
|
|
47
|
-
if value == "changethis":
|
|
48
|
-
message = (
|
|
49
|
-
f'The value of {var_name} is "changethis", '
|
|
50
|
-
"for security, please change it, at least for deployments."
|
|
51
|
-
)
|
|
52
|
-
if self.ENVIRONMENT == "development":
|
|
53
|
-
warnings.warn(message, stacklevel=1)
|
|
54
|
-
else:
|
|
55
|
-
raise ValueError(message)
|
|
56
|
-
|
|
57
|
-
@model_validator(mode="after")
|
|
58
|
-
def _enforce_non_default_secrets(self) -> Self:
|
|
59
|
-
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
|
|
60
|
-
|
|
61
|
-
return self
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
settings = Settings() # type: ignore
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# Configuration module
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
import secrets
|
|
5
|
+
import warnings
|
|
6
|
+
from typing import Annotated, Any, Literal
|
|
7
|
+
|
|
8
|
+
from pydantic import AnyUrl, BeforeValidator, computed_field, model_validator
|
|
9
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
10
|
+
from typing_extensions import Self
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def parse_cors(v: Any) -> list[str] | str:
|
|
14
|
+
if isinstance(v, str) and not v.startswith("["):
|
|
15
|
+
return [i.strip() for i in v.split(",")]
|
|
16
|
+
elif isinstance(v, list | str):
|
|
17
|
+
return v
|
|
18
|
+
raise ValueError(v)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Settings(BaseSettings):
|
|
22
|
+
model_config = SettingsConfigDict(
|
|
23
|
+
env_file=".env",
|
|
24
|
+
env_ignore_empty=True,
|
|
25
|
+
extra="ignore",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
SECRET_KEY: str = secrets.token_urlsafe(32)
|
|
29
|
+
ENVIRONMENT: Literal["development", "production"] = "development"
|
|
30
|
+
|
|
31
|
+
CLIENT_ORIGIN: str = ""
|
|
32
|
+
|
|
33
|
+
BACKEND_CORS_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors)] = (
|
|
34
|
+
[]
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
@computed_field # type: ignore[prop-decorator]
|
|
38
|
+
@property
|
|
39
|
+
def all_cors_origins(self) -> list[str]:
|
|
40
|
+
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
|
|
41
|
+
self.CLIENT_ORIGIN
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
PROJECT_NAME: str = "<project_name>"
|
|
45
|
+
|
|
46
|
+
def _check_default_secret(self, var_name: str, value: str | None) -> None:
|
|
47
|
+
if value == "changethis":
|
|
48
|
+
message = (
|
|
49
|
+
f'The value of {var_name} is "changethis", '
|
|
50
|
+
"for security, please change it, at least for deployments."
|
|
51
|
+
)
|
|
52
|
+
if self.ENVIRONMENT == "development":
|
|
53
|
+
warnings.warn(message, stacklevel=1)
|
|
54
|
+
else:
|
|
55
|
+
raise ValueError(message)
|
|
56
|
+
|
|
57
|
+
@model_validator(mode="after")
|
|
58
|
+
def _enforce_non_default_secrets(self) -> Self:
|
|
59
|
+
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
|
|
60
|
+
|
|
61
|
+
return self
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
settings = Settings() # type: ignore
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# Item CRUD method module
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
import json
|
|
5
|
-
import os
|
|
6
|
-
from typing import List
|
|
7
|
-
|
|
8
|
-
import aiofiles # type: ignore
|
|
9
|
-
|
|
10
|
-
MOCK_FILE = os.path.join(os.path.dirname(__file__), "../mocks/mock_items.json")
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
async def read_items():
|
|
14
|
-
async with aiofiles.open(MOCK_FILE, mode="r") as file:
|
|
15
|
-
contents = await file.read()
|
|
16
|
-
return json.loads(contents)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
async def write_items(items: List[dict]):
|
|
20
|
-
async with aiofiles.open(MOCK_FILE, mode="w") as file:
|
|
21
|
-
await file.write(json.dumps(items, indent=2))
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# Item CRUD method module
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
from typing import List
|
|
7
|
+
|
|
8
|
+
import aiofiles # type: ignore
|
|
9
|
+
|
|
10
|
+
MOCK_FILE = os.path.join(os.path.dirname(__file__), "../mocks/mock_items.json")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
async def read_items():
|
|
14
|
+
async with aiofiles.open(MOCK_FILE, mode="r") as file:
|
|
15
|
+
contents = await file.read()
|
|
16
|
+
return json.loads(contents)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def write_items(items: List[dict]):
|
|
20
|
+
async with aiofiles.open(MOCK_FILE, mode="w") as file:
|
|
21
|
+
await file.write(json.dumps(items, indent=2))
|
fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/exceptions.py-tpl
CHANGED
|
@@ -1,103 +1,103 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# The module defines custom Backend Application Exception class, overrides basic Exception.
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
from datetime import UTC, datetime
|
|
5
|
-
from enum import Enum
|
|
6
|
-
|
|
7
|
-
from pydantic import BaseModel, Field
|
|
8
|
-
|
|
9
|
-
from src.schemas.base import ResponseSchema
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class ErrorCode(Enum):
|
|
13
|
-
"""
|
|
14
|
-
ErrorCode is designed to easily categorize errors into code in the log of the server application.
|
|
15
|
-
|
|
16
|
-
It is designed for custom code-based log filtering in third-party log monitoring systems
|
|
17
|
-
by matching with key HTTP status codes.
|
|
18
|
-
"""
|
|
19
|
-
|
|
20
|
-
# HTTP
|
|
21
|
-
BAD_REQUEST = ("BAD_REQUEST", "HTTP-001", 400)
|
|
22
|
-
NOT_FOUND = ("NOT_FOUND", "HTTP-002", 404)
|
|
23
|
-
METHOD_NOT_ALLOWED = ("METHOD_NOT_ALLOWED", "HTTP-003", 405)
|
|
24
|
-
NOT_ACCESSABLE = ("NOT_ACCESSABLE", "HTTP-004", 406)
|
|
25
|
-
TIMEOUT = ("TIMEOUT", "HTTP-005", 408)
|
|
26
|
-
UNPROCESSABLE = ("UNPROCESSABLE", "HTTP-006", 422)
|
|
27
|
-
TOO_MANY_REQUEST = ("TOO_MANY_REQUEST", "HTTP-007", 429)
|
|
28
|
-
|
|
29
|
-
# DATA
|
|
30
|
-
CONFLICT = ("CONFLICT", "DATA-001", 409)
|
|
31
|
-
|
|
32
|
-
# AUTH
|
|
33
|
-
UNAUTHORIZED = ("UNAUTHORIZED", "AUTH-001", 401)
|
|
34
|
-
FORBIDDEN = ("FORBIDDEN", "AUTH-002", 403)
|
|
35
|
-
|
|
36
|
-
# SERVER
|
|
37
|
-
UNKNOWN_ERROR = ("UNKNOWN_ERROR", "SEVR-001", 500)
|
|
38
|
-
BAD_GATEWAY = ("BAD_GATEWAY", "SEVR-002", 502)
|
|
39
|
-
SERVICE_UNAVAILABLE = ("SERVICE_UNAVAILABLE", "SEVR-003", 503)
|
|
40
|
-
GATEWAY_TIMEOUT = ("GATEWAY_TIMEOUT", "SEVR-004", 504)
|
|
41
|
-
|
|
42
|
-
# [Example] Specific Error Code for ITEM
|
|
43
|
-
ITEM_NOT_FOUND = ("ITEM_NOT_FOUND", "DATA-002", 404)
|
|
44
|
-
ITEM_CONFLICT = ("ITEM_CONFLICT", "DATA-003", 409)
|
|
45
|
-
|
|
46
|
-
# < ADD YOUR ERROR CODES HERE >
|
|
47
|
-
|
|
48
|
-
def __init__(self, error: str, code: str, status_code: int):
|
|
49
|
-
self.error = error
|
|
50
|
-
self.code = code
|
|
51
|
-
self.status_code = status_code
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
class ExceptionSchema(BaseModel):
|
|
55
|
-
timestamp: str = Field(
|
|
56
|
-
...,
|
|
57
|
-
description="Timestamp of the error occurred.",
|
|
58
|
-
)
|
|
59
|
-
status: int = Field(..., description="HTTP status code of the error")
|
|
60
|
-
code: str = Field(
|
|
61
|
-
...,
|
|
62
|
-
description="Server identification code of the error.",
|
|
63
|
-
)
|
|
64
|
-
message: str = Field(
|
|
65
|
-
...,
|
|
66
|
-
description="Message content of the error.",
|
|
67
|
-
)
|
|
68
|
-
path: str = Field(
|
|
69
|
-
...,
|
|
70
|
-
description="Path where the error occurred.",
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
class ConfigDict:
|
|
74
|
-
json_schema_extra = {
|
|
75
|
-
"example": {
|
|
76
|
-
"default": {
|
|
77
|
-
"timestamp": "2023-02-10T01:00:00.000Z",
|
|
78
|
-
"status": 500,
|
|
79
|
-
"code": "SEVR-000",
|
|
80
|
-
"path": "/v1/<some/endpoint>",
|
|
81
|
-
"message": "ERROR : Unknown error occurred in server logic.",
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
class InternalException(Exception):
|
|
88
|
-
def __init__(self, message: str, error_code: ErrorCode):
|
|
89
|
-
self.timestamp = (
|
|
90
|
-
datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
91
|
-
)
|
|
92
|
-
self.status = error_code.status_code
|
|
93
|
-
self.error_code = error_code.code
|
|
94
|
-
self.message = f"ERROR : {message}"
|
|
95
|
-
|
|
96
|
-
def to_response(self, path: str) -> ResponseSchema[str]:
|
|
97
|
-
return ResponseSchema(
|
|
98
|
-
timestamp=self.timestamp,
|
|
99
|
-
status=self.status,
|
|
100
|
-
code=self.error_code,
|
|
101
|
-
path=path,
|
|
102
|
-
message=self.message,
|
|
103
|
-
)
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# The module defines custom Backend Application Exception class, overrides basic Exception.
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
from datetime import UTC, datetime
|
|
5
|
+
from enum import Enum
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel, Field
|
|
8
|
+
|
|
9
|
+
from src.schemas.base import ResponseSchema
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ErrorCode(Enum):
|
|
13
|
+
"""
|
|
14
|
+
ErrorCode is designed to easily categorize errors into code in the log of the server application.
|
|
15
|
+
|
|
16
|
+
It is designed for custom code-based log filtering in third-party log monitoring systems
|
|
17
|
+
by matching with key HTTP status codes.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# HTTP
|
|
21
|
+
BAD_REQUEST = ("BAD_REQUEST", "HTTP-001", 400)
|
|
22
|
+
NOT_FOUND = ("NOT_FOUND", "HTTP-002", 404)
|
|
23
|
+
METHOD_NOT_ALLOWED = ("METHOD_NOT_ALLOWED", "HTTP-003", 405)
|
|
24
|
+
NOT_ACCESSABLE = ("NOT_ACCESSABLE", "HTTP-004", 406)
|
|
25
|
+
TIMEOUT = ("TIMEOUT", "HTTP-005", 408)
|
|
26
|
+
UNPROCESSABLE = ("UNPROCESSABLE", "HTTP-006", 422)
|
|
27
|
+
TOO_MANY_REQUEST = ("TOO_MANY_REQUEST", "HTTP-007", 429)
|
|
28
|
+
|
|
29
|
+
# DATA
|
|
30
|
+
CONFLICT = ("CONFLICT", "DATA-001", 409)
|
|
31
|
+
|
|
32
|
+
# AUTH
|
|
33
|
+
UNAUTHORIZED = ("UNAUTHORIZED", "AUTH-001", 401)
|
|
34
|
+
FORBIDDEN = ("FORBIDDEN", "AUTH-002", 403)
|
|
35
|
+
|
|
36
|
+
# SERVER
|
|
37
|
+
UNKNOWN_ERROR = ("UNKNOWN_ERROR", "SEVR-001", 500)
|
|
38
|
+
BAD_GATEWAY = ("BAD_GATEWAY", "SEVR-002", 502)
|
|
39
|
+
SERVICE_UNAVAILABLE = ("SERVICE_UNAVAILABLE", "SEVR-003", 503)
|
|
40
|
+
GATEWAY_TIMEOUT = ("GATEWAY_TIMEOUT", "SEVR-004", 504)
|
|
41
|
+
|
|
42
|
+
# [Example] Specific Error Code for ITEM
|
|
43
|
+
ITEM_NOT_FOUND = ("ITEM_NOT_FOUND", "DATA-002", 404)
|
|
44
|
+
ITEM_CONFLICT = ("ITEM_CONFLICT", "DATA-003", 409)
|
|
45
|
+
|
|
46
|
+
# < ADD YOUR ERROR CODES HERE >
|
|
47
|
+
|
|
48
|
+
def __init__(self, error: str, code: str, status_code: int):
|
|
49
|
+
self.error = error
|
|
50
|
+
self.code = code
|
|
51
|
+
self.status_code = status_code
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ExceptionSchema(BaseModel):
|
|
55
|
+
timestamp: str = Field(
|
|
56
|
+
...,
|
|
57
|
+
description="Timestamp of the error occurred.",
|
|
58
|
+
)
|
|
59
|
+
status: int = Field(..., description="HTTP status code of the error")
|
|
60
|
+
code: str = Field(
|
|
61
|
+
...,
|
|
62
|
+
description="Server identification code of the error.",
|
|
63
|
+
)
|
|
64
|
+
message: str = Field(
|
|
65
|
+
...,
|
|
66
|
+
description="Message content of the error.",
|
|
67
|
+
)
|
|
68
|
+
path: str = Field(
|
|
69
|
+
...,
|
|
70
|
+
description="Path where the error occurred.",
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
class ConfigDict:
|
|
74
|
+
json_schema_extra = {
|
|
75
|
+
"example": {
|
|
76
|
+
"default": {
|
|
77
|
+
"timestamp": "2023-02-10T01:00:00.000Z",
|
|
78
|
+
"status": 500,
|
|
79
|
+
"code": "SEVR-000",
|
|
80
|
+
"path": "/v1/<some/endpoint>",
|
|
81
|
+
"message": "ERROR : Unknown error occurred in server logic.",
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class InternalException(Exception):
|
|
88
|
+
def __init__(self, message: str, error_code: ErrorCode):
|
|
89
|
+
self.timestamp = (
|
|
90
|
+
datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
91
|
+
)
|
|
92
|
+
self.status = error_code.status_code
|
|
93
|
+
self.error_code = error_code.code
|
|
94
|
+
self.message = f"ERROR : {message}"
|
|
95
|
+
|
|
96
|
+
def to_response(self, path: str) -> ResponseSchema[str]:
|
|
97
|
+
return ResponseSchema(
|
|
98
|
+
timestamp=self.timestamp,
|
|
99
|
+
status=self.status,
|
|
100
|
+
code=self.error_code,
|
|
101
|
+
path=path,
|
|
102
|
+
message=self.message,
|
|
103
|
+
)
|
fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/pagination.py-tpl
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# The module defines paginatable object schemas' helper methods.
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
from typing import Generic, List, Optional, TypeVar
|
|
5
|
-
|
|
6
|
-
from fastapi import Request
|
|
7
|
-
from pydantic import AnyHttpUrl, BaseModel, Field
|
|
8
|
-
|
|
9
|
-
T = TypeVar("T")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class PaginatedResponse(BaseModel, Generic[T]):
|
|
13
|
-
count: int = Field(
|
|
14
|
-
..., title="Count", description="Indicates the total number of imported items."
|
|
15
|
-
)
|
|
16
|
-
next: Optional[AnyHttpUrl] = Field(
|
|
17
|
-
None,
|
|
18
|
-
title="Next",
|
|
19
|
-
description="Indicates the URL of the next page to import the data.",
|
|
20
|
-
)
|
|
21
|
-
previous: Optional[AnyHttpUrl] = Field(
|
|
22
|
-
None,
|
|
23
|
-
title="Previous",
|
|
24
|
-
description="Indicates the URL of the previous page from which the data was imported.",
|
|
25
|
-
)
|
|
26
|
-
results: List[T] = Field(
|
|
27
|
-
...,
|
|
28
|
-
title="Results",
|
|
29
|
-
description="Lists the information of the imported items.",
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class Paginator:
|
|
34
|
-
def __init__(self, data: List[T], page: int, per_page: int, request: Request):
|
|
35
|
-
self.data = data
|
|
36
|
-
self.page = page
|
|
37
|
-
self.page_size = per_page
|
|
38
|
-
self.limit = per_page
|
|
39
|
-
self.offset = (page - 1) * per_page
|
|
40
|
-
self.request = request
|
|
41
|
-
self.number_of_pages = self._get_number_of_pages(len(data))
|
|
42
|
-
|
|
43
|
-
def _get_next_page(self) -> Optional[str]:
|
|
44
|
-
if self.page >= self.number_of_pages:
|
|
45
|
-
return None
|
|
46
|
-
url = self.request.url.include_query_params(page=self.page + 1)
|
|
47
|
-
return str(url)
|
|
48
|
-
|
|
49
|
-
def _get_previous_page(self) -> Optional[str]:
|
|
50
|
-
if self.page == 1:
|
|
51
|
-
return None
|
|
52
|
-
url = self.request.url.include_query_params(page=self.page - 1)
|
|
53
|
-
return str(url)
|
|
54
|
-
|
|
55
|
-
def get_response(self) -> dict:
|
|
56
|
-
paginated_data = self.data[self.offset : self.offset + self.limit]
|
|
57
|
-
return {
|
|
58
|
-
"count": len(self.data),
|
|
59
|
-
"next": self._get_next_page(),
|
|
60
|
-
"previous": self._get_previous_page(),
|
|
61
|
-
"results": paginated_data,
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
def _get_number_of_pages(self, count: int) -> int:
|
|
65
|
-
rest = count % self.page_size
|
|
66
|
-
quotient = count // self.page_size
|
|
67
|
-
return quotient if not rest else quotient + 1
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def paginate(data: List[T], page: int, page_size: int, request: Request) -> dict:
|
|
71
|
-
paginator = Paginator(data, page, page_size, request)
|
|
72
|
-
return paginator.get_response()
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# The module defines paginatable object schemas' helper methods.
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
from typing import Generic, List, Optional, TypeVar
|
|
5
|
+
|
|
6
|
+
from fastapi import Request
|
|
7
|
+
from pydantic import AnyHttpUrl, BaseModel, Field
|
|
8
|
+
|
|
9
|
+
T = TypeVar("T")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PaginatedResponse(BaseModel, Generic[T]):
|
|
13
|
+
count: int = Field(
|
|
14
|
+
..., title="Count", description="Indicates the total number of imported items."
|
|
15
|
+
)
|
|
16
|
+
next: Optional[AnyHttpUrl] = Field(
|
|
17
|
+
None,
|
|
18
|
+
title="Next",
|
|
19
|
+
description="Indicates the URL of the next page to import the data.",
|
|
20
|
+
)
|
|
21
|
+
previous: Optional[AnyHttpUrl] = Field(
|
|
22
|
+
None,
|
|
23
|
+
title="Previous",
|
|
24
|
+
description="Indicates the URL of the previous page from which the data was imported.",
|
|
25
|
+
)
|
|
26
|
+
results: List[T] = Field(
|
|
27
|
+
...,
|
|
28
|
+
title="Results",
|
|
29
|
+
description="Lists the information of the imported items.",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Paginator:
|
|
34
|
+
def __init__(self, data: List[T], page: int, per_page: int, request: Request):
|
|
35
|
+
self.data = data
|
|
36
|
+
self.page = page
|
|
37
|
+
self.page_size = per_page
|
|
38
|
+
self.limit = per_page
|
|
39
|
+
self.offset = (page - 1) * per_page
|
|
40
|
+
self.request = request
|
|
41
|
+
self.number_of_pages = self._get_number_of_pages(len(data))
|
|
42
|
+
|
|
43
|
+
def _get_next_page(self) -> Optional[str]:
|
|
44
|
+
if self.page >= self.number_of_pages:
|
|
45
|
+
return None
|
|
46
|
+
url = self.request.url.include_query_params(page=self.page + 1)
|
|
47
|
+
return str(url)
|
|
48
|
+
|
|
49
|
+
def _get_previous_page(self) -> Optional[str]:
|
|
50
|
+
if self.page == 1:
|
|
51
|
+
return None
|
|
52
|
+
url = self.request.url.include_query_params(page=self.page - 1)
|
|
53
|
+
return str(url)
|
|
54
|
+
|
|
55
|
+
def get_response(self) -> dict:
|
|
56
|
+
paginated_data = self.data[self.offset : self.offset + self.limit]
|
|
57
|
+
return {
|
|
58
|
+
"count": len(self.data),
|
|
59
|
+
"next": self._get_next_page(),
|
|
60
|
+
"previous": self._get_previous_page(),
|
|
61
|
+
"results": paginated_data,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
def _get_number_of_pages(self, count: int) -> int:
|
|
65
|
+
rest = count % self.page_size
|
|
66
|
+
quotient = count // self.page_size
|
|
67
|
+
return quotient if not rest else quotient + 1
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def paginate(data: List[T], page: int, page_size: int, request: Request) -> dict:
|
|
71
|
+
paginator = Paginator(data, page, page_size, request)
|
|
72
|
+
return paginator.get_response()
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# Main server application module
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
from fastapi import FastAPI, Request
|
|
5
|
-
from fastapi.responses import JSONResponse
|
|
6
|
-
from starlette.middleware.cors import CORSMiddleware
|
|
7
|
-
|
|
8
|
-
from src.api.api import api_router
|
|
9
|
-
from src.core.config import settings
|
|
10
|
-
from src.helper.exceptions import InternalException
|
|
11
|
-
from src.utils.documents import add_description_at_api_tags
|
|
12
|
-
|
|
13
|
-
app = FastAPI(
|
|
14
|
-
title=settings.PROJECT_NAME,
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
if settings.all_cors_origins:
|
|
18
|
-
app.add_middleware(
|
|
19
|
-
CORSMiddleware,
|
|
20
|
-
allow_origins=settings.all_cors_origins,
|
|
21
|
-
allow_credentials=True,
|
|
22
|
-
allow_methods=["*"],
|
|
23
|
-
allow_headers=["*"],
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
# Apply Custom Exception Handler
|
|
28
|
-
@app.exception_handler(InternalException)
|
|
29
|
-
async def internal_exception_handler(request: Request, exc: InternalException):
|
|
30
|
-
return JSONResponse(
|
|
31
|
-
status_code=exc.status,
|
|
32
|
-
content=exc.to_response(path=str(request.url)).model_dump(),
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
app.include_router(api_router)
|
|
37
|
-
|
|
38
|
-
# Apply Custom OpenAPI Tags description
|
|
39
|
-
add_description_at_api_tags(app)
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# Main server application module
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
from fastapi import FastAPI, Request
|
|
5
|
+
from fastapi.responses import JSONResponse
|
|
6
|
+
from starlette.middleware.cors import CORSMiddleware
|
|
7
|
+
|
|
8
|
+
from src.api.api import api_router
|
|
9
|
+
from src.core.config import settings
|
|
10
|
+
from src.helper.exceptions import InternalException
|
|
11
|
+
from src.utils.documents import add_description_at_api_tags
|
|
12
|
+
|
|
13
|
+
app = FastAPI(
|
|
14
|
+
title=settings.PROJECT_NAME,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
if settings.all_cors_origins:
|
|
18
|
+
app.add_middleware(
|
|
19
|
+
CORSMiddleware,
|
|
20
|
+
allow_origins=settings.all_cors_origins,
|
|
21
|
+
allow_credentials=True,
|
|
22
|
+
allow_methods=["*"],
|
|
23
|
+
allow_headers=["*"],
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Apply Custom Exception Handler
|
|
28
|
+
@app.exception_handler(InternalException)
|
|
29
|
+
async def internal_exception_handler(request: Request, exc: InternalException):
|
|
30
|
+
return JSONResponse(
|
|
31
|
+
status_code=exc.status,
|
|
32
|
+
content=exc.to_response(path=str(request.url)).model_dump(),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
app.include_router(api_router)
|
|
37
|
+
|
|
38
|
+
# Apply Custom OpenAPI Tags description
|
|
39
|
+
add_description_at_api_tags(app)
|
fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/mocks/mock_items.json-tpl
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"id": 1,
|
|
4
|
-
"name": "Test Item 1",
|
|
5
|
-
"price": 20.0,
|
|
6
|
-
"category": "test"
|
|
7
|
-
}
|
|
8
|
-
]
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": 1,
|
|
4
|
+
"name": "Test Item 1",
|
|
5
|
+
"price": 20.0,
|
|
6
|
+
"category": "test"
|
|
7
|
+
}
|
|
8
|
+
]
|