def request_handler(event, context): try: valid_event = validate_dict( data_dict=event, required_keys=REQUIRED_KEYS_FOR_CANCEL_ORDER_EVENT) if not valid_event: return generate_lambda_response(400, "Bad Request", cors_enabled=True) path_parameters = event.get("pathParameters", None) order_service = OrderService(obj_repo=repo) cancel_order_status = order_service.cancel_order_for_given_order_id( order_id=path_parameters["order_id"]) response = generate_lambda_response(200, { "status": "success", "data": cancel_order_status }, cors_enabled=True) except Exception as e: error_message = format_error_message(status="failed", error=repr(e), payload=payload_dict, net_id=NETWORK_ID, handler="cancel_order_handler") obj_util.report_slack(1, error_message, SLACK_HOOK) response = generate_lambda_response(500, error_message, cors_enabled=True) traceback.print_exc() return response
def request_handler(event, context): try: order_service = OrderService(obj_repo=repo) response = order_service.cancel_order() logger.info(f"Response for update transaction status {response}") return "success" except Exception as e: error_message = "Error in updating channel transaction status \n" "NETWORK ID:" + str(NETWORK_ID) + "\n" "Error:" + repr(e) obj_util.report_slack(error_message, SLACK_HOOK) traceback.print_exc()
def get(event, context): logger.info("Received request to get orders for username") try: username = event["requestContext"]["authorizer"]["claims"]["email"] order_id = None path_parameters = event["pathParameters"] if path_parameters is not None: order_id = path_parameters["order_id"] bad_request = False if order_id is None: logger.info(f"Getting all order details for user {username}") response = OrderService( obj_repo=repo).get_order_details_by_username(username) else: logger.info(f"Getting order details for order_id {order_id}") if order_id is not None: response = OrderService( obj_repo=repo).get_order_details_by_order_id( username=username, order_id=order_id) else: bad_request = True error = {} if bad_request: status_code = StatusCode.BAD_REQUEST status = ResponseStatus.FAILED response = "Bad Request" logger.error(f"response: {response}\n" f"event: {event}") else: status_code = StatusCode.CREATED status = ResponseStatus.SUCCESS except Exception as e: response = "Failed to get orders" logger.error(f"response: {response}\n" f"event: {event}\n" f"error: {repr(e)}") error = Error.UNDEFINED_ERROR status = ResponseStatus.FAILED status_code = StatusCode.INTERNAL_SERVER_ERROR utils.report_slack(1, str(error), SLACK_HOOK) traceback.print_exc() return generate_lambda_response(status_code, make_response_body(status, response, error), cors_enabled=True)
def execute(event, context): logger.info("Received request to execute order") try: username = event["requestContext"]["authorizer"]["claims"]["email"] payload = json.loads(event["body"]) required_keys = ["order_id", "payment_id", "payment_details"] if validate_dict(payload, required_keys): response = OrderService(obj_repo=repo).execute_order( username, payload) status_code = StatusCode.CREATED status = ResponseStatus.SUCCESS else: status_code = StatusCode.BAD_REQUEST status = ResponseStatus.FAILED response = "Bad Request" logger.error(response) logger.info(event) error = {} except ChannelCreationFailed as e: response = e.get_wallet_details() logger.error(f"response: {response}\n" f"event: {event}\n" f"error: CHANNEL_CREATION_FAILED") error = Error.CHANNEL_CREATION_FAILED status = ResponseStatus.FAILED status_code = StatusCode.INTERNAL_SERVER_ERROR utils.report_slack(1, str(error), SLACK_HOOK) except FundChannelFailed as e: response = "Failed to fund channel" logger.error(f"response: {response}\n" f"event: {event}\n" f"error: FUND_CHANNEL_FAILED") error = Error.FUND_CHANNEL_FAILED status = ResponseStatus.FAILED status_code = StatusCode.INTERNAL_SERVER_ERROR utils.report_slack(1, str(error), SLACK_HOOK) except Exception as e: response = "Failed to execute order" logger.error(f"response: {response}\n" f"event: {event}\n" f"error: {repr(e)}") error = Error.UNDEFINED_ERROR status = ResponseStatus.FAILED status_code = StatusCode.INTERNAL_SERVER_ERROR utils.report_slack(1, str(error), SLACK_HOOK) traceback.print_exc() return generate_lambda_response(status_code, make_response_body(status, response, error), cors_enabled=True)
def route_path(path, method, payload_dict, request_context, path_parameters): obj_order_service = OrderService(obj_repo=db[NETWORK_ID]) path_exist = True response_data = None if "/wallet" == path: username = request_context["authorizer"]["claims"]["email"] logger.info(f"Received request to get wallets for user:{username}") response_data = WalletService().get_wallets(username) elif "/wallet/channel" == path and method == 'GET': org_id = payload_dict["org_id"] username = request_context["authorizer"]["claims"]["email"] group_id = payload_dict["group_id"] response_data = WalletService().get_channel_details( username, org_id, group_id) elif re.match("(\/order\/)[^\/]*[/]{0,1}$", path): """ Format /order/{orderId} """ username = request_context["authorizer"]["claims"]["email"] order_id = path_parameters["order_id"] response_data = obj_order_service.get_order_details_by_order_id( username=username, order_id=order_id) elif "/wallet/register" == path and method == "POST": username = request_context["authorizer"]["claims"]["email"] response_data = WalletService().register_wallet( username=username, wallet_details=payload_dict) elif "/wallet/status" == path and method == "POST": username = request_context["authorizer"]["claims"]["email"] response_data = WalletService().set_default_wallet( username=username, address=payload_dict["address"]) else: path_exist = False return path_exist, response_data
def currency_to_token_conversion(event, context): try: logger.info(f"currency_to_token_conversion::event: {event}") valid_event = validate_dict( data_dict=event, required_keys=REQUIRED_KEYS_FOR_CURRENCY_TO_TOKEN_CONVERSION) if not valid_event: return generate_lambda_response(400, "Bad Request", cors_enabled=True) path_parameters = event["pathParameters"] query_string_parameters = event["queryStringParameters"] order_service = OrderService(obj_repo=repo) response_data = order_service.currency_to_token( currency=path_parameters["currency"], amount=query_string_parameters["amount"]) response = generate_lambda_response(200, { "status": "success", "data": response_data }, cors_enabled=True) logger.info(f"currency_to_token_conversion::response: {response}") except Exception as e: error_message = format_error_message( status=ResponseStatus.FAILED, error=repr(e), payload=path_parameters, net_id=NETWORK_ID, handler="currency-to-token-conversion") utils.report_slack(1, error_message, SLACK_HOOK) response = generate_lambda_response(500, error_message, cors_enabled=True) traceback.print_exc() return response
def initiate(event, context): logger.info("Received request to initiate order") try: username = event["requestContext"]["authorizer"]["claims"]["email"] payload = json.loads(event["body"]) required_keys = ["price", "item_details", "payment_method"] if validate_dict(payload, required_keys): response = OrderService(obj_repo=repo).initiate_order( username, payload) status_code = StatusCode.CREATED status = ResponseStatus.SUCCESS else: status_code = StatusCode.BAD_REQUEST status = ResponseStatus.FAILED error = {} response = "Bad Request" logger.error(f"response: {response}\n" f"event: {event}") error = {} except PaymentInitiateFailed: response = "Failed to initiate order" error = Error.PAYMENT_INITIATE_FAILED logger.error(f"response: {response}\n" f"event: {event}\n" f"error: PAYMENT_INITIATE_FAILED") status = ResponseStatus.FAILED utils.report_slack(1, error, SLACK_HOOK) status_code = StatusCode.INTERNAL_SERVER_ERROR traceback.print_exc() except Exception as e: response = "Failed to initiate order" logger.error(f"response: {response}\n" f"event: {event}\n" f"error: {repr(e)}") traceback.print_exc() error = Error.UNDEFINED_ERROR status = ResponseStatus.FAILED status_code = StatusCode.INTERNAL_SERVER_ERROR utils.report_slack(1, str(error), SLACK_HOOK) return generate_lambda_response(status_code, make_response_body(status, response, error), cors_enabled=True)
def cancel(event, context): logger.info("Received request for cancel order") try: path_parameters = event["pathParameters"] bad_request = False if path_parameters is None: bad_request = True else: order_id = path_parameters.get("order_id", None) if order_id is not None: response = OrderService( obj_repo=repo).cancel_order_for_given_order_id(order_id) else: bad_request = True error = {} if bad_request: status_code = StatusCode.BAD_REQUEST status = ResponseStatus.FAILED response = "Bad Request" logger.error(f"response: {response}\n" f"event: {event}") else: status_code = StatusCode.CREATED status = ResponseStatus.SUCCESS except Exception as e: response = "Failed to cancel order" error = Error.UNDEFINED_ERROR status = ResponseStatus.FAILED status_code = StatusCode.INTERNAL_SERVER_ERROR logger.error(f"response: {response}\n" f"event: {event}\n" f"error: {repr(e)}") utils.report_slack(1, error, SLACK_HOOK) traceback.print_exc() return generate_lambda_response(status_code, make_response_body(status, response, error), cors_enabled=True)
def setUp(self): self.NETWORKS_NAME = dict( (NETWORKS[netId]["name"], netId) for netId in NETWORKS.keys()) self.db = dict((netId, Repository(net_id=netId, NETWORKS=NETWORKS)) for netId in NETWORKS.keys()) self.order_service = OrderService(self.db[NETWORK_ID])
class TestOrderService(unittest.TestCase): def setUp(self): self.NETWORKS_NAME = dict( (NETWORKS[netId]["name"], netId) for netId in NETWORKS.keys()) self.db = dict((netId, Repository(net_id=netId, NETWORKS=NETWORKS)) for netId in NETWORKS.keys()) self.order_service = OrderService(self.db[NETWORK_ID]) def test_initiate_order(self): pass @patch("common.boto_utils.BotoUtils.invoke_lambda") def test_manage_create_order(self, mock_lambda_invoke): mock_lambda_invoke.return_value = { 'statusCode': 201, 'body': json.dumps({ "order_id": "e33404a2-f574-11e9-9a93-3abe2d8567d5", "item_details": { "item": "AGI", "quantity": 1, "org_id": "snet", "group_id": "GROUP-123", "service_id": "freecall", "recipient": "0x123", "order_type": "CREATE_WALLET_AND_CHANNEL", "wallet_address": "", "channel_id": "" }, "price": { "amount": 1, "currency": "USD" } }) } username = "******" item_details = { "item": "AGI", "quantity": 1, "org_id": "snet", "group_id": "GROUP-123", "service_id": "freecall", "recipient": "0x123", "order_type": "CREATE_WALLET_AND_CHANNEL", "wallet_address": "", "channel_id": "" } price = {"amount": 1, "currency": "USD"} order_details = self.order_service.manage_create_order( username, item_details, price) assert validate_dict(order_details, ["order_id", "item_details", "price"]) def test_manage_initiate_payment(self): pass def test_execute_order(self): pass def test_manage_execute_payment(self): pass def test_manage_process_order(self): pass def test_get_payment_address_for_org(self): pass def test_get_channel_for_topup(self): pass def test_get_order_details_by_order_id(self): pass def test_get_order_details_by_username(self): pass