Skip to content

Commit 3c0befe

Browse files
authored
Fix boolean query parameters to GET requests (#219)
* init * address aaron's comments
1 parent 1fd58dd commit 3c0befe

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

databricks_cli/sdk/api_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def perform_query(self, method, path, data = {}, headers = None):
111111
with warnings.catch_warnings():
112112
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
113113
if method == 'GET':
114-
resp = self.session.request(method, self.url + path, params = data,
114+
translated_data = {k: _translate_boolean_to_query_param(data[k]) for k in data}
115+
resp = self.session.request(method, self.url + path, params = translated_data,
115116
verify = self.verify, headers = headers)
116117
else:
117118
resp = self.session.request(method, self.url + path, data = json.dumps(data),
@@ -127,3 +128,13 @@ def perform_query(self, method, path, data = {}, headers = None):
127128
pass
128129
raise requests.exceptions.HTTPError(message, response=e.response)
129130
return resp.json()
131+
132+
133+
def _translate_boolean_to_query_param(value):
134+
assert not isinstance(value, list), 'GET parameters cannot pass list of objects'
135+
if isinstance(value, bool):
136+
if value:
137+
return 'true'
138+
else:
139+
return 'false'
140+
return value

tests/sdk/test_api_client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def test_api_client_constructor():
3535
# echo -n "apple:banana" | base64
3636
assert client.default_headers['Authorization'] == 'Basic YXBwbGU6YmFuYW5h'
3737

38+
39+
requests_mock.mock.case_sensitive = True
40+
3841
@pytest.fixture()
3942
def m():
4043
with requests_mock.Mocker() as m:
@@ -61,6 +64,41 @@ def test_content_from_server_on_error(m):
6164
client.perform_query('GET', '/endpoint')
6265
assert error_message_contains in e.value.message
6366

67+
68+
def test_get_request_with_true_param(m):
69+
data = {'cucumber': 'dade'}
70+
m.get('https://databricks.com/api/2.0/endpoint?active_only=true', text=json.dumps(data))
71+
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
72+
assert client.perform_query('GET', '/endpoint', {'active_only': True}) == data
73+
74+
75+
def test_get_request_with_false_param(m):
76+
data = {'cucumber': 'dade'}
77+
m.get('https://databricks.com/api/2.0/endpoint?active_only=false', text=json.dumps(data))
78+
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
79+
assert client.perform_query('GET', '/endpoint', {'active_only': False}) == data
80+
81+
82+
def test_get_request_with_int_param(m):
83+
data = {'cucumber': 'dade'}
84+
m.get('https://databricks.com/api/2.0/endpoint?job_id=0', text=json.dumps(data))
85+
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
86+
assert client.perform_query('GET', '/endpoint', {'job_id': 0}) == data
87+
88+
89+
def test_get_request_with_float_param(m):
90+
data = {'cucumber': 'dade'}
91+
m.get('https://databricks.com/api/2.0/endpoint?job_id=0.25', text=json.dumps(data))
92+
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
93+
assert client.perform_query('GET', '/endpoint', {'job_id': 0.25}) == data
94+
95+
96+
def test_get_request_with_list_param(m):
97+
client = ApiClient(user='apple', password='banana', host='https://databricks.com')
98+
with pytest.raises(AssertionError, message='cannot pass list of objects'):
99+
client.perform_query('GET', '/endpoint', {'job_id': ['a','b']})
100+
101+
64102
def test_api_client_url_parsing():
65103
client = ApiClient(host='https://databricks.com')
66104
assert client.url == 'https://databricks.com/api/2.0'

0 commit comments

Comments
 (0)