python3-commons 0.6.10__py3-none-any.whl → 0.6.12__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.
- python3_commons/api_client.py +53 -16
- {python3_commons-0.6.10.dist-info → python3_commons-0.6.12.dist-info}/METADATA +1 -1
- {python3_commons-0.6.10.dist-info → python3_commons-0.6.12.dist-info}/RECORD +7 -7
- {python3_commons-0.6.10.dist-info → python3_commons-0.6.12.dist-info}/AUTHORS.rst +0 -0
- {python3_commons-0.6.10.dist-info → python3_commons-0.6.12.dist-info}/LICENSE +0 -0
- {python3_commons-0.6.10.dist-info → python3_commons-0.6.12.dist-info}/WHEEL +0 -0
- {python3_commons-0.6.10.dist-info → python3_commons-0.6.12.dist-info}/top_level.txt +0 -0
python3_commons/api_client.py
CHANGED
@@ -2,9 +2,9 @@ from contextlib import asynccontextmanager
|
|
2
2
|
from datetime import datetime, UTC
|
3
3
|
from json import dumps
|
4
4
|
from typing import AsyncGenerator, Literal, Mapping, Sequence
|
5
|
+
from uuid import uuid4
|
5
6
|
|
6
|
-
from aiohttp import ClientSession
|
7
|
-
from aiohttp.web_response import Response
|
7
|
+
from aiohttp import ClientResponse, ClientSession, client_exceptions
|
8
8
|
from pydantic import HttpUrl
|
9
9
|
|
10
10
|
from python3_commons import audit
|
@@ -13,6 +13,26 @@ from python3_commons.helpers import request_to_curl
|
|
13
13
|
from python3_commons.serializers.json import CustomJSONEncoder
|
14
14
|
|
15
15
|
|
16
|
+
async def _store_response_for_audit(
|
17
|
+
response: ClientResponse,
|
18
|
+
audit_name: str,
|
19
|
+
uri_path: str,
|
20
|
+
method: str,
|
21
|
+
request_id: str
|
22
|
+
):
|
23
|
+
response_text = await response.text()
|
24
|
+
|
25
|
+
now = datetime.now(tz=UTC)
|
26
|
+
date_path = now.strftime('%Y/%m/%d')
|
27
|
+
timestamp = now.strftime('%H%M%S_%f')
|
28
|
+
|
29
|
+
await audit.write_audit_data(
|
30
|
+
s3_settings,
|
31
|
+
f'{date_path}/{audit_name}/{uri_path}/{method}_{timestamp}_{request_id}_response.txt',
|
32
|
+
response_text.encode('utf-8')
|
33
|
+
)
|
34
|
+
|
35
|
+
|
16
36
|
@asynccontextmanager
|
17
37
|
async def request(
|
18
38
|
client: ClientSession,
|
@@ -24,10 +44,11 @@ async def request(
|
|
24
44
|
json: Mapping | Sequence | str | None = None,
|
25
45
|
data: bytes | None = None,
|
26
46
|
audit_name: str | None = None
|
27
|
-
) -> AsyncGenerator[
|
47
|
+
) -> AsyncGenerator[ClientResponse]:
|
28
48
|
now = datetime.now(tz=UTC)
|
29
49
|
date_path = now.strftime('%Y/%m/%d')
|
30
50
|
timestamp = now.strftime('%H%M%S_%f')
|
51
|
+
request_id = str(uuid4())[-12:]
|
31
52
|
uri_path = uri[:-1] if uri.endswith('/') else uri
|
32
53
|
uri_path = uri_path[1:] if uri_path.startswith('/') else uri_path
|
33
54
|
url = f'{base_url}{uri}'
|
@@ -44,22 +65,38 @@ async def request(
|
|
44
65
|
if curl_request:
|
45
66
|
await audit.write_audit_data(
|
46
67
|
s3_settings,
|
47
|
-
f'{date_path}/{audit_name}/{uri_path}/{method}_{timestamp}_request.txt',
|
68
|
+
f'{date_path}/{audit_name}/{uri_path}/{method}_{timestamp}_{request_id}_request.txt',
|
48
69
|
curl_request.encode('utf-8')
|
49
70
|
)
|
50
71
|
client_method = getattr(client, method)
|
51
72
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
73
|
+
try:
|
74
|
+
if method == 'get':
|
75
|
+
async with client_method(url, params=query) as response:
|
76
|
+
if audit_name:
|
77
|
+
await _store_response_for_audit(response, audit_name, uri_path, method, request_id)
|
78
|
+
|
79
|
+
yield response
|
80
|
+
else:
|
81
|
+
if json:
|
82
|
+
data = dumps(json, cls=CustomJSONEncoder).encode('utf-8')
|
83
|
+
|
84
|
+
if headers:
|
85
|
+
headers = {**headers, 'Content-Type': 'application/json'}
|
86
|
+
else:
|
87
|
+
headers = {'Content-Type': 'application/json'}
|
88
|
+
|
89
|
+
async with client_method(url, params=query, data=data, headers=headers) as response:
|
90
|
+
if audit_name:
|
91
|
+
await _store_response_for_audit(response, audit_name, uri_path, method, request_id)
|
58
92
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
93
|
+
yield response
|
94
|
+
except client_exceptions.ClientOSError as e:
|
95
|
+
if e.errno == 32:
|
96
|
+
raise ConnectionResetError('Broken pipe') from e
|
97
|
+
elif e.errno == 104:
|
98
|
+
raise ConnectionResetError('Connection reset by peer') from e
|
63
99
|
|
64
|
-
|
65
|
-
|
100
|
+
raise
|
101
|
+
except client_exceptions.ServerDisconnectedError as e:
|
102
|
+
raise ConnectionResetError('Server disconnected') from e
|
@@ -1,5 +1,5 @@
|
|
1
1
|
python3_commons/__init__.py,sha256=0KgaYU46H_IMKn-BuasoRN3C4Hi45KlkHHoPbU9cwiA,189
|
2
|
-
python3_commons/api_client.py,sha256=
|
2
|
+
python3_commons/api_client.py,sha256=ZUgqqTHUuGL1a6-xenLV7OTIxpdMI2StwdmvHjZCLek,3518
|
3
3
|
python3_commons/audit.py,sha256=DMQ-nrWSs0qilD7wkz_8PV4jXcee75O8FgAm2YIuOiY,6256
|
4
4
|
python3_commons/conf.py,sha256=qm2a2yWOhfawicBPjWnUett8TrsMtoyQXDxEJ_N-v-Y,637
|
5
5
|
python3_commons/db.py,sha256=qhaDIdzBWgFyeP_XPKfHZlYVlwS2bpBPYMv84yV6820,738
|
@@ -13,9 +13,9 @@ python3_commons/serializers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMp
|
|
13
13
|
python3_commons/serializers/json.py,sha256=P288wWz9ic38QWEMrpp_uwKPYkQiOgvE1cI4WZn6ZCg,808
|
14
14
|
python3_commons/serializers/msgpack.py,sha256=tzIGGyDL3UpZnnouCtnxuYDx6InKM_C3PP1N4PN8wd4,1269
|
15
15
|
python3_commons/serializers/msgspec.py,sha256=FuZVqOLJb0-lEKrs7dtjhwEbHIpfMUk5yu1hD64zRdc,2038
|
16
|
-
python3_commons-0.6.
|
17
|
-
python3_commons-0.6.
|
18
|
-
python3_commons-0.6.
|
19
|
-
python3_commons-0.6.
|
20
|
-
python3_commons-0.6.
|
21
|
-
python3_commons-0.6.
|
16
|
+
python3_commons-0.6.12.dist-info/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
|
17
|
+
python3_commons-0.6.12.dist-info/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
|
18
|
+
python3_commons-0.6.12.dist-info/METADATA,sha256=qpV7yFXEO1_wjMu7RA8X1-Lvc7JT-T4S6em8pGuzYVQ,986
|
19
|
+
python3_commons-0.6.12.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
20
|
+
python3_commons-0.6.12.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
|
21
|
+
python3_commons-0.6.12.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|