Search code examples
pythonfiwarefiware-orion

Accessing Orion CB with python script


Would like to access orion data using a python script (not using curlor Postman). Below show the python script in my orion.pyscript:

import json
import requests

orion_endpoint="some-endpoint"

url_query=("orion_url" % (orion_endpoint))
body_dict = {
  'entities': [
    {
      'type': 'AirQualityObserved',
      'idPattern': '.*',
    }
  ],

}

r_headers = {'Content-Type': 'application/json'}
#print(data["coordinates"][0][0])
r = requests.post(
    url=url_query,
    data=json.dumps(body_dict),
    headers=r_headers
)

print(r.content) 

Running this script dumps the entity information to the console. How do I use the script to subscribe for notification so I get notified (not just dumping context)?


Solution

  • Orion Context Broker implements a REST API so any programming language able to do HTTP requests (and Python is one of them, e.g. using the requests module) can be used.

    To create a subscription you can use the same requests.post() you are using but with a different parametrization. In particular:

    • url will be the one corresponding to subscritions recourses in the API, i.e. /v2/entities.
    • data should follow the syntax of a subscription, according to the "Subscriptions" section in the NGSIv2 specification.
    • headers can be the same.

    In the case it may help, this script shows how to create subscriptions in Python.