From 44518aef05dfcc3e8897261ce04641c650e0f66a Mon Sep 17 00:00:00 2001 From: Dan Jones <dan.jones@noc.ac.uk> Date: Thu, 17 Nov 2022 12:56:19 +0000 Subject: [PATCH] fix: correct send and notify endpoints + Add flask-cors to enable inbound API requests + Change api default port --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- api.py | 4 +++- endpoints/notify.py | 12 ++++++++---- endpoints/send.py | 10 +++++----- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Pipfile b/Pipfile index 830c70e..9510ffc 100644 --- a/Pipfile +++ b/Pipfile @@ -11,6 +11,7 @@ flask = "*" flask-restful = "*" marshmallow = "*" bson = "*" +flask-cors = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 6bfa164..a8acc9b 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6197445b839f98b90944ea771c7c0f63db31f0e455b325be3435fa2d17a89517" + "sha256": "1147de24391cc36d2688e229ffc68c4a093efa85ef0a957f89206ba6784dc2b3" }, "pipfile-spec": 6, "requires": { @@ -54,6 +54,14 @@ "index": "pypi", "version": "==2.2.2" }, + "flask-cors": { + "hashes": [ + "sha256:74efc975af1194fc7891ff5cd85b0f7478be4f7f59fe158102e91abb72bb4438", + "sha256:b60839393f3b84a0f3746f6cdca56c1ad7426aa738b70d6c61375857823181de" + ], + "index": "pypi", + "version": "==3.0.10" + }, "flask-restful": { "hashes": [ "sha256:4970c49b6488e46c520b325f54833374dc2b98e211f1b272bd4b0c516232afe2", diff --git a/api.py b/api.py index 825c204..fad511c 100644 --- a/api.py +++ b/api.py @@ -5,9 +5,11 @@ from endpoints.clients import Client, ClientList from endpoints.receive import Receive from endpoints.send import Send from endpoints.notify import Notify +from flask_cors import CORS app = Flask(__name__) api = Api(app) +CORS(app, resources={r"*": {"origins": "http://localhost:8086"}}) api.add_resource(HelloWorld, "/") api.add_resource(ClientList, "/client") @@ -17,4 +19,4 @@ api.add_resource(Send, "/send") api.add_resource(Notify, "/notify") if __name__ == "__main__": - app.run(debug=True) + app.run(debug=True, port=8087) diff --git a/endpoints/notify.py b/endpoints/notify.py index 3e0c552..754b7f6 100644 --- a/endpoints/notify.py +++ b/endpoints/notify.py @@ -1,5 +1,6 @@ from flask_restful import Resource, request, abort from marshmallow import Schema, fields +import pika import json class NotifySchema(Schema): @@ -12,7 +13,7 @@ class Notify(Resource): schema = None def __init__(self): - self.schema = ReceiveSchema() + self.schema = NotifySchema() with open("clients.json", "r") as clients_file: self.clients = json.load(clients_file) @@ -22,9 +23,12 @@ class Notify(Resource): if errors: abort(400, message=str(errors)) - messages = [] allow = False body = args.get("body") + message = { + 'topic': 'broadcast', + 'message': body, + } client_id = args.get("client_id") notify_queue = client_id + "-notify" if client_id in self.clients: @@ -36,5 +40,5 @@ class Notify(Resource): connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) channel = connection.channel() channel.queue_declare(queue=notify_queue, durable=True) - channel.basic_publish(exchange="", routing_key=notify_queue, body=body) - deliver_connection.close() + channel.basic_publish(exchange="", routing_key=notify_queue, body=json.dumps(message)) + connection.close() diff --git a/endpoints/send.py b/endpoints/send.py index d755186..17a958e 100644 --- a/endpoints/send.py +++ b/endpoints/send.py @@ -1,5 +1,6 @@ from flask_restful import Resource, request, abort from marshmallow import Schema, fields +import pika import json class SendSchema(Schema): @@ -13,7 +14,7 @@ class Send(Resource): schema = None def __init__(self): - self.schema = ReceiveSchema() + self.schema = SendSchema() with open("clients.json", "r") as clients_file: self.clients = json.load(clients_file) @@ -23,7 +24,6 @@ class Send(Resource): if errors: abort(400, message=str(errors)) - messages = [] allow = False body = args.get("body") topic = args.get("topic") @@ -37,10 +37,10 @@ class Send(Resource): if allow: connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) channel = connection.channel() - channel.queue_declare(queue=notify_queue, durable=True) + channel.queue_declare(queue=outbox_queue, durable=True) message = { 'topic': topic, 'message': body, } - channel.basic_publish(exchange="", routing_key=notify_queue, body=message) - deliver_connection.close() + channel.basic_publish(exchange="", routing_key=outbox_queue, body=json.dumps(message)) + connection.close() -- GitLab