Skip to content

Commit

Permalink
Fix boolean query parameters to GET requests (#219)
Browse files Browse the repository at this point in the history
* init

* address aaron's comments
  • Loading branch information
andrewmchen authored Mar 20, 2019
1 parent 1fd58dd commit 3c0befe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
13 changes: 12 additions & 1 deletion databricks_cli/sdk/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def perform_query(self, method, path, data = {}, headers = None):
with warnings.catch_warnings():
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
if method == 'GET':
resp = self.session.request(method, self.url + path, params = data,
translated_data = {k: _translate_boolean_to_query_param(data[k]) for k in data}
resp = self.session.request(method, self.url + path, params = translated_data,
verify = self.verify, headers = headers)
else:
resp = self.session.request(method, self.url + path, data = json.dumps(data),
Expand All @@ -127,3 +128,13 @@ def perform_query(self, method, path, data = {}, headers = None):
pass
raise requests.exceptions.HTTPError(message, response=e.response)
return resp.json()


def _translate_boolean_to_query_param(value):
assert not isinstance(value, list), 'GET parameters cannot pass list of objects'
if isinstance(value, bool):
if value:
return 'true'
else:
return 'false'
return value
38 changes: 38 additions & 0 deletions tests/sdk/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def test_api_client_constructor():
# echo -n "apple:banana" | base64
assert client.default_headers['Authorization'] == 'Basic YXBwbGU6YmFuYW5h'


requests_mock.mock.case_sensitive = True

@pytest.fixture()
def m():
with requests_mock.Mocker() as m:
Expand All @@ -61,6 +64,41 @@ def test_content_from_server_on_error(m):
client.perform_query('GET', '/endpoint')
assert error_message_contains in e.value.message


def test_get_request_with_true_param(m):
data = {'cucumber': 'dade'}
m.get('https://databricks.com/api/2.0/endpoint?active_only=true', text=json.dumps(data))
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
assert client.perform_query('GET', '/endpoint', {'active_only': True}) == data


def test_get_request_with_false_param(m):
data = {'cucumber': 'dade'}
m.get('https://databricks.com/api/2.0/endpoint?active_only=false', text=json.dumps(data))
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
assert client.perform_query('GET', '/endpoint', {'active_only': False}) == data


def test_get_request_with_int_param(m):
data = {'cucumber': 'dade'}
m.get('https://databricks.com/api/2.0/endpoint?job_id=0', text=json.dumps(data))
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
assert client.perform_query('GET', '/endpoint', {'job_id': 0}) == data


def test_get_request_with_float_param(m):
data = {'cucumber': 'dade'}
m.get('https://databricks.com/api/2.0/endpoint?job_id=0.25', text=json.dumps(data))
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
assert client.perform_query('GET', '/endpoint', {'job_id': 0.25}) == data


def test_get_request_with_list_param(m):
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
with pytest.raises(AssertionError, message='cannot pass list of objects'):
client.perform_query('GET', '/endpoint', {'job_id': ['a','b']})


def test_api_client_url_parsing():
client = ApiClient(host='https://databricks.com')
assert client.url == 'https://databricks.com/api/2.0'
Expand Down

0 comments on commit 3c0befe

Please sign in to comment.