cbQueryBuilder.py
Go to the documentation of this file.
00001 # MIT License
00002 #
00003 # Copyright (c) <2015> <Ikergune, Etxetar>
00004 #
00005 # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
00006 # (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
00007 # publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
00008 # subject to the following conditions:
00009 #
00010 # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 #
00012 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00013 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
00014 # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00015 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016 
00017 import json
00018 import urllib2
00019 
00020 from include.logger import Log
00021 from include.constants import DATA_CONTEXTBROKER
00022 from include.pubsub.iPubSub import IqueryBuilder
00023 
00024 
00025 class CbQueryBuilder(IqueryBuilder):
00026     ## \brief Query data to context broker
00027     def findById(self, entity_id, data_type="ROBOT", isPattern=False):
00028         ## \brief Get entity data from context broker
00029         # \param entity name (can be regular expression)
00030         # \param entity type
00031         # \param if the entity name is a pattern or not (false by default)
00032         url = "http://{}:{}/NGSI10/queryContext".format(DATA_CONTEXTBROKER["ADDRESS"], DATA_CONTEXTBROKER["PORT"])
00033         data = {
00034             "entities": [
00035                 {
00036                     "type": data_type,
00037                     "isPattern": "true" if isPattern else "false",
00038                     "id": entity_id
00039                 }
00040             ]
00041         }
00042         return self._sendRequest(url, json.dumps(data))
00043 
00044     def _sendRequest(self, url, data, method=None):
00045         ## \brief Send request to context broker
00046         # \param url to request to
00047         # \param data to send
00048         # \param HTTP method (GET by default)
00049         try:
00050             request = urllib2.Request(url, data, {'Content-Type': 'application/json', 'Accept': 'application/json'})
00051             if method is not None:
00052                 request.get_method = lambda: method
00053             response = urllib2.urlopen(request)
00054             data = response.read()
00055             response_body = json.loads(data)
00056             response.close()
00057             return response_body
00058         except Exception as ex:
00059             Log("ERROR", ex.reason)
00060             return None


firos
Author(s): IƱigo Gonzalez, igonzalez@ikergune.com
autogenerated on Thu Jun 6 2019 17:51:04