def message_post(): """ Puts message to the message lake and into the processing queue """ body = request.get_json(silent=True) if not body or not isinstance(body, dict): raise MessageDataEmptyError() try: message = Message.from_dict(body) except Exception as e: raise MessageDeserializationError(source=[str(e)]) if not message.is_valid(): raise MessageValidationError(source=message.validation_errors()) # we fill it for message_api but not for message_rx_api if not message.sender_ref: message.kwargs["sender_ref"] = str(uuid.uuid4()) # because we are first who sees that message message.kwargs["status"] = "pending" repo = BCInboxRepo(Config.BC_INBOX_CONF) use_case = EnqueueMessageUseCase(repo) if use_case.execute(message): return Response( json.dumps(message, cls=ser.MessageJSONEncoder), status=HTTPStatus.CREATED, mimetype='application/json', # headers={'Location': message_url} ) else: raise UnableWriteToInboxError()
def __init__(self): channel_notification_repo_conf = env_queue_config( 'CHANNEL_NOTIFICATION_REPO') channel_notification_repo = ChannelNotificationRepo( channel_notification_repo_conf) bc_inbox_repo_conf = env_queue_config('PROC_BC_INBOX') bc_inbox_repo = BCInboxRepo(bc_inbox_repo_conf) self.use_case = ProcessChannelNotificationUseCase( channel_notification_repo=channel_notification_repo, bc_inbox_repo=bc_inbox_repo, routing_table=ROUTING_TABLE)
def message(): """ Usage: curl -XPOST http://127.0.0.1:5000/messages \ -H 'Content-type: application/json' \ -d '{"adf": "ee"}' """ # silent prevents default error which is BadRequest body = request.get_json(silent=True) if not body or not isinstance(body, dict): raise MessageDataEmptyError() try: message = Message.from_dict(body, require_allowed=["sender_ref"]) except Exception as e: raise MessageDeserializationError(source=[str(e)]) if not message.is_valid(): raise MessageValidationError(source=message.validation_errors()) message_url = message.absolute_object_uri() if not message_url: raise MessageAbsoluteURLError() message.kwargs["status"] = "received" # because it's RX api repo = BCInboxRepo( Config.BC_INBOX_CONF ) use_case = EnqueueMessageUseCase(repo) if use_case.execute(message): return Response( json.dumps(message, cls=ser.MessageJSONEncoder), status=201, mimetype='application/json', headers={'Location': message_url} ) else: raise UnableWriteToInboxError()
def test(): # creating testing versions of all required repos message_lake_repo = MessageLakeRepo(MESSAGE_LAKE_REPO_CONF) object_acl_repo = ObjectACLRepo(OBJECT_ACL_REPO_CONF) bc_inbox_repo = BCInboxRepo(BC_INBOX_REPO_CONF) object_retrieval_repo = ObjectRetrievalRepo(OBJECT_RETRIEVAL_REPO_CONF) notifications_repo = NotificationsRepo(NOTIFICATIONS_REPO_CONF) blockchain_outbox_repo = ApiOutboxRepo(BLOCKCHAIN_OUTBOX_REPO_CONF) def clear(): # clearing repos message_lake_repo._unsafe_method__clear() object_acl_repo._unsafe_method__clear() bc_inbox_repo._unsafe_method__clear() object_retrieval_repo._unsafe_method__clear() notifications_repo._unsafe_method__clear() blockchain_outbox_repo._unsafe_method__clear() # test repos are empty assert message_lake_repo.is_empty() assert object_acl_repo.is_empty() assert bc_inbox_repo.is_empty() assert object_retrieval_repo.is_empty() assert notifications_repo.is_empty() assert blockchain_outbox_repo.is_empty() clear() processor = InboundMessageProcessor( bc_inbox_repo_conf=BC_INBOX_REPO_CONF, message_lake_repo_conf=MESSAGE_LAKE_REPO_CONF, object_acl_repo_conf=OBJECT_ACL_REPO_CONF, object_retrieval_repo_conf=OBJECT_RETRIEVAL_REPO_CONF, notifications_repo_conf=NOTIFICATIONS_REPO_CONF, blockchain_outbox_repo_conf=BLOCKCHAIN_OUTBOX_REPO_CONF) # test iter processor returns processor assert iter(processor) is processor # test processor has no jobs assert next(processor) is None sender_ref = "AU:xxxx-xxxx-xxxx" status = 'received' message = _generate_msg_object(sender_ref=sender_ref, status=status) message.sender = "CN" assert bc_inbox_repo.post(message) # testing normal execution received message with sender ref assert next(processor) is True assert next(processor) is None # testing that message is deleted assert bc_inbox_repo.is_empty() # testing message posted to related repos assert not message_lake_repo.is_empty() assert not object_acl_repo.is_empty() # we can't say it's empty because worker gets values from there # assert not object_retrieval_repo.is_empty() # received status should not be posted to blockchain assert blockchain_outbox_repo.is_empty() clear() sender_ref = "AU:xxxx-xxxx-xxxx" # this one should go to blockchain outbox status = 'pending' message = _generate_msg_object(sender_ref=sender_ref, status=status) message.sender = OUR_JRD message.receiver = 'CN' assert bc_inbox_repo.post(message) # testing normal execution received message with sender ref assert next(processor) is True assert next(processor) is None # testing that message is deleted assert bc_inbox_repo.is_empty() # testing message posted to related repos assert not message_lake_repo.is_empty() assert not object_acl_repo.is_empty() clear() # message without sender ref should fail message = _generate_msg_object() assert bc_inbox_repo.post(message) assert next(processor) is False assert next(processor) is None
def _prepare_bc_inbox_repo(self, conf): bc_inbox_repo_conf = env_queue_config('PROC_BC_INBOX') if conf: bc_inbox_repo_conf.update(conf) self.bc_inbox_repo = BCInboxRepo(bc_inbox_repo_conf)