Skip to content

Commit cfa9767

Browse files
committed
Improvements
1 parent f446816 commit cfa9767

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# ThingsBoard REST API client
1+
# ThingsBoard Python REST API client
22

33

4-
The ThingsBoard REST API Client helps you interact with ThingsBoard REST API from your Python script.
5-
With Rest Client you can programmatically create assets, devices, customers, users and other entities and their relations in ThingsBoard.
4+
The ThingsBoard REST API Client helps you interact with [ThingsBoard REST API](https://thingsboard.io/docs/reference/rest-api/) from your Python script.
5+
With [Python Rest Client](https://thingsboard.io/docs/reference/python-rest-client/) you can programmatically create assets, devices, customers, users and other entities and their relations in ThingsBoard.
66

77
The recommended method for installing the Rest Client is a pip.
88

99
*The Python version of the REST API client is under developing. If you have discovered any bug, please write us using email or by opening the issue.*
1010

11+
1112
##### Installation
1213

1314
In order to install the ThingsBoard REST client, you should use the following command:
@@ -16,23 +17,25 @@ In order to install the ThingsBoard REST client, you should use the following co
1617
pip3 install tb-rest-client
1718
```
1819

20+
1921
##### Examples
2022

21-
You can find the examples of the usage in the "examples" folder or on the [our site](https://thingsboard.io/docs/reference/python-rest-client/).
23+
You can find the examples of the usage in the "examples" folder or on the [our website](https://thingsboard.io/docs/reference/python-rest-client/).
2224

2325

2426
**Notate:** There are 2 REST clients for ThingsBoard, they are depend on version of the ThingsBoard, you use.
2527

26-
- If you use the ThingsBoard Community Edition (ThingsBoard CE) - please use the following command to import the REST client into your script:
27-
`from tb_rest_client.rest_client_ce import *`
28-
The REST client class has name "RestClientCE".
28+
- If you use the ThingsBoard Community Edition (ThingsBoard CE) - please use the following command to import the REST client into your script:
29+
`from tb_rest_client.rest_client_ce import *`
30+
The REST client class has name "RestClientCE".
2931

30-
- If you use the ThingsBoard Professional Edition (ThingsBoard PE) - please use the following command to import the REST client into your script:
31-
`from tb_rest_client.rest_client_pe import *`
32-
The REST client class has name "RestClientPE".
32+
- If you use the ThingsBoard Professional Edition (ThingsBoard PE) - please use the following command to import the REST client into your script:
33+
`from tb_rest_client.rest_client_pe import *`
34+
The REST client class has name "RestClientPE".
3335

3436
If you use the wrong version of the REST client, it could work unexpectedly.
3537

38+
3639
## Support
3740

3841
- [Community chat](https://gitter.im/thingsboard/chat)
@@ -41,6 +44,7 @@ If you use the wrong version of the REST client, it could work unexpectedly.
4144

4245
**Don't forget to star the repository to show your ❤️ and support.**
4346

47+
4448
## Licenses
4549

4650
This project is released under [Apache 2.0 License](./LICENSE).

examples/example_application.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515

1616
import logging
17+
# Importing models and REST client class from Community Edition version
1718
from tb_rest_client.rest_client_ce import *
1819
from tb_rest_client.rest import ApiException
1920

@@ -22,24 +23,33 @@
2223
format='%(asctime)s - %(levelname)s - %(module)s - %(lineno)d - %(message)s',
2324
datefmt='%Y-%m-%d %H:%M:%S')
2425

26+
# ThingsBoard REST API URL
2527
url = "http://localhost:8080"
28+
29+
# Default Tenant Administrator credentials
2630
username = "tenant@thingsboard.org"
2731
password = "tenant"
2832

33+
34+
# Creating the REST client object with context manager to get auto token refresh
2935
with RestClientCE(base_url=url) as rest_client:
3036
try:
37+
# Auth with credentials
3138
rest_client.login(username=username, password=password)
3239

40+
# Creating an Asset
3341
asset = Asset(name="Building 1", type="building")
3442
asset = rest_client.save_asset(asset)
3543

3644
logging.info("Asset was created:\n%r\n", asset)
3745

46+
# creating a Device
3847
device = Device(name="Thermometer 1", type="thermometer")
3948
device = rest_client.save_device(device)
4049

4150
logging.info(" Device was created:\n%r\n", device)
4251

52+
# Creating relations from device to asset
4353
relation = EntityRelation(_from=asset.id, to=device.id, type="Contains")
4454
relation = rest_client.save_relation(relation)
4555

examples/example_application_2.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import logging
1717
from json import load
18+
# Importing models and REST client class from Professional Edition version
1819
from tb_rest_client.rest_client_pe import *
1920
from tb_rest_client.rest import ApiException
2021

@@ -24,15 +25,23 @@
2425
datefmt='%Y-%m-%d %H:%M:%S')
2526

2627

28+
# ThingsBoard REST API URL
2729
url = "http://localhost:8080"
30+
31+
# Default Tenant Administrator credentials
2832
username = "tenant@thingsboard.org"
2933
password = "tenant"
3034

3135

36+
# Creating the REST client object with context manager to get auto token refresh
3237
with RestClientPE(base_url=url) as rest_client:
3338
try:
39+
# Auth with credentials
3440
rest_client.login(username=username, password=password)
41+
42+
# Getting current user
3543
current_user = rest_client.get_user()
44+
3645
# Creating Dashboard Group on the Tenant Level
3746
shared_dashboards_group = EntityGroup(name="Shared Dashboards", type="DASHBOARD")
3847
shared_dashboards_group = rest_client.save_entity_group(shared_dashboards_group)
@@ -76,7 +85,6 @@
7685
# Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group.
7786
user_email = "user@thingsboard.org"
7887
user_password = "secret"
79-
8088
additional_info = {
8189
"defaultDashboardId": dashboard.id.id,
8290
"defaultDashboardFullscreen": False
@@ -87,6 +95,7 @@
8795
additional_info=additional_info)
8896
user = rest_client.save_user(user, send_activation_mail=False)
8997
rest_client.activate_user(user.id, user_password)
98+
9099
rest_client.add_entities_to_entity_group(customer1_administrators.id, [user.id.id])
91100

92101
except ApiException as e:

0 commit comments

Comments
 (0)