spakky-fastapi 0.7.19__tar.gz → 0.10.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: spakky-fastapi
3
- Version: 0.7.19
3
+ Version: 0.10.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.7.14)
14
+ Requires-Dist: spakky-core (>=0.12,<0.13)
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.7.19"
3
+ version = "0.10.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.7.14"
15
+ spakky-core = "^0.12"
16
16
  websockets = "^12.0"
17
17
  orjson = "^3.9.15"
18
18
 
@@ -33,21 +33,21 @@ pytest-asyncio = "^0.21.1"
33
33
 
34
34
  [tool.black]
35
35
  line-length = 90
36
- target-version = ["py310"]
36
+ target-version = ["py312"]
37
37
  include = '\.pyi?$'
38
38
 
39
39
  [tool.isort]
40
40
  py_version = 311
41
- src_paths = ["spakky", "tests"]
42
- skip = [".gitignore", ".dockerignore"]
41
+ src_paths = ["spakky_fastapi", "tests"]
42
+ skip = [".venv", ".gitignore", ".dockerignore"]
43
43
  extend_skip = [".md", ".json", ".txt"]
44
44
  profile = "black"
45
45
  combine_as_imports = true
46
46
  remove_redundant_aliases = true
47
47
  length_sort_sections = ["future", "stdlib"]
48
48
 
49
- [tool.pylint.MASTER]
50
- ignore-patterns = "test_.*?py"
49
+ [tool.pylint]
50
+ ignore-paths = [".venv", "tests"]
51
51
 
52
52
  [tool.pylint.'MESSAGES CONTROL']
53
53
  max-line-length = 90
@@ -63,7 +63,10 @@ disable = """
63
63
  too-few-public-methods,
64
64
  unnecessary-ellipsis,
65
65
  redefined-builtin,
66
- too-many-locals
66
+ too-many-locals,
67
+ multiple-statements,
68
+ consider-using-f-string,
69
+ duplicate-code,
67
70
  """
68
71
 
69
72
  [tool.pytest.ini_options]
@@ -1,20 +1,21 @@
1
1
  from typing import Any, TypeVar, Callable, Annotated, Awaitable, TypeAlias, Concatenate
2
+ from inspect import iscoroutinefunction
2
3
  from logging import Logger
3
4
  from dataclasses import InitVar, field, dataclass
4
5
 
5
6
  from fastapi import Depends
6
7
  from fastapi.security import OAuth2PasswordBearer
7
8
  from spakky.aop.advice import Around
8
- from spakky.aop.advisor import IAsyncAdvisor
9
- from spakky.aop.aspect import AsyncAspect
9
+ from spakky.aop.advisor import IAdvisor, IAsyncAdvisor
10
+ from spakky.aop.aspect import Aspect, AsyncAspect
10
11
  from spakky.aop.error import SpakkyAOPError
11
12
  from spakky.aop.order import Order
12
- from spakky.bean.autowired import autowired
13
13
  from spakky.core.annotation import FunctionAnnotation
14
- from spakky.core.types import AsyncFunc, P
14
+ from spakky.core.types import AsyncFunc, Func, P
15
15
  from spakky.cryptography.error import InvalidJWTFormatError, JWTDecodingError
16
16
  from spakky.cryptography.jwt import JWT
17
17
  from spakky.cryptography.key import Key
18
+
18
19
  from spakky_fastapi.error import Unauthorized
19
20
 
20
21
  R_co = TypeVar("R_co", covariant=True)
@@ -24,7 +25,10 @@ class AuthenticationFailedError(SpakkyAOPError):
24
25
  message = "사용자 인증에 실패했습니다."
25
26
 
26
27
 
27
- IAuthenticatedFunction: TypeAlias = Callable[Concatenate[Any, JWT, P], Awaitable[R_co]]
28
+ IAuthenticatedFunction: TypeAlias = (
29
+ Callable[Concatenate[Any, JWT, P], R_co]
30
+ | Callable[Concatenate[Any, JWT, P], Awaitable[R_co]]
31
+ )
28
32
 
29
33
 
30
34
  @dataclass
@@ -46,19 +50,47 @@ class JWTAuth(FunctionAnnotation):
46
50
  return super().__call__(obj)
47
51
 
48
52
 
53
+ @Order(1)
54
+ @Aspect()
55
+ class JWTAuthAdvisor(IAdvisor):
56
+ __logger: Logger
57
+ __key: Key
58
+
59
+ def __init__(self, logger: Logger, key: Key) -> None:
60
+ super().__init__()
61
+ self.__logger = logger
62
+ self.__key = key
63
+
64
+ @Around(lambda x: JWTAuth.contains(x) and not iscoroutinefunction(x))
65
+ def around(self, joinpoint: Func, *args: Any, **kwargs: Any) -> Any:
66
+ annotation: JWTAuth = JWTAuth.single(joinpoint)
67
+ for keyword in annotation.token_keywords:
68
+ token: str = kwargs[keyword]
69
+ try:
70
+ jwt: JWT = JWT(token=token)
71
+ except (InvalidJWTFormatError, JWTDecodingError) as e:
72
+ raise Unauthorized(AuthenticationFailedError()) from e
73
+ if jwt.is_expired:
74
+ raise Unauthorized(AuthenticationFailedError())
75
+ if jwt.verify(self.__key) is False:
76
+ raise Unauthorized(AuthenticationFailedError())
77
+ self.__logger.info(f"[{type(self).__name__}] {jwt.payload!r}")
78
+ kwargs[keyword] = jwt
79
+ return joinpoint(*args, **kwargs)
80
+
81
+
49
82
  @Order(1)
50
83
  @AsyncAspect()
51
84
  class AsyncJWTAuthAdvisor(IAsyncAdvisor):
52
85
  __logger: Logger
53
86
  __key: Key
54
87
 
55
- @autowired
56
88
  def __init__(self, logger: Logger, key: Key) -> None:
57
89
  super().__init__()
58
90
  self.__logger = logger
59
91
  self.__key = key
60
92
 
61
- @Around(JWTAuth.contains)
93
+ @Around(lambda x: JWTAuth.contains(x) and iscoroutinefunction(x))
62
94
  async def around_async(self, joinpoint: AsyncFunc, *args: Any, **kwargs: Any) -> Any:
63
95
  annotation: JWTAuth = JWTAuth.single(joinpoint)
64
96
  for keyword in annotation.token_keywords:
@@ -3,9 +3,11 @@ 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 spakky_fastapi.error import InternalServerError, SpakkyFastAPIError
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
+
10
+ from spakky_fastapi.error import InternalServerError, SpakkyFastAPIError
9
11
 
10
12
  Next: TypeAlias = Callable[[Request], Awaitable[Response]]
11
13
 
@@ -13,9 +15,21 @@ Next: TypeAlias = Callable[[Request], Awaitable[Response]]
13
15
  class ErrorResponse(BaseModel):
14
16
  message: str
15
17
  args: list[str]
18
+ traceback: str = ""
16
19
 
17
20
 
18
21
  class ErrorHandlingMiddleware(BaseHTTPMiddleware):
22
+ __debug: bool
23
+
24
+ def __init__(
25
+ self,
26
+ app: ASGIApp,
27
+ dispatch: DispatchFunction | None = None,
28
+ debug: bool = False,
29
+ ) -> None:
30
+ super().__init__(app, dispatch)
31
+ self.__debug = debug
32
+
19
33
  async def dispatch(self, request: Request, call_next: Next) -> Response:
20
34
  try:
21
35
  return await call_next(request)
@@ -34,6 +48,7 @@ class ErrorHandlingMiddleware(BaseHTTPMiddleware):
34
48
  content=ErrorResponse(
35
49
  message=error.message,
36
50
  args=[str(x) for x in error.args],
51
+ traceback=error.traceback if self.__debug else "",
37
52
  ).model_dump(),
38
53
  status_code=error.status_code,
39
54
  )
@@ -0,0 +1,24 @@
1
+ from logging import Logger
2
+
3
+ from fastapi import FastAPI
4
+ from spakky.application.interfaces.pluggable import IPluggable
5
+ from spakky.application.interfaces.registry import IRegistry
6
+
7
+ from spakky_fastapi.post_processor import FastAPIBeanPostProcessor
8
+
9
+
10
+ class FastAPIPlugin(IPluggable):
11
+ app: FastAPI
12
+ logger: Logger
13
+
14
+ def __init__(self, app: FastAPI, logger: Logger) -> None:
15
+ self.app = app
16
+ self.logger = logger
17
+
18
+ def register(self, registry: IRegistry) -> None:
19
+ registry.register_bean_post_processor(
20
+ FastAPIBeanPostProcessor(
21
+ self.app,
22
+ self.logger,
23
+ )
24
+ )
@@ -0,0 +1,10 @@
1
+ from spakky.application.interfaces.pluggable import IPluggable
2
+ from spakky.application.interfaces.registry import IRegistry
3
+
4
+ from spakky_fastapi.aspects.jwt_auth import AsyncJWTAuthAdvisor, JWTAuthAdvisor
5
+
6
+
7
+ class JWTAuthPlugin(IPluggable):
8
+ def register(self, registry: IRegistry) -> None:
9
+ registry.register_bean(JWTAuthAdvisor)
10
+ registry.register_bean(AsyncJWTAuthAdvisor)
@@ -6,10 +6,14 @@ from dataclasses import asdict
6
6
  from fastapi import APIRouter, FastAPI
7
7
  from fastapi.exceptions import FastAPIError
8
8
  from fastapi.utils import create_response_field # type: ignore
9
- from spakky.bean.interfaces.bean_container import IBeanContainer
10
- from spakky.bean.interfaces.bean_processor import IBeanPostProcessor
11
- from spakky_fastapi.routing import Route, WebSocketRoute
12
- from spakky_fastapi.stereotypes.api_controller import ApiController
9
+ from spakky.application.interfaces.bean_container import IBeanContainer
10
+ from spakky.application.interfaces.bean_processor import IBeanPostProcessor
11
+
12
+ from spakky_fastapi.stereotypes.api_controller import (
13
+ ApiController,
14
+ Route,
15
+ WebSocketRoute,
16
+ )
13
17
 
14
18
 
15
19
  class FastAPIBeanPostProcessor(IBeanPostProcessor):
@@ -32,6 +36,7 @@ class FastAPIBeanPostProcessor(IBeanPostProcessor):
32
36
  if route is None and websocket_route is None:
33
37
  continue
34
38
  if route is not None:
39
+ # pylint: disable=line-too-long
35
40
  self.__logger.info(
36
41
  f"[{type(self).__name__}] {route.methods!r} {controller.prefix}{route.path} -> {method.__qualname__}"
37
42
  )
@@ -50,6 +55,7 @@ class FastAPIBeanPostProcessor(IBeanPostProcessor):
50
55
  route.response_model = return_annotation
51
56
  router.add_api_route(endpoint=method, **asdict(route))
52
57
  if websocket_route is not None:
58
+ # pylint: disable=line-too-long
53
59
  self.__logger.info(
54
60
  f"[{type(self).__name__}] [WebSocket] {controller.prefix}{websocket_route.path} -> {method.__qualname__}"
55
61
  )
File without changes
@@ -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
@@ -1,9 +0,0 @@
1
- from enum import Enum
2
- from dataclasses import dataclass
3
-
4
- from spakky.stereotype.controller import Controller
5
-
6
-
7
- @dataclass
8
- class ApiController(Controller):
9
- tags: list[str | Enum] | None = None