forked from daniel-gonzalez-sanchez/ngsi-ld-client-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve-iot-device-entity.py
56 lines (43 loc) · 1.78 KB
/
retrieve-iot-device-entity.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
import logging
import logging.config
import os
import yaml
import ngsi_ld_client
from ngsi_ld_models.models.iot_device import IotDevice
from ngsi_ld_client.models.entity import Entity
from ngsi_ld_client.api_client import ApiClient as NGSILDClient
from ngsi_ld_client.configuration import Configuration as NGSILDConfiguration
from ngsi_ld_client.exceptions import ApiException
#assuming the log config file name is logging.yaml
with open('logging.yaml', 'r') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
#read the file to logging config
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
# NGSI-LD Context Broker
BROKER_URI = os.getenv("BROKER_URI", "http://localhost:1026/ngsi-ld/v1")
# Context Catalog
CONTEXT_CATALOG_URI = os.getenv("CONTEXT_CATALOG_URI",
"http://context-catalog:8080/context.jsonld")
# Init NGSI-LD Client
configuration = NGSILDConfiguration(host=BROKER_URI)
configuration.debug = True
ngsi_ld = NGSILDClient(configuration=configuration)
ngsi_ld.set_default_header(
header_name="Link",
header_value='<{0}>; '
'rel="http://www.w3.org/ns/json-ld#context"; '
'type="application/ld+json"'.format(CONTEXT_CATALOG_URI)
)
ngsi_ld.set_default_header(
header_name="Accept",
header_value="application/json"
)
api_instance = ngsi_ld_client.ContextInformationConsumptionApi(ngsi_ld)
try:
# Retrieve NGSI-LD Entity by id: GET /entities/{entityId}
api_response = api_instance.retrieve_entity(entity_id='urn:ngsi-ld:IotDevice:1')
logger.info(api_response.to_dict())
# logger.info(Entity.from_dict(api_response.to_dict()).to_dict())
except Exception as e:
logger.exception("Exception when calling ContextInformationConsumptionApi->retrieve_entity: %s\n" % e)