-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
92 lines (53 loc) · 2.25 KB
/
Copy pathapi.py
File metadata and controls
92 lines (53 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import asyncio
import sys
import aiohttp
import time
import json
import coms.coms
import coms.content_packer
import client_tools.get_from_github
async def __start_messaging(url: str, request):
coms.coms.logmsg(f'подключение к {url}')
TIMEOUT_FOR_AIOHTTP = 60 * 5
time_start = time.perf_counter()
req_unpacked = len(request)
request = coms.content_packer.pack_message(request, 'gzip')
req_packed = len(request)
ans_unpacked = 0
ans_packed = 0
headers = {
"Content-Type": "application/octet-stream",
"Content-Length": str(len(request))
}
try:
async with aiohttp.ClientSession(headers=headers) as session:
async with session.post(url, data=request, timeout=TIMEOUT_FOR_AIOHTTP) as response:
if response.status == 200:
answer = await response.read()
ans_packed = len(answer)
_, answer = coms.content_packer.decompress_message(answer)
ans_unpacked = len(answer)
return answer
else:
error_body = await response.text()
short_error_body = error_body[:500] if error_body else "Empty body"
raise Exception(f'aiohttp error, status: {response.status}, {short_error_body}')
except aiohttp.ClientConnectorError:
raise Exception("Ошибка подключения.")
except Exception as e:
coms.coms.logmsg(coms.coms.format_error(e))
raise Exception(f"Произошла ошибка: {e}")
finally:
time_passed = time.perf_counter() - time_start
coms.coms.logmsg(
f'Размеры, запрос: {req_packed}/{req_unpacked}, ответ: {ans_packed}/{ans_unpacked}, время: {time_passed:.3f}')
async def make_request(request):
try:
if isinstance(request, dict):
request = json.dumps(request, indent=4, ensure_ascii=False, sort_keys=False)
url = await client_tools.get_from_github.get_from_github()
if url is None:
raise Exception("Что-то не так")
return await __start_messaging(url, request)
except Exception as e:
return coms.coms.format_error(e)