text
stringlengths
0
828
register_url = self.base_url + ""api/0.1.0/register""
register_headers = {
""apikey"": str(self.owner_api_key),
""resourceID"": str(self.entity_id),
""serviceType"": ""publish,subscribe,historicData""
}
with self.no_ssl_verification():
r = requests.get(register_url, {}, headers=register_headers)
response = r.content.decode(""utf-8"")
if ""APIKey"" in str(r.content.decode(""utf-8"")):
response = json.loads(response[:-331] + ""}"") # Temporary fix to a middleware bug, should be removed in future
response[""Registration""] = ""success""
else:
response = json.loads(response)
response[""Registration""] = ""failure""
return response"
1258,"def no_ssl_verification(self):
"""""" Requests module fails due to lets encrypt ssl encryption. Will be fixed in the future release.""""""
try:
from functools import partialmethod
except ImportError:
# Python 2 fallback: https://gist.github.com/carymrobbins/8940382
from functools import partial
class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance, *(self.args or ()), **(self.keywords or {}))
old_request = requests.Session.request
requests.Session.request = partialmethod(old_request, verify=False)
warnings.filterwarnings('ignore', 'Unverified HTTPS request')
yield
warnings.resetwarnings()
requests.Session.request = old_request"
1259,"def publish(self, data):
"""""" This function allows an entity to publish data to the middleware.
Args:
data (string): contents to be published by this entity.
""""""
if self.entity_api_key == """":
return {'status': 'failure', 'response': 'No API key found in request'}
publish_url = self.base_url + ""api/0.1.0/publish""
publish_headers = {""apikey"": self.entity_api_key}
publish_data = {
""exchange"": ""amq.topic"",
""key"": str(self.entity_id),
""body"": str(data)
}
with self.no_ssl_verification():
r = requests.post(publish_url, json.dumps(publish_data), headers=publish_headers)
response = dict()
if ""No API key"" in str(r.content.decode(""utf-8"")):
response[""status""] = ""failure""
r = json.loads(r.content.decode(""utf-8""))['message']
elif 'publish message ok' in str(r.content.decode(""utf-8"")):
response[""status""] = ""success""
r = r.content.decode(""utf-8"")
else:
response[""status""] = ""failure""
r = r.content.decode(""utf-8"")
response[""response""] = str(r)
return response"
1260,"def db(self, entity, query_filters=""size=10""):
"""""" This function allows an entity to access the historic data.
Args:
entity (string): Name of the device to listen to
query_filters (string): Elastic search response format string
example, ""pretty=true&size=10""
""""""
if self.entity_api_key == """":
return {'status': 'failure', 'response': 'No API key found in request'}
historic_url = self.base_url + ""api/0.1.0/historicData?"" + query_filters
historic_headers = {
""apikey"": self.entity_api_key,
""Content-Type"": ""application/json""
}
historic_query_data = json.dumps({
""query"": {
""match"": {
""key"": entity
}
}
})
with self.no_ssl_verification():
r = requests.get(historic_url, data=historic_query_data, headers=historic_headers)
response = dict()
if ""No API key"" in str(r.content.decode(""utf-8"")):
response[""status""] = ""failure""
else:
r = r.content.decode(""utf-8"")
response = r
return response"