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 json
from http import HTTPStatus
from typing import Any, Dict, Optional
......@@ -265,7 +264,7 @@ def put_table_description() -> Response:
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 put_column_description() -> Response:
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:
......
......@@ -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))
......@@ -34,7 +34,7 @@ requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'r
with open(requirements_path) as requirements_file:
requirements = requirements_file.readlines()
__version__ = '1.0.8'
__version__ = '1.0.9'
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