Skip to content

Commit 8e80154

Browse files
committed
Added ability to use context manager with REST client( Token auto-update ).
1 parent 1d8049c commit 8e80154

File tree

5 files changed

+96
-85
lines changed

5 files changed

+96
-85
lines changed

examples/example_application.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,24 @@
1111
username = "tenant@thingsboard.org"
1212
password = "tenant"
1313

14-
rest_client = RestClientCE(base_url=url)
15-
rest_client.login(username=username, password=password)
14+
with RestClientCE(base_url=url) as rest_client:
15+
try:
16+
rest_client.login(username=username, password=password)
1617

17-
try:
18+
asset = Asset(name="Building 1", type="building")
19+
asset = rest_client.save_asset(asset)
1820

19-
asset = Asset(name="Building 1", type="building")
20-
asset = rest_client.save_asset(asset)
21+
logging.info("Asset was created:\n%r\n", asset)
2122

22-
logging.info("Asset was created:\n%r\n", asset)
23+
device = Device(name="Thermometer 1", type="thermometer")
24+
device = rest_client.save_device(device)
2325

24-
device = Device(name="Thermometer 1", type="thermometer")
25-
device = rest_client.save_device(device)
26+
logging.info(" Device was created:\n%r\n", device)
2627

27-
logging.info(" Device was created:\n%r\n", device)
28+
relation = EntityRelation(_from=asset.id, to=device.id, type="Contains")
29+
relation = rest_client.save_relation(relation)
2830

29-
relation = EntityRelation(_from=asset.id, to=device.id, type="Contains")
30-
relation = rest_client.save_relation(relation)
31+
logging.info(" Relation was created:\n%r\n", relation)
32+
except ApiException as e:
33+
logging.exception(e)
3134

32-
logging.info(" Relation was created:\n%r\n", relation)
33-
except ApiException as e:
34-
logging.exception(e)
35-
36-
rest_client.stop()

examples/example_application_2.py

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from json import load
3-
from tb_rest_client import *
3+
from tb_rest_client.rest_client_pe import *
44
from tb_rest_client.rest import ApiException
55

66

@@ -13,67 +13,66 @@
1313
username = "tenant@thingsboard.org"
1414
password = "tenant"
1515

16-
rest_client = RestClient(base_url=url)
17-
rest_client.login(username=username, password=password)
18-
19-
current_user = rest_client.get_user()
20-
21-
try:
22-
# Creating Dashboard Group on the Tenant Level
23-
shared_dashboards_group = EntityGroup(name="Shared Dashboards", type="DASHBOARD")
24-
shared_dashboards_group = rest_client.save_entity_group(shared_dashboards_group)
25-
26-
# Loading Dashboard from file
27-
dashboard_json = None
28-
with open("watermeters.json", "r") as dashboard_file:
29-
dashboard_json = load(dashboard_file)
30-
dashboard = Dashboard(title=dashboard_json["title"], configuration=dashboard_json["configuration"])
31-
dashboard = rest_client.save_dashboard(dashboard)
32-
33-
# Adding Dashboard to the Shared Dashboards Group
34-
rest_client.add_entities_to_entity_group(shared_dashboards_group.id, [dashboard.id.id])
35-
36-
# Creating Customer 1
37-
customer1 = Customer(title="Customer 1")
38-
customer1 = rest_client.save_customer(customer1)
39-
40-
# Creating Device
41-
device = Device(name="WaterMeter1", type="waterMeter")
42-
device = rest_client.save_device(device)
43-
44-
# Fetching automatically created "Customer Administrators" Group.
45-
customer1_administrators = rest_client.get_entity_group_info_by_owner_and_name_and_type(customer1.id, "USER", "Customer Administrators")
46-
47-
# Creating Read-Only Role
48-
read_only_role = Role(name="Read-Only", permissions=['READ', 'READ_ATTRIBUTES', 'READ_TELEMETRY', 'READ_CREDENTIALS'], type="GROUP")
49-
read_only_role = rest_client.save_role(read_only_role)
50-
51-
# Assigning Shared Dashboards to the Customer 1 Administrators
52-
tenant_id = current_user.tenant_id
53-
group_permission = GroupPermission(role_id=read_only_role.id,
54-
name="Read Only Permission",
55-
is_public=False,
56-
user_group_id=customer1_administrators.id,
57-
tenant_id=tenant_id,
58-
entity_group_id=shared_dashboards_group.id,
59-
entity_group_type=shared_dashboards_group.type)
60-
group_permission = rest_client.save_group_permission(group_permission)
61-
62-
# Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group.
63-
user_email = "user@thingsboard.org"
64-
user_password = "secret"
65-
66-
additional_info = {
67-
"defaultDashboardId": dashboard.id.id,
68-
"defaultDashboardFullscreen": False
69-
}
70-
user = User(authority="CUSTOMER_USER",
71-
customer_id=customer1.id,
72-
email=user_email,
73-
additional_info=additional_info)
74-
user = rest_client.save_user(user, send_activation_mail=False)
75-
rest_client.activate_user(user.id, user_password)
76-
rest_client.add_entities_to_entity_group(customer1_administrators.id, [user.id.id])
77-
78-
except ApiException as e:
79-
logging.exception(e)
16+
17+
with RestClientPE(base_url=url) as rest_client:
18+
try:
19+
rest_client.login(username=username, password=password)
20+
current_user = rest_client.get_user()
21+
# Creating Dashboard Group on the Tenant Level
22+
shared_dashboards_group = EntityGroup(name="Shared Dashboards", type="DASHBOARD")
23+
shared_dashboards_group = rest_client.save_entity_group(shared_dashboards_group)
24+
25+
# Loading Dashboard from file
26+
dashboard_json = None
27+
with open("watermeters.json", "r") as dashboard_file:
28+
dashboard_json = load(dashboard_file)
29+
dashboard = Dashboard(title=dashboard_json["title"], configuration=dashboard_json["configuration"])
30+
dashboard = rest_client.save_dashboard(dashboard)
31+
32+
# Adding Dashboard to the Shared Dashboards Group
33+
rest_client.add_entities_to_entity_group(shared_dashboards_group.id, [dashboard.id.id])
34+
35+
# Creating Customer 1
36+
customer1 = Customer(title="Customer 1")
37+
customer1 = rest_client.save_customer(customer1)
38+
39+
# Creating Device
40+
device = Device(name="WaterMeter1", type="waterMeter")
41+
device = rest_client.save_device(device)
42+
43+
# Fetching automatically created "Customer Administrators" Group.
44+
customer1_administrators = rest_client.get_entity_group_info_by_owner_and_name_and_type(customer1.id, "USER", "Customer Administrators")
45+
46+
# Creating Read-Only Role
47+
read_only_role = Role(name="Read-Only", permissions=['READ', 'READ_ATTRIBUTES', 'READ_TELEMETRY', 'READ_CREDENTIALS'], type="GROUP")
48+
read_only_role = rest_client.save_role(read_only_role)
49+
50+
# Assigning Shared Dashboards to the Customer 1 Administrators
51+
tenant_id = current_user.tenant_id
52+
group_permission = GroupPermission(role_id=read_only_role.id,
53+
name="Read Only Permission",
54+
is_public=False,
55+
user_group_id=customer1_administrators.id,
56+
tenant_id=tenant_id,
57+
entity_group_id=shared_dashboards_group.id,
58+
entity_group_type=shared_dashboards_group.type)
59+
group_permission = rest_client.save_group_permission(group_permission)
60+
61+
# Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group.
62+
user_email = "user@thingsboard.org"
63+
user_password = "secret"
64+
65+
additional_info = {
66+
"defaultDashboardId": dashboard.id.id,
67+
"defaultDashboardFullscreen": False
68+
}
69+
user = User(authority="CUSTOMER_USER",
70+
customer_id=customer1.id,
71+
email=user_email,
72+
additional_info=additional_info)
73+
user = rest_client.save_user(user, send_activation_mail=False)
74+
rest_client.activate_user(user.id, user_password)
75+
rest_client.add_entities_to_entity_group(customer1_administrators.id, [user.id.id])
76+
77+
except ApiException as e:
78+
logging.exception(e)

tb_rest_client/api_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,18 @@ def __deserialize(self, data, klass):
319319
else:
320320
try:
321321
found_class = getattr(tb_rest_client.models.models_pe, klass)
322-
if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
322+
# if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
323+
if all(attr in list(found_class.attribute_map.values()) for attr in list(data.keys())):
323324
klass = found_class
324325
else:
325326
found_class = getattr(tb_rest_client.models.models_ce, klass)
326-
if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
327+
# if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
328+
if all(attr in list(found_class.attribute_map.values()) for attr in list(data.keys())):
327329
klass = found_class
328330
except AttributeError:
329331
found_class = getattr(tb_rest_client.models.models_ce, klass)
330-
if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
332+
if all(attr in list(found_class.attribute_map.values()) for attr in list(data.keys())):
333+
# if sorted(list(found_class.attribute_map.values())) == sorted(list(data.keys())):
331334
klass = found_class
332335
return self.__deserialize_data(data, klass)
333336

tb_rest_client/rest_client_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def __init__(self, base_url):
2828
self.stopped = True
2929
self.configuration = Configuration()
3030
self.configuration.host = self.base_url
31-
self.start()
3231

3332
def run(self):
3433
self.stopped = False
@@ -50,6 +49,13 @@ def run(self):
5049
def stop(self):
5150
self.stopped = True
5251

52+
def __enter__(self):
53+
self.start()
54+
return self
55+
56+
def __exit__(self, exc_type, exc_val, exc_tb):
57+
self.stop()
58+
5359
def login(self, username, password):
5460
"""Authorization on the host and saving the toke information"""
5561
if self.username is None and self.password is None:

tb_rest_client/rest_client_pe.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
class RestClientPE(RestClientBase):
1111
def __init__(self, base_url):
1212
super().__init__(base_url)
13+
14+
def login(self, username, password):
15+
super(RestClientPE, self).login(username=username, password=password)
16+
self.__load_controllers()
17+
1318
"""Admin controller endpoints"""
1419

1520
def get_admin_settings(self, key, system_by_default=False):

0 commit comments

Comments
 (0)