qena-shared-lib 0.1.4__py3-none-any.whl → 0.1.6__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.
@@ -1,9 +1,10 @@
1
+ from collections.abc import Iterable
1
2
  from typing import Any
2
3
 
3
4
  from fastapi import Request, Response, status
4
- from fastapi.encoders import jsonable_encoder
5
5
  from fastapi.exceptions import RequestValidationError
6
6
  from fastapi.responses import JSONResponse
7
+ from pydantic_core import to_jsonable_python
7
8
 
8
9
  from .dependencies.http import get_service
9
10
  from .exceptions import ServiceException, Severity
@@ -93,7 +94,19 @@ def handle_service_exception(
93
94
  content["correctiveAction"] = exception.corrective_action
94
95
 
95
96
  if exception.body is not None:
96
- content.update(dict(exception.body))
97
+ extra_body = to_jsonable_python(exception.body)
98
+ is_updated = False
99
+
100
+ try:
101
+ if isinstance(extra_body, Iterable):
102
+ content.update(extra_body)
103
+
104
+ is_updated = True
105
+ except:
106
+ pass
107
+
108
+ if not is_updated:
109
+ content["data"] = extra_body
97
110
 
98
111
  return JSONResponse(
99
112
  content=content,
@@ -117,7 +130,7 @@ def handle_request_validation_error(
117
130
  "severity": Severity.MEDIUM.name,
118
131
  "message": message,
119
132
  "code": 100,
120
- "detail": jsonable_encoder(error.errors()),
133
+ "detail": to_jsonable_python(error.errors()),
121
134
  },
122
135
  status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
123
136
  )
@@ -21,11 +21,10 @@ from pydantic_core import from_json, to_json
21
21
  from ..dependencies.miscellaneous import validate_annotation
22
22
  from ..logging import LoggerProvider
23
23
  from ..logstash import BaseLogstashSender
24
- from ..utils import AsyncEventLoopMixin
24
+ from ..utils import AsyncEventLoopMixin, TypeAdapterCache
25
25
  from ._channel import BaseChannel
26
26
  from ._exceptions import RabbitMQException
27
27
  from ._pool import ChannelPool
28
- from ._utils import TypeAdapterCache
29
28
 
30
29
  __all__ = [
31
30
  "BackoffRetryDelay",
@@ -13,10 +13,9 @@ from prometheus_client import Counter, Summary
13
13
  from pydantic_core import from_json, to_json
14
14
 
15
15
  from ..logging import LoggerProvider
16
- from ..utils import AsyncEventLoopMixin
16
+ from ..utils import AsyncEventLoopMixin, TypeAdapterCache
17
17
  from ._exceptions import RabbitMQException
18
18
  from ._pool import ChannelPool
19
- from ._utils import TypeAdapterCache
20
19
 
21
20
  __all__ = ["RpcClient"]
22
21
 
@@ -92,11 +92,7 @@ class UserInfo(BaseModel):
92
92
  async def extract_user_info(
93
93
  jwt_adapter: Annotated[JwtAdapter, DependsOn(JwtAdapter)],
94
94
  token: Annotated[
95
- str | None,
96
- Header(
97
- alias=environ.get("TOKEN_HEADER") or "authorization",
98
- include_in_schema=False,
99
- ),
95
+ str | None, Header(alias=environ.get("TOKEN_HEADER") or "authorization")
100
96
  ] = None,
101
97
  user_agent: Annotated[
102
98
  str | None, Header(alias="user-agent", include_in_schema=False)
qena_shared_lib/utils.py CHANGED
@@ -1,6 +1,8 @@
1
1
  from asyncio import AbstractEventLoop, get_running_loop
2
2
 
3
- __all__ = ["AsyncEventLoopMixin"]
3
+ from pydantic import TypeAdapter
4
+
5
+ __all__ = ["AsyncEventLoopMixin", "TypeAdapterCache"]
4
6
 
5
7
  ABSTRACT_EVENT_LOOP_ATTRIBUTE = "__abstract_event_loop__"
6
8
 
@@ -26,3 +28,18 @@ class AsyncEventLoopMixin:
26
28
  setattr(self, ABSTRACT_EVENT_LOOP_ATTRIBUTE, current_running_loop)
27
29
 
28
30
  return getattr(self, ABSTRACT_EVENT_LOOP_ATTRIBUTE)
31
+
32
+
33
+ class TypeAdapterCache:
34
+ _cache = {}
35
+
36
+ @classmethod
37
+ def cache_annotation(cls, annotation: type):
38
+ if annotation not in cls._cache:
39
+ cls._cache[annotation] = TypeAdapter(annotation)
40
+
41
+ @classmethod
42
+ def get_type_adapter(cls, annotation: type) -> TypeAdapter:
43
+ cls.cache_annotation(annotation)
44
+
45
+ return cls._cache[annotation]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: qena-shared-lib
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A shared tools for other services
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: cronsim~=2.0
@@ -1,14 +1,14 @@
1
1
  qena_shared_lib/__init__.py,sha256=WokKEFaMNow6h2ZY_fyB-tiYnic-Ed6K6kyzKgg3Rlw,371
2
2
  qena_shared_lib/application.py,sha256=2bpoEHW9FXRjNgBUuij2djfTLbhk01zymqp6kwasRq0,5470
3
3
  qena_shared_lib/background.py,sha256=upWeJ747-4S4xsBOR-P2_oNar1YiaxCpnLhEDvnArmQ,3097
4
- qena_shared_lib/exception_handlers.py,sha256=od_4g2Ro0HTeiIip_Saq6OCW0FMNjwLuaGevVWRPwFo,4824
4
+ qena_shared_lib/exception_handlers.py,sha256=GclZUv8_QuJtbVSkzux8hxNbZNG7DWI2u0oGXgaEMD4,5143
5
5
  qena_shared_lib/exceptions.py,sha256=cNeksC8ZavKgoqKLBik1NR-lCcdLZzITHMXww5deY5Y,6789
6
6
  qena_shared_lib/http.py,sha256=90bmDfs522KxKBj2o0vO-MgmDqa3EZOfaNUFp1DuUdQ,23864
7
7
  qena_shared_lib/logging.py,sha256=JL6bAmkK1BJA84rZNpvDEmrec3dogQRpSug4fj1Omkw,1618
8
8
  qena_shared_lib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  qena_shared_lib/scheduler.py,sha256=qxM3zdKJnAKUWw8i_I2M5o3uL_fEOTaBzYUBL-HvUzg,11874
10
- qena_shared_lib/security.py,sha256=cL3I75tF9jSBxjw-1fzQ-YXfkEXYyjMhKQsfXrh-1sA,6141
11
- qena_shared_lib/utils.py,sha256=bvPnjaFx7npVGSUO6IKdpiAHfUECsGRCeDW8UOjVckU,845
10
+ qena_shared_lib/security.py,sha256=n872UE3eQfezo1lkHwz5hcAngO0VekA_qDQ9LsZ8JZ8,6072
11
+ qena_shared_lib/utils.py,sha256=y60AKFknK-vus4i-nesgL00BWuAXJyd8Qx79Gi7lRq4,1272
12
12
  qena_shared_lib/dependencies/__init__.py,sha256=W12RgJbhqZ9GiSV1nLlHmpwPzvQv8t7f4JEoazM_WYg,350
13
13
  qena_shared_lib/dependencies/http.py,sha256=3KopQjq05qbYROMPZjji2Zz7qQLU6_2u3qKisLrjLks,1469
14
14
  qena_shared_lib/dependencies/miscellaneous.py,sha256=iGwAjatXb_JVSF13n1vdTRAgSKv19VtHo9ZbjjbkIco,753
@@ -21,11 +21,10 @@ qena_shared_lib/rabbitmq/_base.py,sha256=rOUpYK3FJu3U5rU4vF--31c_Ycpnt4FL9qGkZNG
21
21
  qena_shared_lib/rabbitmq/_channel.py,sha256=R0xzZvLkeE4YWsyLhx8bPDramsTFDZIcCgfsDyFmSB4,5429
22
22
  qena_shared_lib/rabbitmq/_exception_handlers.py,sha256=7-WguEUqUSIdbvCIWn10f2CRaWPCM3MDZ6sxSATCCYA,4762
23
23
  qena_shared_lib/rabbitmq/_exceptions.py,sha256=Mg56Uk4eBEwTnWC91_WPS3_8d-yaqNkbb_32IpX_R-c,1208
24
- qena_shared_lib/rabbitmq/_listener.py,sha256=1c6Qrz87hL10kaGobTZhI48v3ou88A5u371QdkyZIXA,44153
24
+ qena_shared_lib/rabbitmq/_listener.py,sha256=eYrlaptPoc72y7UMRyo_Lnn_OE0HGVlBJIh5UEptnww,44134
25
25
  qena_shared_lib/rabbitmq/_pool.py,sha256=1_ftW0D3sPHI6v5c72oA03s05up4-1QqZ7d8icnZeNs,1951
26
26
  qena_shared_lib/rabbitmq/_publisher.py,sha256=48JdpvNzFaw3Bw5LhunFxcjm3qNWz3AXKyXtcpzcohQ,2380
27
- qena_shared_lib/rabbitmq/_rpc_client.py,sha256=E-J5PIfA1T3j1y9KoxjNTzSLdK95MbaePulNxiqGb-M,8724
28
- qena_shared_lib/rabbitmq/_utils.py,sha256=5WZjYm4MdDrjL1dsrZuT8eKKiWfUef29_FJ262-hfSM,438
29
- qena_shared_lib-0.1.4.dist-info/METADATA,sha256=Z1cefuDH6Kwx2RDb2sRpE_cDfIvpBbthMIcOl58gpKw,8520
30
- qena_shared_lib-0.1.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
31
- qena_shared_lib-0.1.4.dist-info/RECORD,,
27
+ qena_shared_lib/rabbitmq/_rpc_client.py,sha256=5hA8IWvydRufYdv0zViIsi6_W2tWkHcmZotgCV2yjfc,8705
28
+ qena_shared_lib-0.1.6.dist-info/METADATA,sha256=rOQnxhn5ilC37cQKUv5otm-ML0crCpbRB-2RAr7rNpc,8520
29
+ qena_shared_lib-0.1.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
30
+ qena_shared_lib-0.1.6.dist-info/RECORD,,
@@ -1,18 +0,0 @@
1
- from pydantic import TypeAdapter
2
-
3
- __all__ = ["TypeAdapterCache"]
4
-
5
-
6
- class TypeAdapterCache:
7
- _cache = {}
8
-
9
- @classmethod
10
- def cache_annotation(cls, annotation: type):
11
- if annotation not in cls._cache:
12
- cls._cache[annotation] = TypeAdapter(annotation)
13
-
14
- @classmethod
15
- def get_type_adapter(cls, annotation: type) -> TypeAdapter:
16
- cls.cache_annotation(annotation)
17
-
18
- return cls._cache[annotation]