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

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
parent 675a29de
import json
import logging import logging
import json
from http import HTTPStatus from http import HTTPStatus
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
...@@ -265,7 +264,7 @@ def put_table_description() -> Response: ...@@ -265,7 +264,7 @@ def put_table_description() -> Response:
url = '{0}/{1}/description'.format(table_endpoint, table_key) url = '{0}/{1}/description'.format(table_endpoint, table_key)
_log_put_table_description(table_key=table_key, description=description, source=src) _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 status_code = response.status_code
if status_code == HTTPStatus.OK: if status_code == HTTPStatus.OK:
...@@ -301,7 +300,7 @@ def put_column_description() -> Response: ...@@ -301,7 +300,7 @@ def put_column_description() -> Response:
url = '{0}/{1}/column/{2}/description'.format(table_endpoint, table_key, column_name) 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) _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 status_code = response.status_code
if status_code == HTTPStatus.OK: if status_code == HTTPStatus.OK:
......
...@@ -16,14 +16,14 @@ def request_metadata(*, # type: ignore ...@@ -16,14 +16,14 @@ def request_metadata(*, # type: ignore
url: str, url: str,
method: str = 'GET', method: str = 'GET',
timeout_sec: int = 0, timeout_sec: int = 0,
json: str = '{}'): data=None):
""" """
Helper function to make a request to metadata service. Helper function to make a request to metadata service.
Sets the client and header information based on the configuration Sets the client and header information based on the configuration
:param method: DELETE | GET | POST | PUT :param method: DELETE | GET | POST | PUT
:param url: The request URL :param url: The request URL
:param timeout_sec: Number of seconds before timeout is triggered. :param timeout_sec: Number of seconds before timeout is triggered.
:param json: Optional request payload :param data: Optional request payload
:return: :return:
""" """
if app.config['REQUEST_HEADERS_METHOD']: if app.config['REQUEST_HEADERS_METHOD']:
...@@ -35,21 +35,21 @@ def request_metadata(*, # type: ignore ...@@ -35,21 +35,21 @@ def request_metadata(*, # type: ignore
client=app.config['METADATASERVICE_REQUEST_CLIENT'], client=app.config['METADATASERVICE_REQUEST_CLIENT'],
headers=headers, headers=headers,
timeout_sec=timeout_sec, timeout_sec=timeout_sec,
json=json) data=data)
def request_search(*, # type: ignore def request_search(*, # type: ignore
url: str, url: str,
method: str = 'GET', method: str = 'GET',
timeout_sec: int = 0, timeout_sec: int = 0,
json: str = '{}'): data=None):
""" """
Helper function to make a request to search service. Helper function to make a request to search service.
Sets the client and header information based on the configuration Sets the client and header information based on the configuration
:param method: DELETE | GET | POST | PUT :param method: DELETE | GET | POST | PUT
:param url: The request URL :param url: The request URL
:param timeout_sec: Number of seconds before timeout is triggered. :param timeout_sec: Number of seconds before timeout is triggered.
:param json: Optional request payload :param data: Optional request payload
:return: :return:
""" """
if app.config['REQUEST_HEADERS_METHOD']: if app.config['REQUEST_HEADERS_METHOD']:
...@@ -61,11 +61,11 @@ def request_search(*, # type: ignore ...@@ -61,11 +61,11 @@ def request_search(*, # type: ignore
client=app.config['SEARCHSERVICE_REQUEST_CLIENT'], client=app.config['SEARCHSERVICE_REQUEST_CLIENT'],
headers=headers, headers=headers,
timeout_sec=timeout_sec, timeout_sec=timeout_sec,
json=json) data=data)
# TODO: Define an interface for envoy_client # 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 Wraps a request to use Envoy client and headers, if available
:param method: DELETE | GET | POST | PUT :param method: DELETE | GET | POST | PUT
...@@ -73,7 +73,7 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js ...@@ -73,7 +73,7 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js
:param client: Optional Envoy client :param client: Optional Envoy client
:param headers: Optional Envoy request headers :param headers: Optional Envoy request headers
:param timeout_sec: Number of seconds before timeout is triggered. Not used with Envoy :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: :return:
""" """
# If no timeout specified, use the one from the configurations. # 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 ...@@ -85,9 +85,9 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js
elif method == 'GET': elif method == 'GET':
return client.get(url, headers=headers, raw_response=True) return client.get(url, headers=headers, raw_response=True)
elif method == 'POST': 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': 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: else:
raise Exception('Method not allowed: {}'.format(method)) raise Exception('Method not allowed: {}'.format(method))
else: else:
...@@ -97,8 +97,8 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js ...@@ -97,8 +97,8 @@ def request_wrapper(method: str, url: str, client, headers, timeout_sec: int, js
elif method == 'GET': elif method == 'GET':
return s.get(url, headers=headers, timeout=timeout_sec) return s.get(url, headers=headers, timeout=timeout_sec)
elif method == 'POST': 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': 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: else:
raise Exception('Method not allowed: {}'.format(method)) raise Exception('Method not allowed: {}'.format(method))
...@@ -34,7 +34,7 @@ requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'r ...@@ -34,7 +34,7 @@ requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'r
with open(requirements_path) as requirements_file: with open(requirements_path) as requirements_file:
requirements = requirements_file.readlines() requirements = requirements_file.readlines()
__version__ = '1.0.8' __version__ = '1.0.9'
setup( setup(
......
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