spakky-fastapi 0.7.17__tar.gz → 0.8.0__tar.gz
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.
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/PKG-INFO +2 -2
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/pyproject.toml +2 -2
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/jwt_auth.py +18 -15
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/middlewares/error_handling.py +16 -2
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/post_processor.py +7 -2
- spakky_fastapi-0.7.17/spakky_fastapi/routing.py → spakky_fastapi-0.8.0/spakky_fastapi/stereotypes/api_controller.py +6 -0
- spakky_fastapi-0.7.17/spakky_fastapi/stereotypes/api_controller.py +0 -9
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/README.md +0 -0
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/__init__.py +0 -0
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/error.py +0 -0
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/middlewares/__init__.py +0 -0
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/py.typed +0 -0
- {spakky_fastapi-0.7.17 → spakky_fastapi-0.8.0}/spakky_fastapi/stereotypes/__init__.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: spakky-fastapi
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.8.0
|
4
4
|
Summary: Highly abstracted Framework core to use DDD & DI/IoC & AOP & Etc...
|
5
5
|
Author: Spakky
|
6
6
|
Author-email: sejong418@icloud.com
|
@@ -11,7 +11,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.12
|
12
12
|
Requires-Dist: fastapi (>=0.109.2,<0.110.0)
|
13
13
|
Requires-Dist: orjson (>=3.9.15,<4.0.0)
|
14
|
-
Requires-Dist: spakky-core (>=0.
|
14
|
+
Requires-Dist: spakky-core (>=0.10.1)
|
15
15
|
Requires-Dist: websockets (>=12.0,<13.0)
|
16
16
|
Description-Content-Type: text/markdown
|
17
17
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "spakky-fastapi"
|
3
|
-
version = "0.
|
3
|
+
version = "0.8.0"
|
4
4
|
description = "Highly abstracted Framework core to use DDD & DI/IoC & AOP & Etc..."
|
5
5
|
authors = ["Spakky <sejong418@icloud.com>"]
|
6
6
|
readme = "README.md"
|
@@ -12,7 +12,7 @@ build-backend = "poetry.core.masonry.api"
|
|
12
12
|
[tool.poetry.dependencies]
|
13
13
|
python = ">=3.10"
|
14
14
|
fastapi = "^0.109.2"
|
15
|
-
spakky-core = ">=0.
|
15
|
+
spakky-core = ">=0.10.1"
|
16
16
|
websockets = "^12.0"
|
17
17
|
orjson = "^3.9.15"
|
18
18
|
|
@@ -31,6 +31,7 @@ IAuthenticatedFunction: TypeAlias = Callable[Concatenate[Any, JWT, P], Awaitable
|
|
31
31
|
class JWTAuth(FunctionAnnotation):
|
32
32
|
token_url: InitVar[str]
|
33
33
|
authenticator: OAuth2PasswordBearer = field(init=False)
|
34
|
+
token_keywords: list[str] = field(init=False, default_factory=list)
|
34
35
|
|
35
36
|
def __post_init__(self, token_url: str) -> None:
|
36
37
|
self.authenticator = OAuth2PasswordBearer(tokenUrl=token_url)
|
@@ -38,10 +39,10 @@ class JWTAuth(FunctionAnnotation):
|
|
38
39
|
def __call__(
|
39
40
|
self, obj: IAuthenticatedFunction[P, R_co]
|
40
41
|
) -> IAuthenticatedFunction[P, R_co]:
|
41
|
-
obj.__annotations__
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
for key, value in obj.__annotations__.items():
|
43
|
+
if value == JWT:
|
44
|
+
obj.__annotations__[key] = Annotated[JWT, Depends(self.authenticator)]
|
45
|
+
self.token_keywords.append(key)
|
45
46
|
return super().__call__(obj)
|
46
47
|
|
47
48
|
|
@@ -59,15 +60,17 @@ class AsyncJWTAuthAdvisor(IAsyncAdvisor):
|
|
59
60
|
|
60
61
|
@Around(JWTAuth.contains)
|
61
62
|
async def around_async(self, joinpoint: AsyncFunc, *args: Any, **kwargs: Any) -> Any:
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
63
|
+
annotation: JWTAuth = JWTAuth.single(joinpoint)
|
64
|
+
for keyword in annotation.token_keywords:
|
65
|
+
token: str = kwargs[keyword]
|
66
|
+
try:
|
67
|
+
jwt: JWT = JWT(token=token)
|
68
|
+
except (InvalidJWTFormatError, JWTDecodingError) as e:
|
69
|
+
raise Unauthorized(AuthenticationFailedError()) from e
|
70
|
+
if jwt.is_expired:
|
71
|
+
raise Unauthorized(AuthenticationFailedError())
|
72
|
+
if jwt.verify(self.__key) is False:
|
73
|
+
raise Unauthorized(AuthenticationFailedError())
|
74
|
+
self.__logger.info(f"[{type(self).__name__}] {jwt.payload!r}")
|
75
|
+
kwargs[keyword] = jwt
|
73
76
|
return await joinpoint(*args, **kwargs)
|
@@ -3,9 +3,10 @@ from typing import Callable, Awaitable, TypeAlias
|
|
3
3
|
from fastapi import Request
|
4
4
|
from fastapi.responses import ORJSONResponse
|
5
5
|
from pydantic import BaseModel
|
6
|
-
from
|
7
|
-
from starlette.middleware.base import BaseHTTPMiddleware
|
6
|
+
from starlette.middleware.base import BaseHTTPMiddleware, DispatchFunction
|
8
7
|
from starlette.responses import Response
|
8
|
+
from starlette.types import ASGIApp
|
9
|
+
from spakky_fastapi.error import InternalServerError, SpakkyFastAPIError
|
9
10
|
|
10
11
|
Next: TypeAlias = Callable[[Request], Awaitable[Response]]
|
11
12
|
|
@@ -13,9 +14,21 @@ Next: TypeAlias = Callable[[Request], Awaitable[Response]]
|
|
13
14
|
class ErrorResponse(BaseModel):
|
14
15
|
message: str
|
15
16
|
args: list[str]
|
17
|
+
traceback: str = ""
|
16
18
|
|
17
19
|
|
18
20
|
class ErrorHandlingMiddleware(BaseHTTPMiddleware):
|
21
|
+
__debug: bool
|
22
|
+
|
23
|
+
def __init__(
|
24
|
+
self,
|
25
|
+
app: ASGIApp,
|
26
|
+
dispatch: DispatchFunction | None = None,
|
27
|
+
debug: bool = False,
|
28
|
+
) -> None:
|
29
|
+
super().__init__(app, dispatch)
|
30
|
+
self.__debug = debug
|
31
|
+
|
19
32
|
async def dispatch(self, request: Request, call_next: Next) -> Response:
|
20
33
|
try:
|
21
34
|
return await call_next(request)
|
@@ -34,6 +47,7 @@ class ErrorHandlingMiddleware(BaseHTTPMiddleware):
|
|
34
47
|
content=ErrorResponse(
|
35
48
|
message=error.message,
|
36
49
|
args=[str(x) for x in error.args],
|
50
|
+
traceback=error.traceback if self.__debug else "",
|
37
51
|
).model_dump(),
|
38
52
|
status_code=error.status_code,
|
39
53
|
)
|
@@ -8,8 +8,11 @@ from fastapi.exceptions import FastAPIError
|
|
8
8
|
from fastapi.utils import create_response_field # type: ignore
|
9
9
|
from spakky.bean.interfaces.bean_container import IBeanContainer
|
10
10
|
from spakky.bean.interfaces.bean_processor import IBeanPostProcessor
|
11
|
-
from spakky_fastapi.
|
12
|
-
|
11
|
+
from spakky_fastapi.stereotypes.api_controller import (
|
12
|
+
ApiController,
|
13
|
+
Route,
|
14
|
+
WebSocketRoute,
|
15
|
+
)
|
13
16
|
|
14
17
|
|
15
18
|
class FastAPIBeanPostProcessor(IBeanPostProcessor):
|
@@ -32,6 +35,7 @@ class FastAPIBeanPostProcessor(IBeanPostProcessor):
|
|
32
35
|
if route is None and websocket_route is None:
|
33
36
|
continue
|
34
37
|
if route is not None:
|
38
|
+
# pylint: disable=line-too-long
|
35
39
|
self.__logger.info(
|
36
40
|
f"[{type(self).__name__}] {route.methods!r} {controller.prefix}{route.path} -> {method.__qualname__}"
|
37
41
|
)
|
@@ -50,6 +54,7 @@ class FastAPIBeanPostProcessor(IBeanPostProcessor):
|
|
50
54
|
route.response_model = return_annotation
|
51
55
|
router.add_api_route(endpoint=method, **asdict(route))
|
52
56
|
if websocket_route is not None:
|
57
|
+
# pylint: disable=line-too-long
|
53
58
|
self.__logger.info(
|
54
59
|
f"[{type(self).__name__}] [WebSocket] {controller.prefix}{websocket_route.path} -> {method.__qualname__}"
|
55
60
|
)
|
@@ -7,6 +7,7 @@ from fastapi.responses import JSONResponse
|
|
7
7
|
from fastapi.routing import APIRoute
|
8
8
|
from spakky.core.annotation import FunctionAnnotation
|
9
9
|
from spakky.core.types import FuncT
|
10
|
+
from spakky.stereotype.controller import Controller
|
10
11
|
from starlette.routing import Route as StarletteRoute
|
11
12
|
|
12
13
|
SetIntStr: TypeAlias = set[int | str]
|
@@ -500,3 +501,8 @@ def websocket(
|
|
500
501
|
name=name,
|
501
502
|
dependencies=dependencies,
|
502
503
|
)
|
504
|
+
|
505
|
+
|
506
|
+
@dataclass
|
507
|
+
class ApiController(Controller):
|
508
|
+
tags: list[str | Enum] | None = None
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|