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

ENG-3443 | Fixed get_machine url params for v1 endpoints. #58

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 0 additions & 2 deletions smsdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,6 @@ def get_machine_names(self, source_type=None, clean_strings_out=True):
query_params["source_type"] = source_type

machines = self.get_data_v1("machine_v1", "get_machines", True, **query_params)
# machine api endpoint doesn't seem to accept query params
machines = machines[machines["source_type"] == source_type]

if clean_strings_out:
return machines["source_clean"].to_list()
Expand Down
39 changes: 38 additions & 1 deletion smsdk/smsdk_entities/machine/machine_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from smsdk.utils import module_utility
from smsdk import config
from smsdk.ma_session import MaSession
from urllib.parse import urlencode, urlunparse

ENDPOINTS = json.loads(pkg_resources.read_text(config, "api_endpoints.json"))

Expand Down Expand Up @@ -42,10 +43,10 @@ def get_machines(self, *args, **kwargs):
Recommend to use 'enable_pagination':True for larger datasets
"""
url = "{}{}".format(self.base_url, ENDPOINTS["Machine"]["url_v1"])
url = self.modify_query_params(url, kwargs)
records = self._get_records_v1(
url, method="get", results_under="objects", **kwargs
)
# records = self._get_records_v1(url, method="get", **kwargs)
if not isinstance(records, List):
raise ValueError("Error - {}".format(records))
return records
Expand All @@ -64,3 +65,39 @@ def get_type_from_machine_name(self, machine_source, *args, **kwargs):
):
machine_type = record["type"]
return machine_type

def modify_query_params(self, url, kwargs):
ankitintg marked this conversation as resolved.
Show resolved Hide resolved
where = []
order_by = []
params = {}
for key, value in kwargs.items():
where_query = {}
orderby_query = {}
select_query = {}
if not key.startswith("_"):
where_query["name"] = key
where_query["value"] = value
where.append(where_query)
elif key == "_order_by":
orderby_query["name"] = (
value.replace("-", "") if value.startswith("-") else value
)
orderby_query["order"] = "desc" if value.startswith("-") else "asc"
order_by.append(orderby_query)
ankitintg marked this conversation as resolved.
Show resolved Hide resolved

select = [{"name": i} for i in eval(kwargs.get("_only", "[]"))]
ankitintg marked this conversation as resolved.
Show resolved Hide resolved
if len(where) > 0:
where = json.dumps(where, ensure_ascii=False)
params["where"] = where
if len(order_by) > 0:
order_by = json.dumps(order_by, ensure_ascii=False)
params["order_by"] = order_by
if len(select) > 0:
select = json.dumps(select, ensure_ascii=False)
params["select"] = select

if params:
encoded_params = urlencode(params)
url = urlunparse(("", "", url, "", encoded_params, ""))

return url
2 changes: 1 addition & 1 deletion tests/downtime/test_downtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MACHINE_INDEX = 0
START_DATETIME = datetime(2023, 4, 1)
END_DATETIME = datetime(2023, 4, 2)
EXPECTED_ROWS = 15
EXPECTED_ROWS = 18
EXPECTED_COL = 8
URL_V1 = "/v1/datatab/downtime"

Expand Down