Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Support
    • Submit feedback
    • Contribute to GitLab
  • Sign in
N
naiades-platform-poc
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Analytics
    • CI / CD Analytics
    • Repository Analytics
    • Value Stream Analytics
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • naiades
  • naiades-platform-poc
  • Wiki
  • API client examples (curl and python scripts)

API client examples (curl and python scripts)

Last edited by Federico Sismondi Dec 13, 2020
Page history

Prepare environment

After cloning the git repo please prepare your environment (using development environment)

export ORION_HOST=5.53.108.182
cd scripts

If you have deployed the IoT platform on your own, then use the correct IP of the deployment

Create entity

Create the entity Flowerbed number x:

for more see watering_01_create_entity.sh

curl -iX POST \
    "http://$ORION_HOST:1026/v2/entities/" \
    --header "Fiware-Service: carouge" \
    --header "Content-Type: application/json" \
    --data '{
    "id": "urn:ngsi-ld:FlowerBed:FlowerBed-2",
    "type": "FlowerBed",
    "dateLastWatering": {
        "type": "DateTime",
        "value": "1970-01-01T01:00:00.00Z",
    },
    "flowerType": {
        "value": "Perennial",
    },
    "location": {
        "type": "geo:json",
        "value": {
            "coordinates": [
                6.13397,
                46.18261
            ],
            "type": "Point"
        },
    },
    "nextWateringAmountRecommendation": {
        "value": 0.5,

    },
    "nextWateringDeadline": {
        "type": "DateTime",
        "value": "2020-10-27T14:59:16UTC",
    },
    "refDevice": {
        "value": "0018b20000020a35",
    },
    "soilMoisture": {
        "value": 0,
    },
    "sunExposure": {
        "value": "Mid shadow",
    }
}'

Retrieve all entitys

for more see script watering_02_retrieve_entities.sh

Get all entities, note that the response includes type information"


curl --location --request GET \
    "http://$ORION_HOST:1026/v2/entities/" \
    --header "Fiware-Service: carouge" \
    | python -mjson.tool

which returns something like this:

[
  {
        "id": "urn:ngsi-ld:FlowerBed:FlowerBed-7",
        "location": {
            "metadata": {},
            "type": "geo:json",
            "value": {
                "coordinates": [
                    0,
                    0
                ],
                "type": "Point"
            }
        },
        "nextWateringAmountRecommendation": {
            "metadata": {},
            "type": "DateTime",
            "value": "1970-01-01T01:00:00.00Z"
        },
        "nextWateringDeadline": {
            "metadata": {},
            "type": "Text",
            "value": "2020-10-23T14:59:16UTC"
        },
        "refDevice": {
            "metadata": {},
            "type": "Text",
            "value": "None"
        },
        "soilMoisture": {
            "metadata": {},
            "type": "Number",
            "value": 0
        },
        "sunExposure": {
            "metadata": {},
            "type": "Text",
            "value": "Mid shadow"
        },
        "type": "FlowerBed"
    }
(...)

In the following we propose a simpler type-less query, to be used just when you do a GET (if you push data using keyValues then the types of the attributes are overwritten to TEXT type, not good!)

>> ./watering_03_retrieve_entity_keyValues_by_id.sh
Querying context broker at: 5.53.108.182
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   355  100   355    0     0   1338      0 --:--:-- --:--:-- --:--:--  1334
{
    "category": [
        "urbanTreeSpot"
    ],
    "dateLastWatering": "2017-03-31T08:00",
    "dateModified": "2017-03-31T08:00",
    "id": "urn:ngsi-ld:FlowerBed:FlowerBed-1",
    "nextWateringDeadline": "2017-04-31T08:00",
    "soilMoisture": 0.85,
    "soilTemperature": 17,
    "type": "FlowerBed"
}

Update attributes of entity

Here below an example for updating attrs values of a WeatherObserved entity for ALINCANTE pilot.

NOTE: be careful with the types!


curl --location --request PATCH 'http://5.53.108.182:1026/v2/entities/urn:ngsi-ld:WeatherObserved:WeatherObserved-1/attrs' \
--header 'Fiware-Service: alicante' \
--header 'Content-Type: application/json' \
--data-raw '{     
        "dateObserved": {
            "type": "DateTime",
            "value": "2020-10-20T13:45:57.00Z",
            "metadata": {}
        },
        "dewPoint": {
            "type": "Number",
            "value": 13.2,
            "metadata": {}
        }
}'

If you are updating a single attribute then can it becomes even simpler by pointing the `/value` at the URL, e.g. pushing a random value to soilMoisture value of Carouge pilot

```bash
curl --location --request PUT \
    "http://$ORION_HOST:1026/v2/entities/urn:ngsi-ld:FlowerBed:FlowerBed-1/attrs/soilMoisture/value"\
    --header "Fiware-Service: carouge" \
    --header "Content-Type: text/plain" \
    -d $(( RANDOM % 10 ))

You can check that the entity was updated in several ways, but the simplest would be:

curl --location --request GET 'http://5.53.108.182:1026/v2/entities/urn:ngsi-ld:FlowerBed:FlowerBed-2/attrs/soilMoisture' \
--header 'Fiware-Service: carouge'

go test on your own running the following scripts:

watering_01_create_entity.sh
watering_02_retrieve_entities.sh
watering_03_retrieve_entity_keyValues_by_id.sh
watering_04_retrieve_attribute_value.sh
watering_05_retrieve_attribute.sh
watering_06_update_specific_attrs.sh

Subscriptions

This will demonstrate how a WMS or GUI backend component can subscribe to changes/updates of entities

What you need:

- a webserver listening on a certain IP:port (docker-compose includes one already called `wms-app-example`)
- scripts:
  watering_07_subscribe_to_entity.sh
  admin_get_all_orion_susbcriptions.sh
  watering_06_update_specific_attrs.sh

Export new env var about the subscription callback (note my webserver runs locally, that's why I used a private IP):

export SUBSCRIPTION_URL=http://172.18.1.11:5000/on_change_soilMoisture

Subscribe to a certain entity , attr, condition, etc:

Check out what the subscribe message looks like:

>>> cat watering_07_subscribe_to_entity.sh

      (...)

      "description": "A subscription to subscribe to FlowerBed.soilMoisture",
      "subject": {
        "entities": [
          {
            "idPattern": ".*",
            "type": "FlowerBed"
          }
        ],
        "condition": {
          "attrs": [
            "soilMoisture"
          ]
        }
      },
      "notification": {
        "http": {
          "url": "'${SUBSCRIPTION_URL}'"
        },
        "attrs": [
          "soilMoisture"
        ],
      },
      "throttling": 5
    }'

execute subscription:

>>> ./watering_07_subscribe_to_entity.sh
Querying context broker at: 5.53.108.182
Callback URL for subscriptions is: http://172.18.1.11:5000/on_change_soilMoisture

check if our subscription was accepted, and present into orion subscribers:

>>> ./admin_get_all_orion_susbcriptions.sh
Querying context broker at: 5.53.108.182
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   401  100   401    0     0   8358      0 --:--:-- --:--:-- --:--:--  8531
[
    {
        "description": "A subscription to subscribe to FlowerBed.soilMoisture",
        "id": "5ec4f6bf91d551c2b4612f3e",
        "notification": {
            "attrs": [
                "soilMoisture"
            ],
            "http": {
                "url": "http://172.18.1.11:5000/on_change_soilMoisture"
            },
            "onlyChangedAttrs": false
        },
        "status": "active",
        "subject": {
            "condition": {
                "attrs": [
                    "soilMoisture"
                ]
            },
            "entities": [
                {
                    "id": "*",
                    "type": "FlowerBed"
                }
            ]
        },
        "throttling": 5
    }
]

Test subscription by sending an update into the entity and verifying if our webserver received http callback:

send a couple of entity updates:

>>> ./watering_06_update_specific_attrs.sh
>>> ./watering_06_update_specific_attrs.sh
Querying context broker at: 5.53.108.182

callback:

[2020-05-20 09:26:57,280] INFO in app: Got new soil moisture value from context broker: [{'id': 'urn:ngsi-ld:FlowerBed:FlowerBed-1', 'type': 'FlowerBed', 'soilMoisture': 6}]
172.18.1.9 - - [20/May/2020 09:26:57] "POST /on_change_soilMoisture HTTP/1.1" 200 -
[2020-05-20 09:27:01,500] INFO in app: Got new soil moisture value from context broker: [{'id': 'urn:ngsi-ld:FlowerBed:FlowerBed-1', 'type': 'FlowerBed', 'soilMoisture': 7}]
172.18.1.9 - - [20/May/2020 09:27:01] "POST /on_change_soilMoisture HTTP/1.1" 200 -

Yay! we received the subscription updates! :)

see more about subscription params here:

https://fiware-tutorials.readthedocs.io/en/latest/subscriptions/index.html

Historic API

The Historic API is implemented using QuantumLeap (FiWare enabler)

export env var for QuantumLeap host (same as Naiades IoT Platform)

export QUANTUM_HOST=5.53.108.182

Create subscription for subscribing quantum leap to orion, and hence to keep history of a certain entity/attr. This is a suscription for keeping history of all Carouge FlowerBed's humidity measurements.

Check out the suscription and run it!

>>> cat historic_01_set_up.sh
#!/usr/bin/env bash

[[ -z "$ORION_HOST" ]] && echo "Please set ORION_HOST env var. E.g. export ORION_HOST=127.0.0.1" && exit
echo "Querying context broker at: ${ORION_HOST}"

# API NOTES:
# "condition": { "attrs": [] } -> means monitor all the attributes in the entity

curl --location --request POST \
    "http://$ORION_HOST:1026/v2/subscriptions/" \
    --header "Fiware-Service: carouge" \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" \
    --data '{
      "description": "Notify QuantumLeap, the historic API, of all FlowerBed changes",
      "subject": {
        "entities": [
          {
            "idPattern": ".*",
            "type": "FlowerBed"
          }
        ],
        "condition": {
          "attrs": []
        }
      },
      "notification": {
        "http": {
          "url": "http://172.18.1.7:8668/v2/notify"
        },
        "attrs": [],
        "metadata": ["dateCreated", "dateModified"]
      }
}'

>>> ./historic_01_set_up.sh
Querying context broker at: 5.53.108.182

send a couple of entity updates:

>>> ./watering_06_update_specific_attrs.sh
>>> ./watering_06_update_specific_attrs.sh
Querying context broker at: 5.53.108.182

check what the history looks like of the FloweBeds' humidty looks like:

>>> ./historic_02_api_query.sh
Querying QuantumLeap API at: 5.53.108.182
{
  "index": [
    "2017-03-31T08:00:00.000",
    "2017-03-31T08:00:00.000",
    "2017-03-31T08:00:00.000",
    "2017-03-31T08:00:00.000"
  ],
  "values": [
    0.85,
    4.0,
    7.0,
    9.0
  ]
}

The complete documentation for the API can be found here:

swagger hub link to doc

Clone repository
  • API client examples (curl and python scripts)
  • Carouge Watering Use Case
  • IoT Platform FAQ
  • IoT Platform operations
  • NAIADES IoT Platform installation
  • Pushing Data from pilots to IoT Platform
  • Home
  • rewriting history
  • securing API with PEP proxy
More Pages