forked from mtusi2026/mtusi2026lab3
101 lines
4.6 KiB
Python
101 lines
4.6 KiB
Python
"""Общие функции инструментов lab3: минимальный HTTP-клиент (stdlib) и подготовка синтетических данных."""
|
||
import base64
|
||
import http.client
|
||
import json
|
||
import threading
|
||
import urllib.parse
|
||
import uuid
|
||
|
||
|
||
class Api:
|
||
"""Клиент с keep-alive соединением на поток. Сетевые ошибки пробрасываются, соединение пересоздаётся."""
|
||
|
||
def __init__(self, base_url, timeout=5.0):
|
||
self.base_url = base_url
|
||
parsed = urllib.parse.urlparse(base_url)
|
||
self.host, self.port, self.timeout = parsed.hostname, parsed.port or 80, timeout
|
||
self._local = threading.local()
|
||
|
||
def _connection(self):
|
||
conn = getattr(self._local, 'conn', None)
|
||
if conn is None:
|
||
conn = http.client.HTTPConnection(self.host, self.port, timeout=self.timeout)
|
||
self._local.conn = conn
|
||
return conn
|
||
|
||
def request(self, method, path, body=None, token=None, headers=None):
|
||
"""Возвращает (status, headers, json|None). Исключение = нет ответа (обрыв, таймаут)."""
|
||
h = {'Accept': 'application/json'}
|
||
data = None
|
||
if body is not None:
|
||
data = json.dumps(body, ensure_ascii=False).encode('utf-8')
|
||
h['Content-Type'] = 'application/json'
|
||
if token:
|
||
h['Authorization'] = 'Bearer ' + token
|
||
h.update(headers or {})
|
||
# Для GET один раз прозрачно повторяем запрос, если сервер/LB закрыл простаивавшее keep-alive соединение
|
||
# (timeout http-keep-alive в HAProxy). POST так не повторяется: его повтор решает вызывающий с тем же ключом.
|
||
attempts = 2 if method == 'GET' else 1
|
||
for attempt in range(attempts):
|
||
conn = self._connection()
|
||
reused = conn.sock is not None
|
||
try:
|
||
conn.request(method, path, body=data, headers=h)
|
||
response = conn.getresponse()
|
||
raw = response.read()
|
||
if response.getheader('Connection', '').lower() == 'close':
|
||
conn.close()
|
||
self._local.conn = None
|
||
break
|
||
except (OSError, http.client.HTTPException):
|
||
conn.close()
|
||
self._local.conn = None
|
||
if attempt + 1 >= attempts or not reused:
|
||
raise
|
||
payload = None
|
||
if raw and response.getheader('Content-Type', '').startswith('application/json'):
|
||
payload = json.loads(raw)
|
||
return response.status, {k.lower(): v for k, v in response.getheaders()}, payload
|
||
|
||
|
||
def register_user(api, label):
|
||
"""Создаёт пользователя и сессию. Возвращает {'id', 'token'}."""
|
||
username = 'l3_%s_%s' % (label, uuid.uuid4().hex[:10])
|
||
password = 'T3st-' + uuid.uuid4().hex
|
||
status, _, user = api.request('POST', '/api/v1/users',
|
||
{'username': username, 'display_name': 'Нагрузка ' + label, 'password': password})
|
||
assert status == 201, 'register failed: %s' % status
|
||
credential = base64.b64encode((username + ':' + password).encode()).decode()
|
||
status, _, session = api.request('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + credential})
|
||
assert status == 201, 'login failed: %s' % status
|
||
return {'id': user['id'], 'token': session['token']}
|
||
|
||
|
||
def create_chat(api, owner, others=()):
|
||
members = [owner['id']] + [o['id'] for o in others]
|
||
status, _, chat = api.request('POST', '/api/v1/chats', {'title': 'Нагрузка', 'member_ids': members}, token=owner['token'])
|
||
assert status == 201, 'create chat failed: %s' % status
|
||
return chat['id']
|
||
|
||
|
||
def read_history(api, user, chat_id):
|
||
"""Вся история чата постранично (cursor), для проверки дублей и потерь."""
|
||
items, cursor = [], None
|
||
while True:
|
||
path = '/api/v1/chats/%s/messages?limit=100' % chat_id
|
||
if cursor:
|
||
path += '&cursor=' + urllib.parse.quote(cursor, safe='')
|
||
status, _, page = api.request('GET', path, token=user['token'])
|
||
assert status == 200, 'history failed: %s' % status
|
||
items.extend(page['items'])
|
||
cursor = page['next_cursor']
|
||
if cursor is None:
|
||
return items
|
||
|
||
|
||
def percentile(sorted_values, p):
|
||
if not sorted_values:
|
||
return None
|
||
index = max(0, min(len(sorted_values) - 1, int(-(-p * len(sorted_values) // 1)) - 1))
|
||
return sorted_values[index]
|