starmallow 0.6.3__py3-none-any.whl → 0.6.5__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.
starmallow/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "0.6.3"
1
+ __version__ = "0.6.5"
2
2
 
3
3
  from .applications import StarMallow
4
4
  from .exceptions import RequestValidationError
@@ -0,0 +1,29 @@
1
+ import typing
2
+ from logging import getLogger
3
+
4
+ from starlette.background import BackgroundTask as StarletteBackgroundTask
5
+ from starlette.background import BackgroundTasks as StarletteBackgroundTasks
6
+ from starlette.background import P
7
+ from starlette.concurrency import run_in_threadpool
8
+
9
+ logger = getLogger(__name__)
10
+
11
+
12
+ class BackgroundTask(StarletteBackgroundTask):
13
+
14
+ async def __call__(self) -> None:
15
+ try:
16
+ if self.is_async:
17
+ await self.func(*self.args, **self.kwargs)
18
+ else:
19
+ await run_in_threadpool(self.func, *self.args, **self.kwargs)
20
+ except BaseException as e:
21
+ logger.exception(f'Background Task {self.func.__module__}.{self.func.__name__} failed: {e}')
22
+
23
+
24
+ class BackgroundTasks(StarletteBackgroundTasks):
25
+ def add_task(
26
+ self, func: typing.Callable[P, typing.Any], *args: P.args, **kwargs: P.kwargs
27
+ ) -> None:
28
+ task = BackgroundTask(func, *args, **kwargs)
29
+ self.tasks.append(task)
@@ -8,13 +8,14 @@ import marshmallow as ma
8
8
  import marshmallow.fields as mf
9
9
  from marshmallow.error_store import ErrorStore
10
10
  from marshmallow.utils import missing as missing_
11
- from starlette.background import BackgroundTasks
11
+ from starlette.background import BackgroundTasks as StarletteBackgroundTasks
12
12
  from starlette.datastructures import FormData, Headers, QueryParams
13
13
  from starlette.exceptions import HTTPException
14
14
  from starlette.requests import HTTPConnection, Request
15
15
  from starlette.responses import Response
16
16
  from starlette.websockets import WebSocket
17
17
 
18
+ from starmallow.background import BackgroundTasks
18
19
  from starmallow.params import Param, ParamType, ResolvedParam
19
20
  from starmallow.utils import (
20
21
  is_async_gen_callable,
@@ -113,7 +114,7 @@ def request_params_to_args(
113
114
  async def resolve_basic_args(
114
115
  request: Request | WebSocket,
115
116
  response: Response,
116
- background_tasks: BackgroundTasks,
117
+ background_tasks: StarletteBackgroundTasks,
117
118
  params: Dict[ParamType, Dict[str, Param]],
118
119
  ):
119
120
  path_values, path_errors = request_params_to_args(
@@ -183,7 +184,7 @@ async def resolve_basic_args(
183
184
  values[param_name] = request
184
185
  elif lenient_issubclass(param_type, Response):
185
186
  values[param_name] = response
186
- elif lenient_issubclass(param_type, BackgroundTasks):
187
+ elif lenient_issubclass(param_type, StarletteBackgroundTasks):
187
188
  values[param_name] = background_tasks
188
189
 
189
190
  return values, errors
@@ -217,7 +218,7 @@ async def call_resolver(
217
218
  async def resolve_subparams(
218
219
  request: Request | WebSocket,
219
220
  response: Response,
220
- background_tasks: BackgroundTasks,
221
+ background_tasks: StarletteBackgroundTasks,
221
222
  params: Dict[str, ResolvedParam],
222
223
  dependency_cache: Optional[Dict[Tuple[Callable[..., Any], Tuple[str]], Any]],
223
224
  ) -> Dict[str, Any]:
@@ -250,10 +251,10 @@ async def resolve_subparams(
250
251
  async def resolve_params(
251
252
  request: Request | WebSocket,
252
253
  params: Dict[ParamType, Dict[str, Param]],
253
- background_tasks: Optional[BackgroundTasks] = None,
254
+ background_tasks: Optional[StarletteBackgroundTasks] = None,
254
255
  response: Optional[Response] = None,
255
256
  dependency_cache: Optional[Dict[Tuple[Callable[..., Any], Tuple[str]], Any]] = None,
256
- ) -> Tuple[Dict[str, Any], Dict[str, Union[Any, List, Dict]], BackgroundTasks, Response]:
257
+ ) -> Tuple[Dict[str, Any], Dict[str, Union[Any, List, Dict]], StarletteBackgroundTasks, Response]:
257
258
  dependency_cache = dependency_cache or {}
258
259
 
259
260
  if response is None:
starmallow/routing.py CHANGED
@@ -96,7 +96,12 @@ def request_response(
96
96
  response = await run_in_threadpool(func, request)
97
97
  await response(scope, receive, send)
98
98
 
99
- await wrap_app_handling_exceptions(app, request)(scope, receive, send)
99
+ try:
100
+ await wrap_app_handling_exceptions(app, request)(scope, receive, send)
101
+ except RuntimeError as e:
102
+ # This likely means that the exception was thrown by a background task
103
+ # after the response has been successfully send to the client
104
+ logger.exception(f'Runtime error occurred: {e}')
100
105
 
101
106
  return app
102
107
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: starmallow
3
- Version: 0.6.3
3
+ Version: 0.6.5
4
4
  Summary: StarMallow framework
5
5
  Project-URL: Homepage, https://github.com/mvanderlee/starmallow
6
6
  Author-email: Michiel Vanderlee <jmt.vanderlee@gmail.com>
@@ -1,5 +1,6 @@
1
- starmallow/__init__.py,sha256=A9KiNmL4PORVsksM-GGh_4ohWvgxkq5cHhHTdiDscDA,322
1
+ starmallow/__init__.py,sha256=BM647YLqYd8UyAgINXcC9B8aSFtZNYWdO3Gridcnn2s,322
2
2
  starmallow/applications.py,sha256=mSL4YDozP8n6v22g4NX7EAMXmGhzzhtjtZd68YHcFvw,31720
3
+ starmallow/background.py,sha256=qxT6-9SfnkcGZzvecNOpIsCOMW0TPwDtpQ81GHI28P0,995
3
4
  starmallow/concurrency.py,sha256=MVRjo4Vqss_yqhaoeVt3xb7rLaSuAq_q9uYgTwbsojE,1375
4
5
  starmallow/constants.py,sha256=u0h8cJKhJY0oIZqzr7wpEZG2bPLrw5FroMnn3d8KBNQ,129
5
6
  starmallow/dataclasses.py,sha256=ap9DInvQjH2AyI4MAAnbDEuNnbPb94PigaNmEb7AQU8,2658
@@ -13,10 +14,10 @@ starmallow/exception_handlers.py,sha256=gr2qLYWEtsIEH28n7OreEiiLVz6Y7b6osRyS9esJ
13
14
  starmallow/exceptions.py,sha256=vabtPJkTmtCdC8_2OPBE8Osz0v0KxaSOX6IWf1jgNkc,872
14
15
  starmallow/fields.py,sha256=arrTabCYoJFZcoY69EZTBH3YUg7CUSr3-zYLiAjYLTM,1238
15
16
  starmallow/params.py,sha256=MJzUzUs6GEyrbpDZ1r8To8vR-QwpopdxDStq802o5Ug,8662
16
- starmallow/request_resolver.py,sha256=JoCxqdDYyniwNsI6FeeYKB7Muj9xDQ8LEA2ICba8n24,11012
17
+ starmallow/request_resolver.py,sha256=frOxhQPxhWKnStJDTVrs5wu6Icf3dTbbv9VblegxS3A,11135
17
18
  starmallow/requests.py,sha256=o_yNhH9WJ35uAGuoXa5_gihevHeaveOvkP525dbwQSU,843
18
19
  starmallow/responses.py,sha256=k2pf_m21ykf_FECdODUz400pMucMJJf_Zm8TXFujvaU,2012
19
- starmallow/routing.py,sha256=Uxnlkl4eIs56TZ8VAgNmlh-K89ku1Xu_ROR5S8x-rkg,45128
20
+ starmallow/routing.py,sha256=VSotmrEerVzuUfn20mpmSbuRVS4XiHrPtNRvBP8KJ4M,45397
20
21
  starmallow/schema_generator.py,sha256=yi368FwF9B50ZHSNOG0rvYVirVUeMFq2kXkUDeJUz4w,17961
21
22
  starmallow/serializers.py,sha256=rBEKMNgONgz_bai12uDvAEMCI_aEFGsqMSeIoWtlrOI,12514
22
23
  starmallow/types.py,sha256=8GXWjvzXQhF5NMHf14fbid6uErxVd1Xk_w2I4FoUgZ4,717
@@ -34,7 +35,7 @@ starmallow/security/http.py,sha256=cpGjM1kFDq3i_bOY96kMkf4cspBUxFkkET9lTK3NA-0,6
34
35
  starmallow/security/oauth2.py,sha256=1nv1580PY4cwgu5gzpQCf2MfMNv2Cfv05753AUHPOhQ,10005
35
36
  starmallow/security/open_id_connect_url.py,sha256=IPsL2YzWc2mPwJbrUn6oFRTi7uRAG6mR62CGwmzBs1k,1399
36
37
  starmallow/security/utils.py,sha256=bd8T0YM7UQD5ATKucr1bNtAvz_Y3__dVNAv5UebiPvc,293
37
- starmallow-0.6.3.dist-info/METADATA,sha256=ccZtRpeODLBXd3pJPyZ-Bsk_w0mxZvmoOtkRsLDvBm8,5615
38
- starmallow-0.6.3.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
39
- starmallow-0.6.3.dist-info/licenses/LICENSE.md,sha256=QelyGgOzch8CXzy6HrYwHh7nmj0rlWkDA0YzmZ3CPaY,1084
40
- starmallow-0.6.3.dist-info/RECORD,,
38
+ starmallow-0.6.5.dist-info/METADATA,sha256=cAAboTKZYPwxOZEqzjIxZx6lfcMlRLPbee8vRRAQkfo,5615
39
+ starmallow-0.6.5.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
40
+ starmallow-0.6.5.dist-info/licenses/LICENSE.md,sha256=QelyGgOzch8CXzy6HrYwHh7nmj0rlWkDA0YzmZ3CPaY,1084
41
+ starmallow-0.6.5.dist-info/RECORD,,