-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_broker_api.py
202 lines (159 loc) · 7.94 KB
/
context_broker_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Software Name: context_broker_api.pu
# SPDX-FileCopyrightText: Copyright (c) 2023 Universidad de Cantabria
# SPDX-License-Identifier: LGPL-3.0
#
# This software is distributed under the LGPL-3.0 license;
# see the LICENSE file for more details.
#
# Author: Laura MARTIN <[email protected]> et al.
import requests, json
from dateutil import parser
from dateutil.relativedelta import relativedelta
import configuration_variables
types = configuration_variables.types.split(",")
# upsert_entity: upsert entity into the Context Broker
# Params:
# - body: complete entity body
# Return:
# - status code -- 201 Created / 204 No Content
def upsert_entity(body):
url = configuration_variables.broker_url + "entityOperations/upsert?options=update"
payload = json.dumps([body])
headers = {'Content-Type': 'application/ld+json'}
response = requests.request("POST", url, headers=headers, data=payload)
return response.status_code
# check_if_entity_already_exists: check if the entity requested already exists in the Context broker
# Params:
# - entity_id: id of the entity requested
# - entity_type: type of the entity requested
# Return:
# - True/False: Entity found/not found
# - error: boolean specifying if there have been any errors throughout the function (associated with requests to the Context Broker or external instances)
def check_if_entity_already_exists(entity_id, entity_type):
url = configuration_variables.broker_url + "entities/" + entity_id
context_link = (
configuration_variables.base_context+entity_type.lower()+'-context.jsonld'
if entity_type in types
else configuration_variables.base_context + "default-context.jsonld"
)
headers = {
'Accept': 'application/ld+json',
'Link': '<'+context_link+'>;rel="http://www.w3.org/ns/json-ld#context"'
}
response = requests.request("GET", url, headers=headers, data={})
if response.status_code == 200: return True, False
elif response.status_code == 404: return False, False
else: return None, True
# get_entities_by_type_geoQuery: get entities stored in the Context Broker filtering by type and applying a geoQuery
# Params:
# - entity_type: type requested
# - coordinates: coordinates to make the geoQuery filter
# Return:
# - array of entities
# - error: boolean specifying if there have been any errors throughout the function (associated with requests to the Context Broker or external instances)
def get_entities_by_type_geoQuery(entity_type, coordinates):
url = configuration_variables.broker_url + "entities/?type="+entity_type+"&georel=near%3BmaxDistance=="+configuration_variables.distance_range+"&coordinates="+coordinates+"&geometry=Point"
context_link = (
configuration_variables.base_context+entity_type.lower()+'-context.jsonld'
if entity_type in types
else configuration_variables.base_context + "default-context.jsonld"
)
headers = {
'Accept': 'application/ld+json',
'Link': '<'+context_link+'>;rel="http://www.w3.org/ns/json-ld#context"'
}
response = requests.request("GET", url, headers=headers, data={})
if response.status_code == 200: return response.json(), False
else: return None, True
# get_entity_by_id: get last value recorded in the Context Broker of an entity (by its unique id) or several entities (by a string with a list of ids)
# Params:
# - entity_id: id or list of id concatenated as a string with commas e.g.: id1,id2,id3
# - entity_type: type of the entities requested
# Return:
# - array of entities
# - error: boolean specifying if there have been any errors throughout the function (associated with requests to the Context Broker or external instances)
def get_entity_by_id(entity_id, entity_type):
url = configuration_variables.broker_url + "entities/?id=" + entity_id
context_link = (
configuration_variables.base_context+entity_type.lower()+'-context.jsonld'
if entity_type in types
else configuration_variables.base_context + "default-context.jsonld"
)
headers = {
'Accept': 'application/ld+json',
'Link': '<'+context_link+'>;rel="http://www.w3.org/ns/json-ld#context"'
}
response = requests.request("GET", url, headers=headers, data={})
if response.status_code == 200: return response.json(), False
else: return None, True
# get_temporal_values_by_id: get temporal instances of attributes of a determined entity filtered by id and within a time window
# Params:
# - entity_id: id of the requested entity
# - entity_type: type of the requested entity
# - entity_date_string: date of the requested entity
# Return:
# - entity with arrays in attributes if there is more thant one temporal instance
# - error: boolean specifying if there have been any errors throughout the function (associated with requests to the Context Broker or external instances)
def get_temporal_values_by_id(entity_id, entity_type, entity_date_string):
entity_date = parser.parse(entity_date_string)
timeAt = (entity_date - relativedelta(minutes=configuration_variables.time_window)).strftime('%Y-%m-%dT%H:%M:%SZ')
url = configuration_variables.broker_url + "temporal/entities/"+entity_id+"?timerel=after&timeAt="+timeAt+"&lastN="+str(configuration_variables.lastN)
context_link = (
configuration_variables.base_context+entity_type.lower()+'-context.jsonld'
if entity_type in types
else configuration_variables.base_context + "default-context.jsonld"
)
headers = {
'Accept': 'application/ld+json',
'Link': '<'+context_link+'>;rel="http://www.w3.org/ns/json-ld#context"'
}
response = requests.request("GET", url, headers=headers, data={})
if response.status_code == 200: return response.json(), False
else: return None, True
def get_entities_by_type(entity_type):
url = configuration_variables.broker_url + "entities/?type="+entity_type+"&lastN=200"
context_link = (
configuration_variables.base_context+entity_type.lower()+'-context.jsonld'
if entity_type in types
else configuration_variables.base_context + "default-context.jsonld"
)
headers = {
'Accept': 'application/ld+json',
'Link': '<'+context_link+'>;rel="http://www.w3.org/ns/json-ld#context"'
}
response = requests.request("GET", url, headers=headers, data={})
if response.status_code == 200: return response.json(), False
else: return None, True
def delete_entities():
id_list=[]
# Temperature
url = configuration_variables.broker_url + "entities/?type=Temperature&limit=1000"
headers = {
'Accept': 'application/ld+json',
'Link': '<https://raw.githubusercontent.com/SALTED-Project/contexts/main/wrapped_contexts/energycim-context.jsonld>;rel="http://www.w3.org/ns/json-ld#context";type="application/ld+json"'
}
response = requests.request("GET", url, headers=headers, data={})
for i in response.json():
id_list.append(i['id'])
# DataQualityAssessment
url = configuration_variables.broker_url + "entities/?type=DataQualityAssessment&limit=1000"
headers = {
'Accept': 'application/ld+json',
'Link': '<https://raw.githubusercontent.com/SALTED-Project/contexts/main/wrapped_contexts/dataquality-context.jsonld>;rel="http://www.w3.org/ns/json-ld#context";type="application/ld+json"'
}
response = requests.request("GET", url, headers=headers, data={})
for i in response.json():
id_list.append(i['id'])
# DELETE
url = configuration_variables.broker_url + "entityOperations/delete"
payload = json.dumps(id_list)
headers = {
'Content-Type': 'application/ld+json'
}
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code != 204: print("DELETE /entities ", response.status_code)
# DELETE /temporal
for i in id_list:
url = configuration_variables.broker_url + "temporal/entities/"+i
response = requests.request("DELETE", url, headers=headers, data=payload)
if response.status_code != 204: print("DELETE /temporal/entities ", response.status_code)