Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested query parameters in ApiClient.do() #249

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import urllib.parse
from datetime import datetime
from json import JSONDecodeError
from typing import Callable, Dict, Iterable, List, Optional, Union
from typing import Any, Callable, Dict, Iterable, List, Optional, Union

import requests
import requests.auth
Expand Down Expand Up @@ -911,7 +911,27 @@ def _fix_query_string(query: Optional[dict] = None) -> Optional[dict]:
# See: https://github.com/databricks/databricks-sdk-py/issues/142
if query is None:
return None
return {k: v if type(v) != bool else ('true' if v else 'false') for k, v in query.items()}
with_fixed_bools = {k: v if type(v) != bool else ('true' if v else 'false') for k, v in query.items()}

# Query parameters may be nested, e.g.
# {'filter_by': {'user_ids': [123, 456]}}
# The HTTP-compatible representation of this is
# filter_by.user_ids=123&filter_by.user_ids=456
# To achieve this, we convert the above dictionary to
# {'filter_by.user_ids': [123, 456]}
# See the following for more information:
# https://cloud.google.com/endpoints/docs/grpc-service-config/reference/rpc/google.api#google.api.HttpRule
def flatten_dict(d: Dict[str, Any]) -> Dict[str, Any]:
for k1, v1 in d.items():
if isinstance(v1, dict):
v1 = dict(flatten_dict(v1))
for k2, v2 in v1.items():
yield f"{k1}.{k2}", v2
else:
yield k1, v1

flattened = dict(flatten_dict(with_fixed_bools))
return flattened

def do(self,
method: str,
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/test_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datetime import datetime

from databricks.sdk.service.sql import QueryFilter, TimeRange


def test_query_history_list_with_filter(w):

def date_to_ms(date):
return int(datetime.strptime(date, '%Y-%m-%d').timestamp() * 1000)

filter = QueryFilter(query_start_time_range=TimeRange(start_time_ms=date_to_ms('2023-01-01'),
end_time_ms=date_to_ms('2023-01-02')))
queries = w.query_history.list(filter_by=filter)
for q in queries:
print(q)
Loading