dynatrace-oss / api-client-python

Dynatrace API Python client
Apache License 2.0
57 stars 22 forks source link

JSONDecodeError when parsing response from DELETE call #86

Closed Dynatrace-Jeroen-Hautekeete closed 1 month ago

Dynatrace-Jeroen-Hautekeete commented 1 month ago

Describe the bug I'm receiving a JSONDecodeError while validating response from call to dt.settings.delete_object(objectid) Json encoding is trying to retrieve the msg content, while no content is returned on a succesfull delete call

To Reproduce Steps to reproduce the behavior:

(116) #delete settings object by id (117) dresp = dt.settings.delete_object(so.object_id) (118) log.info("\tdeleted: %s %s", so.object_id, str(dresp))

Expected behavior dresp should contain [{ "code": 204 }]

Stacktrace

Traceback (most recent call last): File "C:\Data\git\ISHSSD\dynatrace_tools\monaco_externalid\venv\lib\site-packages\requests\models.py", line 974, in json return complexjson.loads(self.text, **kwargs) File "C:\Apps\Python310\lib\json__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Apps\Python310\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Apps\Python310\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Data\git\ISHSSD\dynatrace_tools\monaco_externalid\bin\update_settings_metadata.py", line 129, in main() File "C:\Data\git\ISHSSD\dynatrace_tools\monaco_externalid\bin\update_settings_metadata.py", line 117, in main dresp = dt.settings.delete_object(so.object_id) File "C:\Data\git\ISHSSD\dynatrace_tools\monaco_externalid\venv\lib\site-packages\dynatrace\environment_v2\settings.py", line 120, in delete_object ).json() File "C:\Data\git\ISHSSD\dynatrace_tools\monaco_externalid\venv\lib\site-packages\requests\models.py", line 978, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Dynatrace-James-Kitson commented 1 month ago

It looks like the delete call does not return a body when successful, so seems like the best way of handling it will be to change it to just return the response. Then you can check the response code and if there is an error indicating code inspect the body/json if present. Example of usage below:

objects = dt.settings.list_objects("builtin:anomaly-detection.metric-events")
for o in objects:
    print(o.object_id)
    r = dt.settings.delete_object(o.object_id)
    print(r.status_code)
    if r.status_code != 204:
        print(r.json())
    break

It's just a one line change which I will do now.