Skip to content

Commit

Permalink
Strip None values when sending JSON (#249)
Browse files Browse the repository at this point in the history
* Strip None values when sending JSON

* Recursively strip
  • Loading branch information
pederhan authored May 29, 2024
1 parent b5fac79 commit 0c198c5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions mreg_cli/utilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ def result_check(result: Response, operation_type: str, url: str) -> None:
cli_warning(message)


def _strip_none(data: dict[str, Any]) -> dict[str, Any]:
"""Recursively strip None values from a dictionary."""
new: dict[str, Any] = {}
for key, value in data.items():
if value is not None:
if isinstance(value, dict):
v = _strip_none(value) # pyright: ignore[reportUnknownArgumentType]
if v:
new[key] = v
else:
new[key] = value
return new


def _request_wrapper(
operation_type: str,
path: str,
Expand All @@ -217,6 +231,10 @@ def _request_wrapper(
params = {}
url = urljoin(MregCliConfig().get_url(), path)

# Strip None values from data
if data:
data = _strip_none(data)

result = getattr(session, operation_type)(
url,
params=params,
Expand Down

0 comments on commit 0c198c5

Please sign in to comment.