thingsboard-python-rest-client icon indicating copy to clipboard operation
thingsboard-python-rest-client copied to clipboard

rest_client.download_ota_package : UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

Open ysimonx opened this issue 3 years ago • 2 comments

While trying to download a gz file as OTA package thanks to thingsboard-python-rest-client I have this error

Traceback (most recent call last):
  File "ota.py", line 31, in <module>
    downloads=rest_client.download_ota_package(str(first_package.id.id))
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/rest_client_ce.py", line 589, in download_ota_package
    return self.ota_package_controller.download_ota_package_using_get(ota_package_id=ota_package_id)
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api/api_ce/ota_package_controller_api.py", line 142, in download_ota_package_using_get
    (data) = self.download_ota_package_using_get_with_http_info(ota_package_id, **kwargs)  # noqa: E501
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api/api_ce/ota_package_controller_api.py", line 201, in download_ota_package_using_get_with_http_info
    return self.api_client.call_api(
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api_client.py", line 409, in call_api
    return self.__call_api(resource_path, method,
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api_client.py", line 193, in __call_api
    response_data = self.request(
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api_client.py", line 431, in request
    return self.rest_client.GET(url,
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/rest.py", line 237, in GET
    return self.request("GET", url,
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/rest.py", line 225, in request
    r.data = r.data.decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte


The firmware package is a gz file

and of course, this file can not be considered as a "utf-8" string,

It should not be decoded in line 225 of rest.py

` if _preload_content: r = RESTResponse(r)

        # In the python 3, the response.data is bytes.
        # we need to decode it to string.
        if six.PY3:
            r.data = r.data.decode('utf8')

`

How to reproduce it :



with RestClientCE(base_url=url) as rest_client:
    try:
       
        rest_client.login(username=username, password=password)

       
        packs = rest_client.get_ota_packages_v1(100,0,None, None, None)

        package = packs.data[0]
        
        downloads=rest_client.download_ota_package(package.id.id)

        print(downloads)
        
    except ApiException as e:
        logging.exception(e)


ysimonx avatar Jul 05 '22 15:07 ysimonx

here is the workaround I made

  rest_client.login(username=username, password=password)
        
        token = rest_client.api_client.configuration.api_key["X-Authorization"]

        packs = rest_client.get_ota_packages_v1(100,0,None, None, None)
      

        first_package = packs.data[0]
      
        url = "https://xxxx.com"+ "/api/otaPackage/{otaPackageId}/download".replace("{otaPackageId}",first_package.id.id )
       
        with open(first_package.file_name, "wb") as f:
            r = requests.get(url,  headers={"X-Authorization":"Bearer " + token } )
            f.write(r.content)

ysimonx avatar Jul 06 '22 08:07 ysimonx

Hi @ysimonx ,

Thank you for your message, we will check and fix the rest client.

imbeacon avatar Jul 06 '22 10:07 imbeacon