From 46c6d4de2e8bf56a260c6c5079ce53fdeb0e9e0c Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Tue, 19 Dec 2017 11:10:41 -0300 Subject: [PATCH 01/10] Adds list_entities method --- fiotclient/context.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/fiotclient/context.py b/fiotclient/context.py index d939d64..b85882a 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -95,6 +95,17 @@ def get_entities_by_type(self, entity_type): url = "http://{}:{}/v2/entities?type={}".format(self.cb_host, self.cb_port, entity_type) payload = '' + return self._send_request(url, payload, 'GET') + def list_entities(self): + """List all entities + + :return: A list with the id and type of all entities + """ + logging.info("Getting id of all entities") + + url = "http://{}:{}/v2/entities?attr=null".format(self.cb_host, self.cb_port, entity_type) + payload = '' + return self._send_request(url, payload, 'GET') def subscribe_attributes_change(self, device_id, attributes, notification_url): From 1c4a6fbb79d5f3bfc84e3a032d02974c6ff542a9 Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Tue, 19 Dec 2017 13:14:01 -0300 Subject: [PATCH 02/10] change method list_entities to get_entities. Return 1000 entities {id, type} --- fiotclient/context.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index b85882a..2ad4855 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -96,14 +96,15 @@ def get_entities_by_type(self, entity_type): payload = '' return self._send_request(url, payload, 'GET') - def list_entities(self): + + def get_entities(self): """List all entities - :return: A list with the id and type of all entities + :return: A list with the id and type of all entities (1.000 first entities) """ logging.info("Getting id of all entities") - - url = "http://{}:{}/v2/entities?attr=null".format(self.cb_host, self.cb_port, entity_type) + offset = 'offset=0&limit=1000' + url = "http://{}:{}/v2/entities?attrs=null&{}".format(self.cb_host, self.cb_port, offset) payload = '' return self._send_request(url, payload, 'GET') From 78338e47cc073a16f833c8232d088ee16af35e30 Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Tue, 19 Dec 2017 18:38:21 -0300 Subject: [PATCH 03/10] Create entity Delete entity ISSUE #6 --- fiotclient/context.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index 2ad4855..72e2b87 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -42,8 +42,19 @@ def create_entity(self, entity_id, entity_schema): :return: Information of the registered entity """ - # TODO Implement - pass + """Get entity information given its entity id + + :param entity_id: The id of the entity to be searched + :return: The information of the entity found with the given id + or None if no entity was found with the id + """ + logging.info("Creating entity by id '{}'".format(entity_id)) + + url = "http://{}:{}/v2/entities".format(self.cb_host, self.cb_port) + payload = entity_schema or '' + additional_headers = {"Content-Type": "application/json"} + + return self._send_request(url, payload, 'POST', additional_headers=additional_headers) def update_entity(self, entity_id, entity_schema): """Updates an entity with the given id for the new structure in the currently selected service @@ -56,14 +67,22 @@ def update_entity(self, entity_id, entity_schema): # TODO Implement pass - def remove_entity(self, entity_id): + def remove_entity(self, entity_id, type_id=False): """Removes an entity with the given id :param entity_id: The id to the entity to be removed :return: Information of the removed entity """ - # TODO Implement + logging.info("Deletin entity by id '{}'".format(entity_id)) + if type_id: + url = "http://{}:{}/v2/entities/{}?type={}".format(self.cb_host, self.cb_port, entity_id, type_id) + else: + url = "http://{}:{}/v2/entities/{}".format(self.cb_host, self.cb_port, entity_id) + + payload = '' + print(url) + return self._send_request(url, payload, 'DELETE') pass def get_entity_by_id(self, entity_id): From 3901202210c09fecc8ed0bcafeb9ed3e18e6878d Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Thu, 21 Dec 2017 14:17:07 -0300 Subject: [PATCH 04/10] simple ajdust: method remove_entity --- fiotclient/context.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index 72e2b87..6348e12 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -83,7 +83,6 @@ def remove_entity(self, entity_id, type_id=False): payload = '' print(url) return self._send_request(url, payload, 'DELETE') - pass def get_entity_by_id(self, entity_id): """Get entity information given its entity id From 20fb273c84b6f7a4bdfdf3fb6182143ac06760df Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Thu, 21 Dec 2017 14:44:05 -0300 Subject: [PATCH 05/10] Change method get_entity_by_id: find by type, or only entity_id. Return only id and type --- fiotclient/context.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index 6348e12..51f6fdc 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -84,7 +84,7 @@ def remove_entity(self, entity_id, type_id=False): print(url) return self._send_request(url, payload, 'DELETE') - def get_entity_by_id(self, entity_id): + def get_entity_by_id(self, entity_id, type_id=None): """Get entity information given its entity id :param entity_id: The id of the entity to be searched @@ -94,7 +94,10 @@ def get_entity_by_id(self, entity_id): logging.info("Getting entity by id '{}'".format(entity_id)) # TODO Remove hardcoded type from url - url = "http://{}:{}/v2/entities/{}/attrs?type=thing".format(self.cb_host, self.cb_port, entity_id) + if type_id: + url = "http://{}:{}/v2/entities/{}/?attrs=null&type={}".format(self.cb_host, self.cb_port, entity_id,type_id) + else: + url = "http://{}:{}/v2/entities/{}/?attrs=null".format(self.cb_host, self.cb_port, entity_id) payload = '' From 35ecb5c3b3830d5c875bdc1aa3e98a0b22425045 Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Thu, 21 Dec 2017 15:12:27 -0300 Subject: [PATCH 06/10] Insert comments and TODO's --- fiotclient/context.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index 51f6fdc..aa3af25 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -118,14 +118,27 @@ def get_entities_by_type(self, entity_type): return self._send_request(url, payload, 'GET') - def get_entities(self): + def get_entities(self, type_id=None, offset=0, offset_limit=None): """List all entities - + :param type_id: The type of the entities to be searched + :param offset: The first entity registry to be searched + :param offset_limit: The quantity of entity registry to be searched, from offset parameter. Default: 1000 :return: A list with the id and type of all entities (1.000 first entities) """ logging.info("Getting id of all entities") - offset = 'offset=0&limit=1000' - url = "http://{}:{}/v2/entities?attrs=null&{}".format(self.cb_host, self.cb_port, offset) + # TODO Implement an elegant way + # TODO Define a offset_limit. 1000 its ok? + if offset_limit: + pass + else: + offset_limit = 1000 + + offset = 'offset={}&limit={}'.format(offset, offset_limit) + # TODO creae a method create_url + if type_id: + url = "http://{}:{}/v2/entities?attrs=null&{}&type={}".format(self.cb_host, self.cb_port, offset,type_id) + else: + url = "http://{}:{}/v2/entities?attrs=null&{}".format(self.cb_host, self.cb_port, offset) payload = '' return self._send_request(url, payload, 'GET') From 22de6430776d3641a30029c7451cb0d4cb48ed92 Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Thu, 21 Dec 2017 15:27:00 -0300 Subject: [PATCH 07/10] change and create get_entities_types and get_entities_by_type methods and define entity_type (some times type_id) --- fiotclient/context.py | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index aa3af25..3c5ecef 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -67,7 +67,7 @@ def update_entity(self, entity_id, entity_schema): # TODO Implement pass - def remove_entity(self, entity_id, type_id=False): + def remove_entity(self, entity_id, entity_type=False): """Removes an entity with the given id :param entity_id: The id to the entity to be removed @@ -75,8 +75,8 @@ def remove_entity(self, entity_id, type_id=False): :return: Information of the removed entity """ logging.info("Deletin entity by id '{}'".format(entity_id)) - if type_id: - url = "http://{}:{}/v2/entities/{}?type={}".format(self.cb_host, self.cb_port, entity_id, type_id) + if entity_type: + url = "http://{}:{}/v2/entities/{}?type={}".format(self.cb_host, self.cb_port, entity_id, entity_type) else: url = "http://{}:{}/v2/entities/{}".format(self.cb_host, self.cb_port, entity_id) @@ -84,7 +84,7 @@ def remove_entity(self, entity_id, type_id=False): print(url) return self._send_request(url, payload, 'DELETE') - def get_entity_by_id(self, entity_id, type_id=None): + def get_entity_by_id(self, entity_id, entity_type=None): """Get entity information given its entity id :param entity_id: The id of the entity to be searched @@ -94,8 +94,8 @@ def get_entity_by_id(self, entity_id, type_id=None): logging.info("Getting entity by id '{}'".format(entity_id)) # TODO Remove hardcoded type from url - if type_id: - url = "http://{}:{}/v2/entities/{}/?attrs=null&type={}".format(self.cb_host, self.cb_port, entity_id,type_id) + if entity_type: + url = "http://{}:{}/v2/entities/{}/?attrs=null&type={}".format(self.cb_host, self.cb_port, entity_id, entity_type) else: url = "http://{}:{}/v2/entities/{}/?attrs=null".format(self.cb_host, self.cb_port, entity_id) @@ -105,22 +105,35 @@ def get_entity_by_id(self, entity_id, type_id=None): # TODO Implement get all entities of all types of selected service - def get_entities_by_type(self, entity_type): + def get_entities_types(self, entity_type=None): """Get entities created with a given entity type :param entity_type: The type of the entities to be searched :return: A list with the information of the entities found with the given type """ - logging.info("Getting entities by type '{}'".format(type)) + logging.info("Getting entities types '{}'".format(entity_type)) - url = "http://{}:{}/v2/entities?type={}".format(self.cb_host, self.cb_port, entity_type) + if entity_type: + url = "http://{}:{}/v2/types/{}".format(self.cb_host, self.cb_port, entity_type) + else: + url = "http://{}:{}/v2/types".format(self.cb_host, self.cb_port) payload = '' return self._send_request(url, payload, 'GET') - def get_entities(self, type_id=None, offset=0, offset_limit=None): + def get_entities_by_type(self, entity_type, offset=0, offset_limit=None): + """Get entities created with a given entity type + + :param entity_type: The type of the entities to be searched + :return: A list with the information of the entities found with the given type + """ + logging.info("Getting entities by type '{}'".format(entity_type)) + return self.get_entities(entity_type=entity_type, offset=0, offset_limit=None) + + + def get_entities(self, entity_type=None, offset=0, offset_limit=None): """List all entities - :param type_id: The type of the entities to be searched + :param entity_type: The type of the entities to be searched :param offset: The first entity registry to be searched :param offset_limit: The quantity of entity registry to be searched, from offset parameter. Default: 1000 :return: A list with the id and type of all entities (1.000 first entities) @@ -135,8 +148,8 @@ def get_entities(self, type_id=None, offset=0, offset_limit=None): offset = 'offset={}&limit={}'.format(offset, offset_limit) # TODO creae a method create_url - if type_id: - url = "http://{}:{}/v2/entities?attrs=null&{}&type={}".format(self.cb_host, self.cb_port, offset,type_id) + if entity_type: + url = "http://{}:{}/v2/entities?attrs=null&{}&type={}".format(self.cb_host, self.cb_port, offset, entity_type) else: url = "http://{}:{}/v2/entities?attrs=null&{}".format(self.cb_host, self.cb_port, offset) payload = '' From de417c8ea295d45fada4b6738125704b2beff814 Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Thu, 21 Dec 2017 15:48:32 -0300 Subject: [PATCH 08/10] create method get_entitiesID_by_type --- fiotclient/context.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fiotclient/context.py b/fiotclient/context.py index 3c5ecef..8f0838e 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -83,6 +83,13 @@ def remove_entity(self, entity_id, entity_type=False): payload = '' print(url) return self._send_request(url, payload, 'DELETE') + def get_entitiesID_by_type(self, entity_type): + resp = self.get_entities(entity_type) + entities = resp['response'] + entityID_list = [] + for e in entities: + entityID_list.append(e['id']) + return entityID_list def get_entity_by_id(self, entity_id, entity_type=None): """Get entity information given its entity id From 3bf0fda7cec37fbd9b7c6b9c7a6a8e56c22f69ff Mon Sep 17 00:00:00 2001 From: Julio Zinga Date: Thu, 21 Dec 2017 16:29:53 -0300 Subject: [PATCH 09/10] create method remove_entities and get_entitiesID. methods comment #7 --- fiotclient/context.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index 8f0838e..397527e 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -71,6 +71,7 @@ def remove_entity(self, entity_id, entity_type=False): """Removes an entity with the given id :param entity_id: The id to the entity to be removed + :param entity_type: The entity type of the entity to be removed :return: Information of the removed entity """ @@ -81,9 +82,33 @@ def remove_entity(self, entity_id, entity_type=False): url = "http://{}:{}/v2/entities/{}".format(self.cb_host, self.cb_port, entity_id) payload = '' - print(url) + return self._send_request(url, payload, 'DELETE') - def get_entitiesID_by_type(self, entity_type): + + def remove_entities(self, entity_type): + """Removes all entities with the given entity_type (Remove the first 1000 entities) + + :param entity_type: The entity type of the entities to be removed + + :return: Information of the removed entity + """ + + if isinstance(entity_type, str): + entities = self.get_entitiesID(entity_type=entity_type) + for e in entities: + self.remove_entity(e) + else: + print("ERROR: entity_type must be string") + #TODO Exception? + + + def get_entitiesID(self, entity_type): + """Get a list of entities Ids of the provided entity type + + :param entity_type: The entity type of the entities to be searched + :return: A list with ids of the entities found with the given entity type (max 1.000) + or None if no entity was found with the id + """ resp = self.get_entities(entity_type) entities = resp['response'] entityID_list = [] @@ -95,7 +120,8 @@ def get_entity_by_id(self, entity_id, entity_type=None): """Get entity information given its entity id :param entity_id: The id of the entity to be searched - :return: The information of the entity found with the given id + :param entity_type: The entity type of the entity to be searched + :return: The information of the entity found with the given id (and entity type) or None if no entity was found with the id """ logging.info("Getting entity by id '{}'".format(entity_id)) From 6882f5bfc0c7b52acc22b867e28abd1587d8330f Mon Sep 17 00:00:00 2001 From: juliozinga Date: Tue, 2 Jan 2018 14:48:27 -0300 Subject: [PATCH 10/10] fix filter attrs --- fiotclient/context.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fiotclient/context.py b/fiotclient/context.py index 397527e..acd89df 100644 --- a/fiotclient/context.py +++ b/fiotclient/context.py @@ -128,9 +128,9 @@ def get_entity_by_id(self, entity_id, entity_type=None): # TODO Remove hardcoded type from url if entity_type: - url = "http://{}:{}/v2/entities/{}/?attrs=null&type={}".format(self.cb_host, self.cb_port, entity_id, entity_type) + url = "http://{}:{}/v2/entities{}?attrs=null&type={}".format(self.cb_host, self.cb_port, entity_id, entity_type) else: - url = "http://{}:{}/v2/entities/{}/?attrs=null".format(self.cb_host, self.cb_port, entity_id) + url = "http://{}:{}/v2/entities{}?attrs=null".format(self.cb_host, self.cb_port, entity_id) payload = ''