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 = "*" ...@@ -11,6 +11,7 @@ flask = "*"
flask-restful = "*" flask-restful = "*"
marshmallow = "*" marshmallow = "*"
bson = "*" bson = "*"
flask-cors = "*"
[dev-packages] [dev-packages]
......
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "6197445b839f98b90944ea771c7c0f63db31f0e455b325be3435fa2d17a89517" "sha256": "1147de24391cc36d2688e229ffc68c4a093efa85ef0a957f89206ba6784dc2b3"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
...@@ -54,6 +54,14 @@ ...@@ -54,6 +54,14 @@
"index": "pypi", "index": "pypi",
"version": "==2.2.2" "version": "==2.2.2"
}, },
"flask-cors": {
"hashes": [
"sha256:74efc975af1194fc7891ff5cd85b0f7478be4f7f59fe158102e91abb72bb4438",
"sha256:b60839393f3b84a0f3746f6cdca56c1ad7426aa738b70d6c61375857823181de"
],
"index": "pypi",
"version": "==3.0.10"
},
"flask-restful": { "flask-restful": {
"hashes": [ "hashes": [
"sha256:4970c49b6488e46c520b325f54833374dc2b98e211f1b272bd4b0c516232afe2", "sha256:4970c49b6488e46c520b325f54833374dc2b98e211f1b272bd4b0c516232afe2",
......
...@@ -5,9 +5,11 @@ from endpoints.clients import Client, ClientList ...@@ -5,9 +5,11 @@ from endpoints.clients import Client, ClientList
from endpoints.receive import Receive from endpoints.receive import Receive
from endpoints.send import Send from endpoints.send import Send
from endpoints.notify import Notify from endpoints.notify import Notify
from flask_cors import CORS
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
CORS(app, resources={r"*": {"origins": "http://localhost:8086"}})
api.add_resource(HelloWorld, "/") api.add_resource(HelloWorld, "/")
api.add_resource(ClientList, "/client") api.add_resource(ClientList, "/client")
...@@ -17,4 +19,4 @@ api.add_resource(Send, "/send") ...@@ -17,4 +19,4 @@ api.add_resource(Send, "/send")
api.add_resource(Notify, "/notify") api.add_resource(Notify, "/notify")
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True, port=8087)
from flask_restful import Resource, request, abort from flask_restful import Resource, request, abort
from marshmallow import Schema, fields from marshmallow import Schema, fields
import pika
import json import json
class NotifySchema(Schema): class NotifySchema(Schema):
...@@ -12,7 +13,7 @@ class Notify(Resource): ...@@ -12,7 +13,7 @@ class Notify(Resource):
schema = None schema = None
def __init__(self): def __init__(self):
self.schema = ReceiveSchema() self.schema = NotifySchema()
with open("clients.json", "r") as clients_file: with open("clients.json", "r") as clients_file:
self.clients = json.load(clients_file) self.clients = json.load(clients_file)
...@@ -22,9 +23,12 @@ class Notify(Resource): ...@@ -22,9 +23,12 @@ class Notify(Resource):
if errors: if errors:
abort(400, message=str(errors)) abort(400, message=str(errors))
messages = []
allow = False allow = False
body = args.get("body") body = args.get("body")
message = {
'topic': 'broadcast',
'message': body,
}
client_id = args.get("client_id") client_id = args.get("client_id")
notify_queue = client_id + "-notify" notify_queue = client_id + "-notify"
if client_id in self.clients: if client_id in self.clients:
...@@ -36,5 +40,5 @@ class Notify(Resource): ...@@ -36,5 +40,5 @@ class Notify(Resource):
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue=notify_queue, durable=True) channel.queue_declare(queue=notify_queue, durable=True)
channel.basic_publish(exchange="", routing_key=notify_queue, body=body) channel.basic_publish(exchange="", routing_key=notify_queue, body=json.dumps(message))
deliver_connection.close() connection.close()
from flask_restful import Resource, request, abort from flask_restful import Resource, request, abort
from marshmallow import Schema, fields from marshmallow import Schema, fields
import pika
import json import json
class SendSchema(Schema): class SendSchema(Schema):
...@@ -13,7 +14,7 @@ class Send(Resource): ...@@ -13,7 +14,7 @@ class Send(Resource):
schema = None schema = None
def __init__(self): def __init__(self):
self.schema = ReceiveSchema() self.schema = SendSchema()
with open("clients.json", "r") as clients_file: with open("clients.json", "r") as clients_file:
self.clients = json.load(clients_file) self.clients = json.load(clients_file)
...@@ -23,7 +24,6 @@ class Send(Resource): ...@@ -23,7 +24,6 @@ class Send(Resource):
if errors: if errors:
abort(400, message=str(errors)) abort(400, message=str(errors))
messages = []
allow = False allow = False
body = args.get("body") body = args.get("body")
topic = args.get("topic") topic = args.get("topic")
...@@ -37,10 +37,10 @@ class Send(Resource): ...@@ -37,10 +37,10 @@ class Send(Resource):
if allow: if allow:
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue=notify_queue, durable=True) channel.queue_declare(queue=outbox_queue, durable=True)
message = { message = {
'topic': topic, 'topic': topic,
'message': body, 'message': body,
} }
channel.basic_publish(exchange="", routing_key=notify_queue, body=message) channel.basic_publish(exchange="", routing_key=outbox_queue, body=json.dumps(message))
deliver_connection.close() 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