Skip to content

Commit

Permalink
Change 'json' to 'data' field for Python Requests (#303)
Browse files Browse the repository at this point in the history
* Switch usage of 'json' to 'data' in metadata APIs
* Use 'raw_request=True' for Envoy client post/put APIs
  • Loading branch information
Daniel authored Oct 2, 2019
1 parent 675a29d commit dfb6d21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
7 changes: 3 additions & 4 deletions amundsen_application/api/metadata/v0.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging

import json
from http import HTTPStatus
from typing import Any, Dict, Optional

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 12 additions & 12 deletions amundsen_application/api/utils/request_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand All @@ -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']:
Expand All @@ -61,19 +61,19 @@ 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
:param url: The request URL
: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.
Expand All @@ -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:
Expand All @@ -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))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dfb6d21

Please sign in to comment.