/
qpfilw
/
restapi2
Обзор
Документация
Войти
/
qpfilw
/
restapi2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
laba4
client.py
176 строк
5 KB
qpfilw
fix scripts
23 дек 2025, 11:05
23 дек 2025, 11:05
6d3448c
Код
Авторство
О чём код?
import pika import json import uuid import time class RabbitMQClient: def __init__(self): self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5673)) self.channel = self.connection.channel() result = self.channel.queue_declare(queue='', exclusive=True) self.callback_queue = result.method.queue self.channel.basic_consume(queue=self.callback_queue, on_message_callback=self.on_response, auto_ack=True) self.responses = {} def on_response(self, ch, method, props, body): if props.correlation_id in self.responses: self.responses[props.correlation_id] = json.loads(body) def call(self, message): corr_id = str(uuid.uuid4()) message['id'] = corr_id self.responses[corr_id] = None self.channel.basic_publish( exchange='', routing_key='api.requests', properties=pika.BasicProperties( reply_to=self.callback_queue, correlation_id=corr_id, ), body=json.dumps(message) ) while self.responses[corr_id] is None: self.connection.process_data_events(time_limit=30) return self.responses.pop(corr_id) def close(self): self.connection.close() def run_demo(): client = RabbitMQClient() # создание пользователя print("Sending create_user request...") create_user_msg = { "version": "v1", "action": "create_user", "data": { "username": "demo_user", "email": "demo@example.com", "password": "demopass" }, "auth": None } response = client.call(create_user_msg) print("Create user response:", json.dumps(response, indent=4)) time.sleep(1) # логин для получения токена print("Sending login request...") login_msg = { "version": "v1", "action": "login", "data": { "username": "demo_user", "password": "demopass" }, "auth": None } response = client.call(login_msg) print("Login response:", json.dumps(response, indent=4)) token = response.get('data', {}).get('access_token') if response['status'] == 'ok' else None if not token: print("Failed to get token. Exiting.") client.close() return time.sleep(1) # создание задачи (v1) print("Sending create_task (v1) request...") create_task_v1_msg = { "version": "v1", "action": "create_task", "data": { "title": "Demo Task V1", "completed": False }, "auth": token } response = client.call(create_task_v1_msg) print("Create task v1 response:", json.dumps(response, indent=4)) task_id_v1 = response.get('data', {}).get('id') if response['status'] == 'ok' else None time.sleep(1) # создание задачи (v2 с description) print("Sending create_task (v2) request...") create_task_v2_msg = { "version": "v2", "action": "create_task", "data": { "title": "Demo Task V2", "completed": False, "description": "This is a demo description" }, "auth": token } response = client.call(create_task_v2_msg) print("Create task v2 response:", json.dumps(response, indent=4)) task_id_v2 = response.get('data', {}).get('id') if response['status'] == 'ok' else None time.sleep(1) # получение списка задач print("Sending get_tasks request...") get_tasks_msg = { "version": "v1", "action": "get_tasks", "data": { "skip": 0, "limit": 10, "fields": "id,title,completed" }, "auth": token } response = client.call(get_tasks_msg) print("Get tasks response:", json.dumps(response, indent=4)) time.sleep(1) # получение одной задачи if task_id_v1: print("Sending get_task request...") get_task_msg = { "version": "v1", "action": "get_task", "data": {"task_id": task_id_v1}, "auth": token } response = client.call(get_task_msg) print("Get task response:", json.dumps(response, indent=4)) time.sleep(1) # обновление задачи if task_id_v1: print("Sending update_task request...") update_task_msg = { "version": "v1", "action": "update_task", "data": { "task_id": task_id_v1, "task": { "title": "Updated Demo Task V1", "completed": True } }, "auth": token } response = client.call(update_task_msg) print("Update task response:", json.dumps(response, indent=4)) time.sleep(1) # удаление задачи if task_id_v1: print("Sending delete_task request...") delete_task_msg = { "version": "v1", "action": "delete_task", "data": {"task_id": task_id_v1}, "auth": token } response = client.call(delete_task_msg) print("Delete task response:", json.dumps(response, indent=4)) client.close() if __name__ == "__main__": run_demo()