thingsboard / thingsboard-python-rest-client

ThingsBoard REST API client
https://thingsboard.io/docs/reference/python-rest-client/
Apache License 2.0
79 stars 52 forks source link

Cannot use get_entity_groups_by_type() method #34

Closed JacksonBowe closed 3 years ago

JacksonBowe commented 3 years ago

Version 1.3.2

Running method dashboard_groups = rest_client.get_entity_groups_by_type('DASHBOARD')

Resulting error

Traceback (most recent call last):
  File "C:\Users\JacksonBowe\Envs\myinsightplatform\lib\site-packages\tb_rest_client\api_client.py", line 338, in __deserialize
    found_class = getattr(tb_rest_client.models.models_ce, klass)
AttributeError: module 'tb_rest_client.models.models_ce' has no attribute 'EntityGroupInfo'

I'm noticing this error is fairly common, where the CE and PE clients are so intertwined that some methods are defaulting to CE without being possible. E.g. same issue happens with get_integrations_get() method.

JacksonBowe commented 3 years ago

Did some digging, below is the problem

File _apiclient.py line 333ish. Looking for the below line if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):

Printing these out shows the following

print(sorted(list(found_class.attribute_map.values())))
> ['additionalInfo', 'configuration', 'createdTime', 'edgeGroupAll', 'groupAll', 'id', 'name', 'ownerId', 'ownerIds', 'type']
print(sorted(list(data.keys())))
> ['additionalInfo', 'configuration', 'createdTime', 'groupAll', 'id', 'name', 'ownerId', 'ownerIds', 'type']

The only key different is edgeGroupAll. This is possibly because I'm trying to pull data from an installation of TB3.1.1PE.

To resolve this issue I tried uncommenting the following lines (see <<<<<<)

found_class = getattr(tb_rest_client.models.models_pe, klass)
if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
    klass = found_class
    print('here')
# if all(attr in list(found_class.attribute_map.values()) for attr in list(data.keys())): <<<<<<<<<<
#     klass = found_class <<<<<<<<<<
else:
    found_class = getattr(tb_rest_client.models.models_ce, klass)
    # if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
    klass = found_class

Uncommenting the above lines worked. Is there a reason that they were commented out? I don't want to be introducing errors back into my code that have already been handled, but for now I think I will keep it. When I upgrade my deployment to TB3.3PE I will revert back.

samson0v commented 3 years ago

Hi @JacksonBowe, the latest version of rest-client is for ThingsBoard v.3.3-v.3.X, for this reason, attributes can be different. Please update your ThingsBoard to the latest version.