From a1d455df2a7423e9a74dfcb79910e26a0f852775 Mon Sep 17 00:00:00 2001 From: Malachy Byrne Date: Wed, 12 Apr 2023 18:14:41 +0100 Subject: [PATCH] Added project server to be used by other endpoints --- classes.py | 30 ++++++++++++++++++++++++++++++ project.py | 19 ------------------- project_server.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 classes.py delete mode 100644 project.py create mode 100644 project_server.py diff --git a/classes.py b/classes.py new file mode 100644 index 0000000..36dd166 --- /dev/null +++ b/classes.py @@ -0,0 +1,30 @@ +from dataclasses import dataclass, field +import pickle +from typing import List + +@dataclass +class Transaction: + person: str + value: int + description: str + + +@dataclass +class Project: + acronym: str + title: str + description: str + amount: int + + history: List[Transaction] = field(default_factory=list) + +if __name__ == "__main__": + history = [Transaction(f"person {i}", i, f"description {i}") for i in range(10)] + project1 = Project("acronym", "title", "description", 500, history) + with open("file", "wb+") as file1: + pickle.dump(project1, file1) + with open("file", "rb") as file2: + project2 = pickle.load(file2) + print(project2) + + diff --git a/project.py b/project.py deleted file mode 100644 index 5e76247..0000000 --- a/project.py +++ /dev/null @@ -1,19 +0,0 @@ -from dataclasses import dataclass -import pickle - -@dataclass -class Project: - acronym: str - title: str - description: str - amount: int - - -if __name__ == "__main__": - project1 = Project("acronym", "title", "description", 500) - with open("file", "wb+") as file1: - pickle.dump(project1, file1) - with open("file", "rb") as file2: - project2 = pickle.load(file2) - print(project2) - diff --git a/project_server.py b/project_server.py new file mode 100644 index 0000000..35497ca --- /dev/null +++ b/project_server.py @@ -0,0 +1,42 @@ +import pika + +import pickle +import os + +from classes import Project + +if not os.path.exists("approved_projects"): + os.makedirs("approved_projects") + +connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) + +channel = connection.channel() + +channel.queue_declare(queue='get_project') +channel.queue_declare(queue='save_project') + +def get_project(ch, method, properties, body): + project_id = body.decode() + with open(os.path.join("approved_projects", f"{project_id}.txt"), "rb") as file: + project = file.read() + channel.basic_publish(exchange='', + routing_key=properties.reply_to, + properties=pika.BasicProperties(correlation_id=properties.correlation_id), + body=project) + print(f"got project {project_id}") + + +def save_project(ch, method, properties, body): + project_data = body.decode() + acronym, title, description, amount = project_data.split(',') + + project = Project(acronym, title, description, int(amount)) + filename = os.path.join("approved_projects", f"{acronym}.txt") + with open(filename, "wb+") as file: + pickle.dump(project, file) + + +channel.basic_consume(queue='get_project', on_message_callback=get_project, auto_ack=False) +channel.basic_consume(queue='save_project', on_message_callback=save_project, auto_ack=True) + +channel.start_consuming()