From d5d1eec0aa7cf81f907c7e23787fab998a77ea7c Mon Sep 17 00:00:00 2001 From: Adam Dorsey <1881857+asdorsey@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:58:07 -0500 Subject: [PATCH] fix: decode parameters if needed (#189) * fixed decoding parameters if needed * added test for bytes data formatting Co-authored-by: Adam Dorsey --- proxmoxer/backends/command_base.py | 7 ++++++- tests/test_command_base.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/proxmoxer/backends/command_base.py b/proxmoxer/backends/command_base.py index f8ca4a5..4d8e7b0 100644 --- a/proxmoxer/backends/command_base.py +++ b/proxmoxer/backends/command_base.py @@ -88,7 +88,12 @@ def request(self, method, url, data=None, params=None, headers=None): command = [f"{self.service}sh", cmd, url] # convert the options dict into a 2-tuple with the key formatted as a flag - option_pairs = [(f"-{k}", str(v)) for k, v in chain(data.items(), params.items())] + option_pairs = [] + for k, v in chain(data.items(), params.items()): + try: + option_pairs.append((f"-{k}", str(v, "utf-8"))) + except TypeError: + option_pairs.append((f"-{k}", str(v))) # add back in all the command arguments as their own pairs if data_command is not None: if isinstance(data_command, list): diff --git a/tests/test_command_base.py b/tests/test_command_base.py index c7b6b4a..6306eed 100644 --- a/tests/test_command_base.py +++ b/tests/test_command_base.py @@ -122,6 +122,22 @@ def test_request_data(self, mock_exec): "json", ] + def test_request_bytes_data(self, mock_exec): + resp = self._session.request( + "GET", self.base_url + "/fake/echo", data={"key": b"bytes-value"} + ) + + assert resp.status_code == 200 + assert resp.content == [ + "pvesh", + "get", + self.base_url + "/fake/echo", + "-key", + "bytes-value", + "--output-format", + "json", + ] + def test_request_qemu_exec(self, mock_exec): resp = self._session.request( "POST",