Unverified Commit 44518aef authored by Dan Jones's avatar Dan Jones
Browse files

fix: correct send and notify endpoints

+ Add flask-cors to enable inbound API requests
+ Change api default port
parent 6482ba23
......@@ -11,6 +11,7 @@ flask = "*"
flask-restful = "*"
marshmallow = "*"
bson = "*"
flask-cors = "*"
[dev-packages]
......
{
"_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",
......
......@@ -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)
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()
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()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment