def get_service_for_given_org(event, context): obj_reg = Registry(obj_repo=db) org_id = event['pathParameters']['orgId'] service_id = event['pathParameters']['serviceId'] response_data = obj_reg.get_service_data_by_org_id_and_service_id( org_id=org_id, service_id=service_id) return generate_lambda_response(200, { "status": "success", "data": response_data }, cors_enabled=True)
def test_get_service_data_by_org_id_and_service_id_without_media(self): registry = Registry(obj_repo=db) self.tearDown() insert_organization_query = f"""INSERT INTO organization (row_id ,org_id, organization_name, owner_address, org_metadata_uri, org_email, org_assets_url, row_created, row_updated, description, assets_hash, contacts) VALUES(10, '{TEST_ORG_ID}', '{TEST_SERVICE_ID}', """ + """ 'owner_add', 'uri', 'email', '{"url":"google.com"}', '2021-01-08 05:48:26', '2021-01-08 05:48:26', 'description', '{}','{}');""" db.execute(insert_organization_query) insert_service_query = f"""INSERT INTO service (row_id ,org_id, service_id, service_path, ipfs_hash, is_curated, service_email, row_created, row_updated) VALUES(10, '{TEST_ORG_ID}', '{TEST_SERVICE_ID}', """ + """'service_path', 'test_ipfs_hash', 1, 'email', '2021-01-08 05:48:26', '2021-01-08 05:48:26');""" db.execute(insert_service_query) # intentionally omitted the value for demo_component_available inorder to test the default value insert_metadata_query = f"""INSERT INTO service_metadata (row_id ,service_row_id, org_id, service_id, display_name, description, short_description, url, json, model_ipfs_hash, encoding, `type`, mpe_address, assets_url, assets_hash, service_rating, ranking, contributors, row_created, row_updated) VALUES(10,10, '{TEST_ORG_ID}', '{TEST_SERVICE_ID}', """ + """ 'test_display_name', 'test_description', 'short description', 'test_url', '{"name":"test_name", "age":31, "city":"test_city"}', 'test_hash', 'proto', 'grpc', 'test_mpe_address','{"hero_image": "https://test-s3-push"}', '{"hero_image": "test_hero_image_hash"}','{"rating": 0.0, "total_users_rated": 0}', 1, '[{"name": "dummy dummy", "email_id": "*****@*****.**"}]', '2021-01-08 05:48:26', '2021-01-08 05:48:26')""" db.execute(insert_metadata_query) response = registry.get_service_data_by_org_id_and_service_id(TEST_ORG_ID, TEST_SERVICE_ID) self.assertDictEqual(response, GET_SERVICE_RESPONSE["BASE"])
def test_get_service_data_by_org_id_and_service_id_with_media(self): registry = Registry(obj_repo=db) self.tearDown() insert_organization_query = f"""INSERT INTO organization (row_id ,org_id, organization_name, owner_address, org_metadata_uri, org_email, org_assets_url, row_created, row_updated, description, assets_hash, contacts) VALUES(10,'{TEST_ORG_ID}', '{TEST_SERVICE_ID}', """ + """'owner_add', 'uri', 'email', '{"url":"google.com"}', '2021-01-08 05:48:26', '2021-01-08 05:48:26', 'description', '{}','{}');""" db.execute(insert_organization_query) insert_service_query = f"""INSERT INTO service (row_id ,org_id, service_id, service_path, ipfs_hash, is_curated, service_email, row_created, row_updated) VALUES(10,'{TEST_ORG_ID}', '{TEST_SERVICE_ID}', """ + """ 'service_path', 'test_ipfs_hash', 1, 'email', '2021-01-08 05:48:26', '2021-01-08 05:48:26');""" db.execute(insert_service_query) insert_metadata_query = f"""INSERT INTO service_metadata (row_id ,service_row_id, org_id, service_id, display_name, description, short_description,demo_component_available, url, json, model_ipfs_hash, encoding, `type`, mpe_address, assets_url, assets_hash, service_rating, ranking, contributors, row_created, row_updated) VALUES(10,10, '{TEST_ORG_ID}', '{TEST_SERVICE_ID}', """ + """ 'test_display_name', 'test_description', 'short description',0, 'test_url', '{"name":"test_name", "age":31, "city":"test_city"}', 'test_hash', 'proto', 'grpc', 'test_mpe_address','{"hero_image": "https://test-s3-push"}', '{"hero_image": "test_hero_image_hash"}','{"rating": 0.0, "total_users_rated": 0}', 1, '[{"name": "dummy dummy", "email_id": "*****@*****.**"}]', '2021-01-08 05:48:26', '2021-01-08 05:48:26')""" db.execute(insert_metadata_query) service_media_repo.add_item(ServiceMedia( row_id=8, service_row_id=10, org_id=TEST_ORG_ID, service_id=TEST_SERVICE_ID, url="test_media_s3_url", order=0, asset_type="grpc-stub", file_type="grpc-stub/nodejs", alt_text="", ipfs_url="", created_on="2021-06-11 14:21:25", updated_on="2021-06-11 14:21:25" )) service_media_repo.add_item(ServiceMedia( row_id=10, service_row_id=10, org_id=TEST_ORG_ID, service_id=TEST_SERVICE_ID, url="https://test-s3-push", order=5, asset_type="hero_image", file_type="image", alt_text="data is missing", ipfs_url="Qmbb7tmKZX2TSxDKsK6DEAbp3tPgNUYP11CC93Cft7EkFb", created_on="2021-01-08 13:31:50", updated_on="2021-01-08 13:31:50" )) response = registry.get_service_data_by_org_id_and_service_id(TEST_ORG_ID, TEST_SERVICE_ID) result = {} result.update(GET_SERVICE_RESPONSE["BASE"]) result.update(GET_SERVICE_RESPONSE["MEDIA"]) self.assertDictEqual(response, result) offchain_service_repo.add_item(OffchainServiceConfigDB( org_id=TEST_ORG_ID, service_id=TEST_SERVICE_ID, parameter_name="demo_component_url", parameter_value="sample_demo_url", created_on=modified_date, updated_on=modified_date, )) offchain_service_repo.add_item(OffchainServiceConfigDB( org_id=TEST_ORG_ID, service_id=TEST_SERVICE_ID, parameter_name="demo_component_status", parameter_value="PENDING", created_on=modified_date, updated_on=modified_date, )) offchain_service_repo.add_item(OffchainServiceConfigDB( org_id=TEST_ORG_ID, service_id=TEST_SERVICE_ID, parameter_name="demo_component_required", parameter_value=1, created_on=modified_date, updated_on=modified_date, )) response = registry.get_service_data_by_org_id_and_service_id(TEST_ORG_ID, TEST_SERVICE_ID) result = {} result.update(GET_SERVICE_RESPONSE["BASE"]) result.update(GET_SERVICE_RESPONSE["MEDIA"]) result.update({"demo_component": {'demo_component_url': 'sample_demo_url', 'demo_component_required': 1, 'demo_component_status': 'PENDING', 'demo_component_last_modified': "2021-08-19T00:01:20" }, "demo_component_required": 1 }) self.assertDictEqual(response, result)
def test_get_service_data_by_org_id_and_service_id_with_media(self): registry = Registry(obj_repo=db) self.clear_dependencies() insert_organization_query = """INSERT INTO organization (row_id ,org_id, organization_name, owner_address, org_metadata_uri, org_email, org_assets_url, row_created, row_updated, description, assets_hash, contacts) VALUES(10,'snet', 'gene-annotation-service', 'owner_add', 'uri', 'email', '{"url":"google.com"}', '2021-01-08 05:48:26', '2021-01-08 05:48:26', 'description', '{}','{}');""" db.execute(insert_organization_query) insert_service_query = """INSERT INTO service (row_id ,org_id, service_id, service_path, ipfs_hash, is_curated, service_email, row_created, row_updated) VALUES(10,'snet', 'gene-annotation-service', 'service_path', 'QmdGjaVYPMSGpC1qT3LDALSNCCu7JPf7j51H1GQirvQJYf', 1, 'email', '2021-01-08 05:48:26', '2021-01-08 05:48:26');""" db.execute(insert_service_query) insert_metadata_query = """INSERT INTO service_metadata (row_id ,service_row_id, org_id, service_id, display_name, description, short_description, url, json, model_ipfs_hash, encoding, `type`, mpe_address, assets_url, assets_hash, service_rating, ranking, contributors, row_created, row_updated) VALUES(10,10, 'snet', 'gene-annotation-service', 'Annotation Service', 'Use this service to annotate a humane genome with uniform terms, Reactome pathway memberships, and BioGrid protein interactions.', 'short description', 'https://mozi-ai.github.io/annotation-service/', '{"name":"John", "age":31, "city":"New York"}', 'QmXqonxB9EvNBe11J8oCYXMQAtPKAb2x8CyFLmQpkvVaLf', 'proto', 'grpc', '0x8FB1dC8df86b388C7e00689d1eCb533A160B4D0C','{"hero_image": "https://test-s3-push"}', '{"hero_image": "QmVcE6fEDP764ibadXTjZHk251Lmt5xAxdc4P9mPA4kksk/hero_gene-annotation-2b.png"}','{"rating": 0.0, "total_users_rated": 0}', 1, '[{"name": "dummy dummy", "email_id": "*****@*****.**"}]', '2021-01-08 05:48:26', '2021-01-08 05:48:26')""" db.execute(insert_metadata_query) insert_service_media = """INSERT INTO service_media (row_id,org_id, service_id, url, `order`, file_type, asset_type, alt_text, created_on, updated_on, ipfs_url, service_row_id) VALUES(10,'snet', 'gene-annotation-service', 'https://test-s3-push', 5, 'text', 'hero_image','data is missing', '2021-01-08 13:31:50', '2021-01-08 13:31:50', 'Qmbb7tmKZX2TSxDKsK6DEAbp3tPgNUYP11CC93Cft7EkFb/hero_fbprophet_forecast1', 10);""" db.execute(insert_service_media) response = registry.get_service_data_by_org_id_and_service_id( 'snet', 'gene-annotation-service') assert response == { 'service_row_id': 10, 'org_id': 'snet', 'service_id': 'gene-annotation-service', 'display_name': 'Annotation Service', 'description': 'Use this service to annotate a humane genome with uniform terms, Reactome pathway memberships, and BioGrid protein interactions.', 'short_description': 'short description', 'url': 'https://mozi-ai.github.io/annotation-service/', 'json': '{"name":"John", "age":31, "city":"New York"}', 'model_ipfs_hash': 'QmXqonxB9EvNBe11J8oCYXMQAtPKAb2x8CyFLmQpkvVaLf', 'encoding': 'proto', 'type': 'grpc', 'mpe_address': '0x8FB1dC8df86b388C7e00689d1eCb533A160B4D0C', 'service_rating': { 'rating': 0.0, 'total_users_rated': 0 }, 'ranking': 1, 'contributors': [{ 'name': 'dummy dummy', 'email_id': '*****@*****.**' }], 'service_path': 'service_path', 'ipfs_hash': 'QmdGjaVYPMSGpC1qT3LDALSNCCu7JPf7j51H1GQirvQJYf', 'is_curated': 1, 'service_email': 'email', 'organization_name': 'gene-annotation-service', 'owner_address': 'owner_add', 'org_metadata_uri': 'uri', 'org_email': 'email', 'org_assets_url': { 'url': 'google.com' }, 'org_description': 'description', 'contacts': {}, 'is_available': 0, 'groups': [], 'tags': [], 'media': [{ 'row_id': 10, 'url': 'https://test-s3-push', 'file_type': 'text', 'order': 5, 'alt_text': 'data is missing', "asset_type": "hero_image" }] }
def request_handler(event, context): print(event) if 'path' not in event: return get_response(400, "Bad Request") try: payload_dict = None path = event['path'].lower() path = re.sub(r"^(\/contract-api)", "", path) stage = event['requestContext']['stage'] net_id = NETWORKS_NAME[stage] method = event['httpMethod'] response_data = None if method == 'POST': payload_dict = json.loads(event['body']) elif method == 'GET': payload_dict = event.get('queryStringParameters') else: return get_response(405, "Method Not Allowed") sub_path = path.split("/") if sub_path[1] in ["org", "service"]: obj_reg = Registry(obj_repo=db[net_id]) elif sub_path[1] in ["channel", "group"]: obj_mpe = MPE(net_id=net_id, obj_repo=db[net_id]) if "/org" == path: response_data = obj_reg.get_all_org() elif re.match("(\/org\/)[^\/]*(\/group)[/]{0,1}$", path): org_id = sub_path[2] response_data = obj_reg.get_all_group_for_org_id(org_id=org_id) elif "/service" == path and method == 'POST': payload_dict = {} if payload_dict is None else payload_dict response_data = obj_reg.get_all_srvcs(qry_param=payload_dict) elif "/service" == path and method == 'GET': response_data = obj_reg.get_filter_attribute( attribute=payload_dict["attribute"]) elif re.match("(\/org\/)[^\/]*(\/service\/)[^\/]*(\/group)[/]{0,1}$", path): org_id = sub_path[2] service_id = sub_path[4] response_data = obj_reg.get_group_info(org_id=org_id, service_id=service_id) elif "/channel" == path: response_data = obj_mpe.get_channels_by_user_address( user_address=payload_dict["user_address"], org_id=payload_dict.get("org_id", None), service_id=payload_dict.get("service_id", None)) elif re.match("(\/org\/)[^\/]*(\/service\/)[^\/]*[/]{0,1}$", path): org_id = sub_path[2] service_id = sub_path[4] response_data = obj_reg.get_service_data_by_org_id_and_service_id( org_id=org_id, service_id=service_id) elif re.match("(\/group\/)[^\/]*(\/channel\/)[^\/]*[/]{0,1}$", path): group_id = sub_path[2] channel_id = sub_path[4] response_data = obj_mpe.get_channel_data_by_group_id_and_channel_id( group_id=group_id, channel_id=channel_id) elif re.match("(\/org\/)[^\/]*[/]{0,1}$", path): org_id = sub_path[2] response_data = obj_reg.get_org_details(org_id=org_id) elif re.match("(\/org\/)[^\/]*(\/service\/)[^\/]*(\/rating)[/]{0,1}$", path) and method == 'POST': org_id = sub_path[2] service_id = sub_path[4] response_data = obj_reg.update_service_rating( org_id=org_id, service_id=service_id) else: return get_response(404, "Not Found") if response_data is None: err_msg = { 'status': 'failed', 'error': 'Bad Request', 'api': event['path'], 'payload': payload_dict, 'network_id': net_id } obj_util.report_slack(1, str(err_msg), SLACK_HOOK) response = get_response(500, err_msg) else: response = get_response(200, { "status": "success", "data": response_data }) except Exception as e: err_msg = { "status": "failed", "error": repr(e), 'api': event['path'], 'payload': payload_dict, 'network_id': net_id } obj_util.report_slack(1, str(err_msg), SLACK_HOOK) response = get_response(500, err_msg) traceback.print_exc() return response