From dfb6d21a970e61a21175efee138b21fd3ad245b5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 2 Oct 2019 11:24:49 -0700 Subject: [PATCH] Change 'json' to 'data' field for Python Requests (#303) * Switch usage of 'json' to 'data' in metadata APIs * Use 'raw_request=True' for Envoy client post/put APIs --- amundsen_application/api/metadata/v0.py | 7 +++--- .../api/utils/request_utils.py | 24 +++++++++---------- setup.py | 2 +- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/amundsen_application/api/metadata/v0.py b/amundsen_application/api/metadata/v0.py index cf544be24..09dfcdabe 100644 --- a/amundsen_application/api/metadata/v0.py +++ b/amundsen_application/api/metadata/v0.py @@ -1,6 +1,5 @@ -import json import logging - +import json from http import HTTPStatus from typing import Any, Dict, Optional @@ -265,7 +264,7 @@ def _log_put_table_description(*, table_key: str, description: str, source: str) url = '{0}/{1}/description'.format(table_endpoint, table_key) _log_put_table_description(table_key=table_key, description=description, source=src) - response = request_metadata(url=url, method='PUT', json=json.dumps({'description': description})) + response = request_metadata(url=url, method='PUT', data=json.dumps({'description': description})) status_code = response.status_code if status_code == HTTPStatus.OK: @@ -301,7 +300,7 @@ def _log_put_column_description(*, table_key: str, column_name: str, description url = '{0}/{1}/column/{2}/description'.format(table_endpoint, table_key, column_name) _log_put_column_description(table_key=table_key, column_name=column_name, description=description, source=src) - response = request_metadata(url=url, method='PUT', json=json.dumps({'description': description})) + response = request_metadata(url=url, method='PUT', data=json.dumps({'description': description})) status_code = response.status_code if status_code == HTTPStatus.OK: diff --git a/amundsen_application/api/utils/request_utils.py b/amundsen_application/api/utils/request_utils.py index 9758711e7..989d98608 100644 --- a/amundsen_application/api/utils/request_utils.py +++ b/amundsen_application/api/utils/request_utils.py @@ -16,14 +16,14 @@ def request_metadata(*, # type: ignore url: str, method: str = 'GET', timeout_sec: int = 0, - json: str = '{}'): + data=None): """ Helper function to make a request to metadata service. Sets the client and header information based on the configuration :param method: DELETE | GET | POST | PUT :param url: The request URL :param timeout_sec: Number of seconds before timeout is triggered. - :param json: Optional request payload + :param data: Optional request payload :return: """ if app.config['REQUEST_HEADERS_METHOD']: @@ -35,21 +35,21 @@ def request_metadata(*, # type: ignore client=app.config['METADATASERVICE_REQUEST_CLIENT'], headers=headers, timeout_sec=timeout_sec, - json=json) + data=data) def request_search(*, # type: ignore url: str, method: str = 'GET', timeout_sec: int = 0, - json: str = '{}'): + data=None): """ Helper function to make a request to search service. Sets the client and header information based on the configuration :param method: DELETE | GET | POST | PUT :param url: The request URL :param timeout_sec: Number of seconds before timeout is triggered. - :param json: Optional request payload + :param data: Optional request payload :return: """ if app.config['REQUEST_HEADERS_METHOD']: @@ -61,11 +61,11 @@ def request_search(*, # type: ignore client=app.config['SEARCHSERVICE_REQUEST_CLIENT'], headers=headers, timeout_sec=timeout_sec, - json=json) + data=data) # TODO: Define an interface for envoy_client -def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, json: str = '{}'): # type: ignore +def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, data=None): # type: ignore """ Wraps a request to use Envoy client and headers, if available :param method: DELETE | GET | POST | PUT @@ -73,7 +73,7 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js :param client: Optional Envoy client :param headers: Optional Envoy request headers :param timeout_sec: Number of seconds before timeout is triggered. Not used with Envoy - :param json: Optional request payload + :param data: Optional request payload :return: """ # If no timeout specified, use the one from the configurations. @@ -85,9 +85,9 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js elif method == 'GET': return client.get(url, headers=headers, raw_response=True) elif method == 'POST': - return client.post(url, headers=headers, raw_response=True, json=json) + return client.post(url, headers=headers, raw_response=True, raw_request=True, data=data) elif method == 'PUT': - return client.put(url, headers=headers, raw_response=True, json=json) + return client.put(url, headers=headers, raw_response=True, raw_request=True, data=data) else: raise Exception('Method not allowed: {}'.format(method)) else: @@ -97,8 +97,8 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js elif method == 'GET': return s.get(url, headers=headers, timeout=timeout_sec) elif method == 'POST': - return s.post(url, headers=headers, timeout=timeout_sec, json=json) + return s.post(url, headers=headers, timeout=timeout_sec, data=data) elif method == 'PUT': - return s.put(url, headers=headers, timeout=timeout_sec, json=json) + return s.put(url, headers=headers, timeout=timeout_sec, data=data) else: raise Exception('Method not allowed: {}'.format(method)) diff --git a/setup.py b/setup.py index 26a1ad8ac..c5665d2cf 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ def build_js() -> None: with open(requirements_path) as requirements_file: requirements = requirements_file.readlines() -__version__ = '1.0.8' +__version__ = '1.0.9' setup(