Skip to content

Commit

Permalink
Updated REST Client
Browse files Browse the repository at this point in the history
  • Loading branch information
samson0v committed May 15, 2023
1 parent 8567559 commit 80b249b
Show file tree
Hide file tree
Showing 881 changed files with 36,268 additions and 9,650 deletions.
52 changes: 32 additions & 20 deletions examples/example_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from tb_rest_client.rest_client_ce import *
from tb_rest_client.rest import ApiException


logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(module)s - %(lineno)d - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
Expand All @@ -31,29 +30,42 @@
password = "tenant"


# Creating the REST client object with context manager to get auto token refresh
with RestClientCE(base_url=url) as rest_client:
try:
# Auth with credentials
rest_client.login(username=username, password=password)
def main():
# Creating the REST client object with context manager to get auto token refresh
with RestClientCE(base_url=url) as rest_client:
try:
# Auth with credentials
rest_client.login(username=username, password=password)

# Creating an Asset
default_asset_profile_id = rest_client.get_default_asset_profile_info().id
asset = Asset(name="Building 1", label="Building 1",
asset_profile_id=default_asset_profile_id)
asset = rest_client.save_asset(asset)

# Creating an Asset
asset = Asset(name="Building 1", type="building")
asset = rest_client.save_asset(asset)
logging.info("Asset was created:\n%r\n", asset)

logging.info("Asset was created:\n%r\n", asset)
# Creating a Device
# Also, you can use default Device Profile:
# default_device_profile_id = rest_client.get_default_device_profile_info().id
device_profile = DeviceProfile(name="Thermometer",
profile_data=DeviceProfileData(configuration={"type": "DEFAULT"},
transport_configuration={"type": "DEFAULT"}))
device_profile = rest_client.save_device_profile(device_profile)
device = Device(name="Thermometer 1", label="Thermometer 1",
device_profile_id=device_profile.id)
device = rest_client.save_device(device)

# creating a Device
device = Device(name="Thermometer 1", type="thermometer")
device = rest_client.save_device(device)
logging.info(" Device was created:\n%r\n", device)

logging.info(" Device was created:\n%r\n", device)
# Creating relations from device to asset
relation = EntityRelation(_from=asset.id, to=device.id, type="Contains")
rest_client.save_relation(relation)

# Creating relations from device to asset
relation = EntityRelation(_from=asset.id, to=device.id, type="Contains")
relation = rest_client.save_relation(relation)
logging.info(" Relation was created:\n%r\n", relation)
except ApiException as e:
logging.exception(e)

logging.info(" Relation was created:\n%r\n", relation)
except ApiException as e:
logging.exception(e)

if __name__ == '__main__':
main()
27 changes: 17 additions & 10 deletions examples/example_application_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,51 @@ def main():
current_user = rest_client.get_user()

# Creating Dashboard Group on the Tenant Level
shared_dashboards_group = EntityGroup(name="Shared Dashboards", type="DASHBOARD")
shared_dashboards_group = EntityGroup(name="Shared Dashboards3", type="DASHBOARD")
shared_dashboards_group = rest_client.save_entity_group(shared_dashboards_group)
logging.info('Dashboard group created:\n%r\n', shared_dashboards_group)

# Loading Dashboard from file
dashboard_json = None
with open("watermeters.json", "r") as dashboard_file:
dashboard_json = load(dashboard_file)
dashboard = Dashboard(title=dashboard_json["title"], configuration=dashboard_json["configuration"])
dashboard = rest_client.save_dashboard(dashboard)
logging.info('Dashboard created:\n%r\n', dashboard)

# Adding Dashboard to the Shared Dashboards Group
rest_client.add_entities_to_entity_group([dashboard.id.id], shared_dashboards_group.id)
dashboard = list(filter(lambda x: x.name == dashboard.name,
rest_client.get_user_dashboards(10, 0).data))[0]
rest_client.add_entities_to_entity_group(shared_dashboards_group.id, [dashboard.id.id])

# Creating Customer 1
customer1 = Customer(title="Customer 1")
customer1 = rest_client.save_customer(customer1)
customer1 = Customer(title="Customer 11")
customer1 = rest_client.save_customer(body=customer1)

# Creating Device
device = Device(name="WaterMeter1", type="waterMeter")
default_device_profile_id = rest_client.get_default_device_profile_info().id
device = Device(name="WaterMeter 1", label="WaterMeter 1", device_profile_id=default_device_profile_id)
device = rest_client.save_device(device)
logging.info('Device created:\n%r\n', device)

# Fetching automatically created "Customer Administrators" Group.
customer1_administrators = rest_client.get_entity_group_by_owner_and_name_and_type(customer1.id.entity_type, customer1.id.id, "USER", "Customer Administrators")
customer1_administrators = rest_client.get_entity_group_by_owner_and_name_and_type(customer1.id, "USER", "Customer Administrators")

# Creating Read-Only Role
read_only_role = Role(name="Read-Only", permissions=['READ', 'READ_ATTRIBUTES', 'READ_TELEMETRY', 'READ_CREDENTIALS'], type="GROUP")
read_only_role = rest_client.save_role(read_only_role)
logging.info('Role created:\n%r\n', read_only_role)

# Assigning Shared Dashboards to the Customer 1 Administrators
tenant_id = current_user.tenant_id
group_permission = GroupPermission(role_id=read_only_role.id,
name="Read Only Permission",
is_public=False,
user_group_id=customer1_administrators.id,
tenant_id=tenant_id,
entity_group_id=shared_dashboards_group.id,
entity_group_type=shared_dashboards_group.type)
group_permission = rest_client.save_group_permission(group_permission)
logging.info('Group permission created:\n%r\n', group_permission)

# Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group.
user_email = "user@thingsboard.org"
Expand All @@ -95,10 +102,10 @@ def main():
email=user_email,
additional_info=additional_info)
user = rest_client.save_user(user, send_activation_mail=False)
rest_client.activate_user(user.id, user_password)

rest_client.add_entities_to_entity_group([user.id.id], customer1_administrators.id)
rest_client.activate_user(body=ActivateUserRequest(user.id, user_password), send_activation_mail=False)

rest_client.add_entities_to_entity_group(customer1_administrators.id, [user.id.id])
logging.info('User created:\n%r\n', user)
except ApiException as e:
logging.exception(e)

Expand Down
9 changes: 7 additions & 2 deletions tb_rest_client/api/api_ce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .entity_relation_controller_api import EntityRelationControllerApi
from .entity_view_controller_api import EntityViewControllerApi
from .admin_controller_api import AdminControllerApi
from .sign_up_controller_api import SignUpControllerApi
from .tb_resource_controller_api import TbResourceControllerApi
from .o_auth_2_controller_api import OAuth2ControllerApi
from .tenant_profile_controller_api import TenantProfileControllerApi
Expand All @@ -32,6 +31,12 @@
from .ota_package_controller_api import OtaPackageControllerApi
from .entities_version_control_controller_api import EntitiesVersionControlControllerApi
from .device_api_controller_api import DeviceApiControllerApi
from .two_fa_config_controller_api import TwoFaConfigControllerApi
from .two_factor_auth_controller_api import TwoFactorAuthControllerApi
from .alarm_comment_controller_api import AlarmCommentControllerApi
from .asset_profile_controller_api import AssetProfileControllerApi
from .notification_controller_api import NotificationControllerApi
from .notification_rule_controller_api import NotificationRuleControllerApi
from .notification_target_controller_api import NotificationTargetControllerApi
from .notification_template_controller_api import NotificationTemplateControllerApi
from .usage_info_controller_api import UsageInfoControllerApi
from .two_factor_auth_config_controller_api import TwoFactorAuthConfigControllerApi
Loading

0 comments on commit 80b249b

Please sign in to comment.