Unverified Commit cbca411e authored by Daniel's avatar Daniel Committed by GitHub

Simplify API endpoint configs (#74)

* Simplify API endpoint configs
parent ffe837ed
...@@ -20,9 +20,14 @@ REQUEST_SESSION_TIMEOUT = 10 ...@@ -20,9 +20,14 @@ REQUEST_SESSION_TIMEOUT = 10
metadata_blueprint = Blueprint('metadata', __name__, url_prefix='/api/metadata/v0') metadata_blueprint = Blueprint('metadata', __name__, url_prefix='/api/metadata/v0')
TABLE_ENDPOINT = '/table'
LAST_INDEXED_ENDPOINT = '/latest_updated_ts'
POPULAR_TABLES_ENDPOINT = '/popular_tables'
TAGS_ENDPOINT = '/tags'
def _get_table_endpoint() -> str: def _get_table_endpoint() -> str:
table_endpoint = app.config['METADATASERVICE_TABLE_ENDPOINT'] table_endpoint = app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT
if table_endpoint is None: if table_endpoint is None:
raise Exception('An request endpoint for table resources must be configured') raise Exception('An request endpoint for table resources must be configured')
return table_endpoint return table_endpoint
...@@ -62,7 +67,7 @@ def popular_tables() -> Response: ...@@ -62,7 +67,7 @@ def popular_tables() -> Response:
} }
try: try:
url = app.config['METADATASERVICE_POPULAR_TABLES_ENDPOINT'] url = app.config['METADATASERVICE_BASE'] + POPULAR_TABLES_ENDPOINT
# TODO: Create an abstraction for this logic that is reused many times # TODO: Create an abstraction for this logic that is reused many times
if app.config['METADATASERVICE_REQUEST_CLIENT'] is not None: if app.config['METADATASERVICE_REQUEST_CLIENT'] is not None:
...@@ -279,7 +284,7 @@ def get_last_indexed() -> Response: ...@@ -279,7 +284,7 @@ def get_last_indexed() -> Response:
Schema Defined Here: https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/system.py Schema Defined Here: https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/system.py
""" """
try: try:
url = app.config['METADATASERVICE_LAST_INDEXED_ENDPOINT'] url = app.config['METADATASERVICE_BASE'] + LAST_INDEXED_ENDPOINT
# TODO: Create an abstraction for this logic that is reused many times # TODO: Create an abstraction for this logic that is reused many times
if app.config['METADATASERVICE_REQUEST_CLIENT'] is not None: if app.config['METADATASERVICE_REQUEST_CLIENT'] is not None:
...@@ -471,7 +476,7 @@ def get_tags() -> Response: ...@@ -471,7 +476,7 @@ def get_tags() -> Response:
Schema Defined Here: https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/tag.py Schema Defined Here: https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/tag.py
""" """
try: try:
url = app.config['METADATASERVICE_TAGS_ENDPOINT'] url = app.config['METADATASERVICE_BASE'] + TAGS_ENDPOINT
# TODO: Create an abstraction for this logic that is reused many times # TODO: Create an abstraction for this logic that is reused many times
if app.config['METADATASERVICE_REQUEST_CLIENT'] is not None: if app.config['METADATASERVICE_REQUEST_CLIENT'] is not None:
......
...@@ -25,6 +25,8 @@ valid_search_fields = { ...@@ -25,6 +25,8 @@ valid_search_fields = {
'column' 'column'
} }
SEARCH_ENDPOINT = '/search'
def _create_error_response(*, message: str, payload: Dict, status_code: int) -> Response: def _create_error_response(*, message: str, payload: Dict, status_code: int) -> Response:
logging.info(message) logging.info(message)
...@@ -94,7 +96,7 @@ def _create_url_with_field(*, search_term: str, page_index: int) -> str: ...@@ -94,7 +96,7 @@ def _create_url_with_field(*, search_term: str, page_index: int) -> str:
field_val = search_field[1].lower() field_val = search_field[1].lower()
search_term = ' '.join(fields[1:]) search_term = ' '.join(fields[1:])
url = '{0}/field/{1}/field_val/{2}' \ url = '{0}/field/{1}/field_val/{2}' \
'?page_index={3}'.format(app.config['SEARCHSERVICE_ENDPOINT'], '?page_index={3}'.format(app.config['SEARCHSERVICE_BASE'] + SEARCH_ENDPOINT,
field_key, field_key,
field_val, field_val,
page_index) page_index)
...@@ -192,7 +194,7 @@ def _search_table(*, search_term: str, page_index: int) -> Dict[str, Any]: ...@@ -192,7 +194,7 @@ def _search_table(*, search_term: str, page_index: int) -> Dict[str, Any]:
url = _create_url_with_field(search_term=search_term, url = _create_url_with_field(search_term=search_term,
page_index=page_index) page_index=page_index)
else: else:
url = '{0}?query_term={1}&page_index={2}'.format(app.config['SEARCHSERVICE_ENDPOINT'], url = '{0}?query_term={1}&page_index={2}'.format(app.config['SEARCHSERVICE_BASE'] + SEARCH_ENDPOINT,
search_term, search_term,
page_index) page_index)
......
...@@ -28,19 +28,12 @@ class LocalConfig(Config): ...@@ -28,19 +28,12 @@ class LocalConfig(Config):
LOCAL_HOST = '0.0.0.0' LOCAL_HOST = '0.0.0.0'
SEARCHSERVICE_REQUEST_CLIENT = None SEARCHSERVICE_REQUEST_CLIENT = None
SEARCHSERVICE_ENDPOINT = 'http://{LOCAL_HOST}:{PORT}/search'.format(LOCAL_HOST=LOCAL_HOST, PORT=SEARCH_PORT)
SEARCHSERVICE_REQUEST_HEADERS = None SEARCHSERVICE_REQUEST_HEADERS = None
SEARCHSERVICE_BASE = 'http://{LOCAL_HOST}:{PORT}'.format(LOCAL_HOST=LOCAL_HOST, PORT=SEARCH_PORT)
METADATASERVICE_REQUEST_CLIENT = None METADATASERVICE_REQUEST_CLIENT = None
METADATASERVICE_POPULAR_TABLES_ENDPOINT = \
'http://{LOCAL_HOST}:{PORT}/popular_tables'.format(LOCAL_HOST=LOCAL_HOST, PORT=METADATA_PORT)
METADATASERVICE_LAST_INDEXED_ENDPOINT = \
'http://{LOCAL_HOST}:{PORT}/latest_updated_ts'.format(LOCAL_HOST=LOCAL_HOST, PORT=METADATA_PORT)
METADATASERVICE_TABLE_ENDPOINT = \
'http://{LOCAL_HOST}:{PORT}/table'.format(LOCAL_HOST=LOCAL_HOST, PORT=METADATA_PORT)
METADATASERVICE_TAGS_ENDPOINT = \
'http://{LOCAL_HOST}:{PORT}/tags'.format(LOCAL_HOST=LOCAL_HOST, PORT=METADATA_PORT)
METADATASERVICE_REQUEST_HEADERS = None METADATASERVICE_REQUEST_HEADERS = None
METADATASERVICE_BASE = 'http://{LOCAL_HOST}:{PORT}'.format(LOCAL_HOST=LOCAL_HOST, PORT=METADATA_PORT)
AUTH_USER_METHOD = None AUTH_USER_METHOD = None
GET_PROFILE_URL = None GET_PROFILE_URL = None
......
...@@ -5,6 +5,8 @@ import unittest ...@@ -5,6 +5,8 @@ import unittest
from http import HTTPStatus from http import HTTPStatus
from amundsen_application import create_app from amundsen_application import create_app
from amundsen_application.api.metadata.v0 import \
TABLE_ENDPOINT, LAST_INDEXED_ENDPOINT, POPULAR_TABLES_ENDPOINT, TAGS_ENDPOINT
local_app = create_app('amundsen_application.config.LocalConfig') local_app = create_app('amundsen_application.config.LocalConfig')
...@@ -167,7 +169,7 @@ class MetadataTest(unittest.TestCase): ...@@ -167,7 +169,7 @@ class MetadataTest(unittest.TestCase):
Test successful popular_tables request Test successful popular_tables request
:return: :return:
""" """
responses.add(responses.GET, local_app.config['METADATASERVICE_POPULAR_TABLES_ENDPOINT'], responses.add(responses.GET, local_app.config['METADATASERVICE_BASE'] + POPULAR_TABLES_ENDPOINT,
json=self.mock_popular_tables, status=HTTPStatus.OK) json=self.mock_popular_tables, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -183,7 +185,7 @@ class MetadataTest(unittest.TestCase): ...@@ -183,7 +185,7 @@ class MetadataTest(unittest.TestCase):
returned to the React application returned to the React application
:return: :return:
""" """
responses.add(responses.GET, local_app.config['METADATASERVICE_POPULAR_TABLES_ENDPOINT'], responses.add(responses.GET, local_app.config['METADATASERVICE_BASE'] + POPULAR_TABLES_ENDPOINT,
json=self.mock_popular_tables, status=HTTPStatus.BAD_REQUEST) json=self.mock_popular_tables, status=HTTPStatus.BAD_REQUEST)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -197,7 +199,7 @@ class MetadataTest(unittest.TestCase): ...@@ -197,7 +199,7 @@ class MetadataTest(unittest.TestCase):
results from the metadata service results from the metadata service
:return: :return:
""" """
responses.add(responses.GET, local_app.config['METADATASERVICE_POPULAR_TABLES_ENDPOINT'], responses.add(responses.GET, local_app.config['METADATASERVICE_BASE'] + POPULAR_TABLES_ENDPOINT,
json={'popular_tables': None}, status=HTTPStatus.OK) json={'popular_tables': None}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -210,7 +212,7 @@ class MetadataTest(unittest.TestCase): ...@@ -210,7 +212,7 @@ class MetadataTest(unittest.TestCase):
Test successful get_table_metadata request Test successful get_table_metadata request
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table'
responses.add(responses.GET, url, json=self.mock_metadata, status=HTTPStatus.OK) responses.add(responses.GET, url, json=self.mock_metadata, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -235,7 +237,7 @@ class MetadataTest(unittest.TestCase): ...@@ -235,7 +237,7 @@ class MetadataTest(unittest.TestCase):
Test successful update_table_owner request Test successful update_table_owner request
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/owner/test' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table/owner/test'
responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK) responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -257,7 +259,7 @@ class MetadataTest(unittest.TestCase): ...@@ -257,7 +259,7 @@ class MetadataTest(unittest.TestCase):
Test successful get_last_indexed request Test successful get_last_indexed request
:return: :return:
""" """
responses.add(responses.GET, local_app.config['METADATASERVICE_LAST_INDEXED_ENDPOINT'], responses.add(responses.GET, local_app.config['METADATASERVICE_BASE'] + LAST_INDEXED_ENDPOINT,
json={'neo4j_latest_timestamp': 1538352000}, status=HTTPStatus.OK) json={'neo4j_latest_timestamp': 1538352000}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -273,7 +275,7 @@ class MetadataTest(unittest.TestCase): ...@@ -273,7 +275,7 @@ class MetadataTest(unittest.TestCase):
to be returned to the React application to be returned to the React application
:return: :return:
""" """
responses.add(responses.GET, local_app.config['METADATASERVICE_LAST_INDEXED_ENDPOINT'], responses.add(responses.GET, local_app.config['METADATASERVICE_BASE'] + LAST_INDEXED_ENDPOINT,
json=None, status=HTTPStatus.BAD_REQUEST) json=None, status=HTTPStatus.BAD_REQUEST)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -286,7 +288,7 @@ class MetadataTest(unittest.TestCase): ...@@ -286,7 +288,7 @@ class MetadataTest(unittest.TestCase):
Test successful get_table_description request Test successful get_table_description request
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/description' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table/description'
responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.OK) responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -305,7 +307,7 @@ class MetadataTest(unittest.TestCase): ...@@ -305,7 +307,7 @@ class MetadataTest(unittest.TestCase):
to be returned to the React application to be returned to the React application
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/description' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table/description'
responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.BAD_REQUEST) responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.BAD_REQUEST)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -321,7 +323,7 @@ class MetadataTest(unittest.TestCase): ...@@ -321,7 +323,7 @@ class MetadataTest(unittest.TestCase):
Test successful put_table_description request Test successful put_table_description request
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/description/test' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table/description/test'
responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK) responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -344,7 +346,8 @@ class MetadataTest(unittest.TestCase): ...@@ -344,7 +346,8 @@ class MetadataTest(unittest.TestCase):
Test successful get_column_description request Test successful get_column_description request
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/column/colA/description' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + \
'/db://cluster.schema/table/column/colA/description'
responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.OK) responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -370,7 +373,8 @@ class MetadataTest(unittest.TestCase): ...@@ -370,7 +373,8 @@ class MetadataTest(unittest.TestCase):
to be returned to the React application to be returned to the React application
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/column/colA/description' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + \
'/db://cluster.schema/table/column/colA/description'
responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.BAD_REQUEST) responses.add(responses.GET, url, json={'description': 'This is a test'}, status=HTTPStatus.BAD_REQUEST)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -393,8 +397,8 @@ class MetadataTest(unittest.TestCase): ...@@ -393,8 +397,8 @@ class MetadataTest(unittest.TestCase):
Test successful put_column_description request Test successful put_column_description request
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] \ url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + \
+ '/db://cluster.schema/table/column/col/description/test' '/db://cluster.schema/table/column/col/description/test'
responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK) responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -418,7 +422,7 @@ class MetadataTest(unittest.TestCase): ...@@ -418,7 +422,7 @@ class MetadataTest(unittest.TestCase):
Test successful fetch of all tags Test successful fetch of all tags
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TAGS_ENDPOINT'] url = local_app.config['METADATASERVICE_BASE'] + TAGS_ENDPOINT
responses.add(responses.GET, url, json=self.mock_tags, status=HTTPStatus.OK) responses.add(responses.GET, url, json=self.mock_tags, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -432,7 +436,7 @@ class MetadataTest(unittest.TestCase): ...@@ -432,7 +436,7 @@ class MetadataTest(unittest.TestCase):
Test adding a tag on a table Test adding a tag on a table
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/tag/tag_5' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table/tag/tag_5'
responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK) responses.add(responses.PUT, url, json={}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -454,7 +458,7 @@ class MetadataTest(unittest.TestCase): ...@@ -454,7 +458,7 @@ class MetadataTest(unittest.TestCase):
Test deleting a tag on a table Test deleting a tag on a table
:return: :return:
""" """
url = local_app.config['METADATASERVICE_TABLE_ENDPOINT'] + '/db://cluster.schema/table/tag/tag_5' url = local_app.config['METADATASERVICE_BASE'] + TABLE_ENDPOINT + '/db://cluster.schema/table/tag/tag_5'
responses.add(responses.DELETE, url, json={}, status=HTTPStatus.OK) responses.add(responses.DELETE, url, json={}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
......
...@@ -5,7 +5,7 @@ import unittest ...@@ -5,7 +5,7 @@ import unittest
from http import HTTPStatus from http import HTTPStatus
from amundsen_application import create_app from amundsen_application import create_app
from amundsen_application.api.search.v0 import _create_url_with_field from amundsen_application.api.search.v0 import _create_url_with_field, SEARCH_ENDPOINT
local_app = create_app('amundsen_application.config.LocalConfig', 'static/templates') local_app = create_app('amundsen_application.config.LocalConfig', 'static/templates')
...@@ -121,7 +121,7 @@ class SearchTest(unittest.TestCase): ...@@ -121,7 +121,7 @@ class SearchTest(unittest.TestCase):
Test request success Test request success
:return: :return:
""" """
responses.add(responses.GET, local_app.config['SEARCHSERVICE_ENDPOINT'], responses.add(responses.GET, local_app.config['SEARCHSERVICE_BASE'] + SEARCH_ENDPOINT,
json=self.mock_search_table_results, status=HTTPStatus.OK) json=self.mock_search_table_results, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -139,7 +139,7 @@ class SearchTest(unittest.TestCase): ...@@ -139,7 +139,7 @@ class SearchTest(unittest.TestCase):
Test request failure if search endpoint returns non-200 http code Test request failure if search endpoint returns non-200 http code
:return: :return:
""" """
responses.add(responses.GET, local_app.config['SEARCHSERVICE_ENDPOINT'], responses.add(responses.GET, local_app.config['SEARCHSERVICE_BASE'] + SEARCH_ENDPOINT,
json=self.mock_search_table_results, status=HTTPStatus.INTERNAL_SERVER_ERROR) json=self.mock_search_table_results, status=HTTPStatus.INTERNAL_SERVER_ERROR)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -153,7 +153,7 @@ class SearchTest(unittest.TestCase): ...@@ -153,7 +153,7 @@ class SearchTest(unittest.TestCase):
from the search endpoint from the search endpoint
:return: :return:
""" """
responses.add(responses.GET, local_app.config['SEARCHSERVICE_ENDPOINT'], responses.add(responses.GET, local_app.config['SEARCHSERVICE_BASE'] + SEARCH_ENDPOINT,
json=self.bad_search_results, status=HTTPStatus.OK) json=self.bad_search_results, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
...@@ -166,7 +166,7 @@ class SearchTest(unittest.TestCase): ...@@ -166,7 +166,7 @@ class SearchTest(unittest.TestCase):
Test search request if user search with colon Test search request if user search with colon
:return: :return:
""" """
responses.add(responses.GET, local_app.config['SEARCHSERVICE_ENDPOINT'], responses.add(responses.GET, local_app.config['SEARCHSERVICE_BASE'] + SEARCH_ENDPOINT,
json={}, status=HTTPStatus.OK) json={}, status=HTTPStatus.OK)
with local_app.test_client() as test: with local_app.test_client() as test:
......
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