Commit 447c76aa authored by Alexander Lercher's avatar Alexander Lercher

Rest Gateway: takes traces via REST, message to trace-retrieval

parent 5a50ca77
...@@ -13,7 +13,7 @@ class MessageSender: ...@@ -13,7 +13,7 @@ class MessageSender:
_connection: pika.BlockingConnection = None _connection: pika.BlockingConnection = None
_channel: pika.channel.Channel = None _channel: pika.channel.Channel = None
def __init__(self, rabbit_mq_ip='143.205.173.36', rabbit_mq_port=30302): def __init__(self, rabbit_mq_ip='rabbit-mq', rabbit_mq_port=5672):
self._rabbit_mq_ip = rabbit_mq_ip self._rabbit_mq_ip = rabbit_mq_ip
self._rabbit_mq_port = rabbit_mq_port self._rabbit_mq_port = rabbit_mq_port
......
...@@ -39,4 +39,54 @@ paths: ...@@ -39,4 +39,54 @@ paths:
description: "Lists all messages received from Rabbit MQ." description: "Lists all messages received from Rabbit MQ."
responses: responses:
200: 200:
description: "Message list" description: "Message list"
\ No newline at end of file
/trace:
post:
operationId: "rest.blockchain_trace.receive"
tags:
- "Blockchain Trace"
summary: "Add a new blockchain trace to SMART"
description: "Receives a new blockchain trace to store in SMART."
parameters:
- in: body
name: "BlockchainTrace"
description: "The trace to be added"
required: true
schema:
$ref: "#/definitions/BlockchainTrace"
responses:
201:
description: "Successfully added"
400:
description: "Invalid input"
definitions:
BlockchainTrace:
type: "object"
properties:
TransactionId:
type: string
format: uuid
Timestamp:
type: "string"
format: "date-time"
ApplicationType:
type: "string"
TransactionFrom:
type: "string"
format: "uuid"
TransactionTo:
type: "string"
format: "uuid"
TransferredAsset:
type: "string"
ResourceIds:
type: "string"
ResourceMd5:
type: "string"
ResourceState:
type: "string"
Metadata:
type: "string"
\ No newline at end of file
...@@ -19,11 +19,12 @@ from messaging.MessageReceiver import MessageReceiver ...@@ -19,11 +19,12 @@ from messaging.MessageReceiver import MessageReceiver
from messaging.MessageSender import MessageSender from messaging.MessageSender import MessageSender
from MessageList import MessageList from MessageList import MessageList
RABBIT_MQ_DNS_NAME = 'rabbit-mq'
RABBIT_MQ_PORT = '5672'
messages = MessageList.getInstance() messages = MessageList.getInstance()
message_sender = MessageSender()
message_sender.connect()
message_sender.create_exchange('inhub', 'direct')
# init message handler # init message handler
def message_received_callback(channel, method, properties, body): def message_received_callback(channel, method, properties, body):
messages.appendMessage(body) messages.appendMessage(body)
...@@ -44,9 +45,7 @@ def api_root(): ...@@ -44,9 +45,7 @@ def api_root():
# start app # start app
if __name__ == '__main__': if __name__ == '__main__':
message_rec = MessageReceiver( message_rec = MessageReceiver(exchange_name='rest-gateway', exchange_type='direct', queue_name='rest-gateway', auto_ack=True)
rabbit_mq_ip=RABBIT_MQ_DNS_NAME, rabbit_mq_port=RABBIT_MQ_PORT,
exchange_name='rest-gateway', exchange_type='direct', queue_name='rest-gateway', auto_ack=True)
message_rec.start(message_received_callback, pika_error_callback) message_rec.start(message_received_callback, pika_error_callback)
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False) # disable reloader so only subscribed once to rabbitmq app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False) # disable reloader so only subscribed once to rabbitmq
\ No newline at end of file
from flask import request, Response
import main
def receive():
body = request.json
if isBlockchainTraceValid(body):
message = {'type': 'blockchain-transaction', 'content': body}
main.message_sender.send('inhub', message, 'trace-retrieval')
return Response(status=201)
def isBlockchainTraceValid(trace) -> bool:
return 'TransactionId' in trace \
and 'Timestamp' in trace \
and 'ApplicationType' in trace \
and 'TransactionFrom' in trace \
and 'TransactionFromLatLng' in trace \
and 'TransactionTo' in trace \
and 'TransactionToLatLng' in trace \
and 'TransferredAsset' in trace \
and 'ResourceIds' in trace \
and 'ResourceMd5' in trace \
and 'ResourceState' in trace \
and 'Metadata' in trace
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