Skip to content

Commit

Permalink
Fixes "filter" Parameter in methods GET
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuscardosodeveloper committed Oct 16, 2023
1 parent a3e9242 commit 8295b38
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/tagoio_sdk/common/tagoio_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,27 @@ def validateParams(self) -> None:
raise Exception("Token is invalid")
pass

def _converter_dict_param_filter(self, params: dict) -> None:
if params is None or not params.get("filter"):
return
filter_params = params["filter"]
params.pop("filter")
for key, value in filter_params.items():
if isinstance(value, list):
for i, item in enumerate(value):
for sub_key, sub_value in item.items():
converted_key = f"filter[{key}][{i}][{sub_key}]"
params[converted_key] = sub_value
elif isinstance(value, dict):
for sub_key, sub_value in value.items():
converted_key = f"filter[{key}][{sub_key}]"
params[converted_key] = sub_value
else:
params[key] = value

def doRequest(self, params: DoRequestParams) -> dict[str, any]:
url = getConnectionURI(self.region)["api"]
self._converter_dict_param_filter(params=params.get("params", {}))
return apiRequest({**params, "url": url, "headers": {"token": self.token}})

@staticmethod
Expand Down
39 changes: 39 additions & 0 deletions tests/commom/test_tagoio_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from src.tagoio_sdk.common.tagoio_module import TagoIOModule


def test_converter_dict_param_filter_single_key_value():
"""
Test case 1: dictionary with single key-value pair
"""
params = {"filter": {"tags": [{"key": "org_id", "value": "123"}]}}
expected_result = {"filter[tags][0][key]": "org_id", "filter[tags][0][value]": "123"}

tokenFake = {"token": "fake_token"}
TagoIOModule(params=tokenFake)._converter_dict_param_filter(params) == expected_result

assert params == expected_result


def test_converter_dict_param_filter_multiple_key_value():
"""
Test case 2: dictionary with multiple key-value pairs
"""
params = {
"filter": {
"tags": [
{"key": "org_id", "value": "123"},
{"key": "device_type", "value": "test"}
]
}
}
expected_result = {
"filter[tags][0][key]": "org_id",
"filter[tags][0][value]": "123",
"filter[tags][1][key]": "device_type",
"filter[tags][1][value]": "test"
}

tokenFake = {"token": "fake_token"}
TagoIOModule(params=tokenFake)._converter_dict_param_filter(params) == expected_result

assert params == expected_result

0 comments on commit 8295b38

Please sign in to comment.