From 42f5bf8a523ce48358111a58e251feedbdf5fff9 Mon Sep 17 00:00:00 2001 From: Moshe Eichler <78307768+MosheEichler@users.noreply.github.com> Date: Thu, 21 Sep 2023 15:03:11 +0300 Subject: [PATCH] Improve the way strings are handled when writing to files (#118) * check the returned data type (str, bytes) * revert api_client chnages * added str response * fix * changelog * gen-doce * backslash * code commit * cr * cr * path --- CHANGELOG.md | 3 +++ demisto_client/demisto_api/api_client.py | 8 ++++++-- gen-code.sh | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7f6b5f..635105e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ [PyPI History][1] [1]: https://pypi.org/project/demisto-py/#history +## 3.2.12 +* Fixed an issue where download command would fail when trying to convert bytes types to string. + ## 3.2.11 * Added advanced key authentication support for XSIAM and XSOAR 8+. * Added support for `DEMISTO_API_KEY_ID` and deprecated old `XSIAM_AUTH_ID`. diff --git a/demisto_client/demisto_api/api_client.py b/demisto_client/demisto_api/api_client.py index e98db49..20e7ce8 100644 --- a/demisto_client/demisto_api/api_client.py +++ b/demisto_client/demisto_api/api_client.py @@ -554,8 +554,12 @@ def __deserialize_file(self, response): content_disposition).group(1) path = os.path.join(os.path.dirname(path), filename) - with open(path, "wb") as f: - f.write(response.data) + if isinstance(response.data, str): + with open(path, 'w') as f: + f.write(response.data) + elif isinstance(response.data, bytes): + with open(path, 'wb') as f: + f.write(response.data) return path diff --git a/gen-code.sh b/gen-code.sh index 9065a7a..c660cab 100755 --- a/gen-code.sh +++ b/gen-code.sh @@ -160,6 +160,18 @@ from hashlib import sha256\\ \\ NONCE_POSSIBLE_VALUES = string.ascii_letters + string.digits/" demisto_client/demisto_api/api_client.py # End fix return partial errors +# Fix return str response +select_start_line=`grep 'with open(path, "wb") as f:' demisto_client/demisto_api/api_client.py -n | cut -f1 -d: | tail -1 | tr -d "\\n"` +select_end_line=$((select_start_line + 1)) +sed -i "${INPLACE[@]}" -e "${select_start_line},${select_end_line}d" demisto_client/demisto_api/api_client.py +sed -i "${INPLACE[@]}" -e "${select_start_line}i\\ +\ \ \ \ \ \ \ \ if isinstance(response.data, str):\\ +\ \ \ \ \ \ \ \ \ \ \ \ with open(path, 'w') as f:\\ +\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f.write(response.data)\\ +\ \ \ \ \ \ \ \ elif isinstance(response.data, bytes):\\ +\ \ \ \ \ \ \ \ \ \ \ \ with open(path, 'wb') as f:\\ +\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f.write(response.data)" demisto_client/demisto_api/api_client.py +# End fix return str response # remove files not used rm .travis.yml rm git_push.sh