Skip to content
This repository has been archived by the owner on May 6, 2021. It is now read-only.
Lukas Brinkmann edited this page Dec 19, 2017 · 2 revisions

Introduction

Welcome to the iot-application-services-sdk-nodejs wiki!

API

General

new nodeAE()

Initializes a new NodeWrapper.

The SDK requires the following environment-variables while initializing:

  • AE_OAUTH_CLIENT_ID
  • AE_OAUTH_CLIENT_SECRET
  • AE_TENANT
  • AE_LANDSCAPE
  • AE_HOST

You can provide these variables

  1. directly in your environment (e.g. Cloud Foundry),
  2. create a file called .env in the root of your project or
  3. pass a json object to the constructor containing the following variables: clientId, clientSecret, tenant, landscape, host.

Example:

var nodeAE = new NodeAE()

nodeAE.setBaseURI(sMicroservice) ⇒ sBaseUri

Sets the base URI of the module. This base URI is contacted for all further requests as long as a new base URI is set or the base URI is removed (c.f. Example). The base URI is only used, if you pass a valid resource path to a request. You can skip the use of the base URI if you pass a fully qualified URI to a request. In this case the base URI will be ignored.

Param Type
sMicroservice String

Following helper functions are available:

  • getBaseURI()
  • deleteBaseURI()

HTTP-Requests

nodeAE._basicRequest(sHttpMethod, sURI, oBody, aHeaders) ⇒ Promise

Template for specific requests to the API of SAP IoT AE.

Returns: Promise - oResponse - resolved / rejected with the response from the API

Param Type Description
sHttpMethod String the http method of the request
sURI String the URI to request
oBody JSON the body of the request (only type JSON supported)
aHeaders JSON additional headers of the http request

Example

nodeAE._basicRequest('POST', 'https://location.cfapps.eu10.hana.ondemand.com/Locations', { <json-payload> }, { 'If-Match': '3' })

nodeAE.post(sURI, oBody, aHeaders) ⇒ Promise

Sends a POST-request to the API of SAP IoT AE creating a new instance of a database entity managed by a service.

Param Type
sURI String
oBody JSON
aHeaders JSON

See: _basicRequest for further information.

nodeAE.get(sURI, aHeaders) ⇒ Promise

Sends a GET request to the API of SAP IoT AE retrieving multiple database entities managed by a particular serviceular service.

Param Type
sURI String
aHeaders JSON

See: _basicRequest for further information.

nodeAE.put(sURI, oBody, aHeaders) ⇒ Promise

Sends a PUT request to the API of SAP IoT AE modifying a database entity managed by a serviceponse.

Param Type
sURI String
oBody JSON
aHeaders JSON

See: _basicRequest for further information.

nodeAE.delete(sURI, aHeaders) ⇒ Promise

Sends a DELETE request to the API of SAP IoT AE deleting a database entity managed by a service (logically or physically, depending on the service).

Param Type
sURI String
aHeaders JSON

See: _basicRequest for further information.

Streams

nodeAE._basicStream(sHttpMethod, sURI, oBody, aHeaders) ⇒ Promise

Opens a stream to the API of SAP IoT AE.

Param Type Description
sHttpMethod String the http method of the request
sURI String - the URI to request - if a base URI is set, you can just pass the resource-path (+ optional query options)
oBody JSON the body of the request (only type JSON supported)
aHeaders JSON additional headers of the http request

Example

nodeAE.setBaseURI('appiot-mds')
var loadingStream = nodeAE._basicStream('GET', '/Things')
loadingStream.then(
  function success (stream) {
    stream.pipe(fs.createWriteStream('./output.txt')) // will write all Things to 'output.txt'
  },
  function error (err) {
    console.log(err)
  }
)

nodeAE.openPostStream(sURI, oBody, aHeaders) ⇒ Promise

Sends a POST request to the API of SAP IoT AE and streams the response.

Param Type
sURI String
oBody JSON
aHeaders JSON

See: _basicStream for further information.

nodeAE.openGetStream(sURI, aHeaders) ⇒ Promise

Sends a GET request to the API of SAP IoT AE and streams the response.

Param Type
sURI String
aHeaders JSON

See: _basicStream for further information.

nodeAE.openPutStream(sURI, oBody, aHeaders) ⇒ Promise

Sends a PUT request to the API of SAP IoT AE and streams the response.

Param Type
sURI String
oBody JSON
aHeaders JSON

See: _basicStream for further information.

nodeAE.openDeleteStream(sURI, aHeaders) ⇒ Promise

Sends a DELETE request to the API of SAP IoT AE and streams the response.

Param Type
sURI String
aHeaders JSON

See: _basicStream for further information.

Examples

Setting a base URI

var NodeAE = require('sap-iot-ae-node-wrapper')
var nodeAE = new NodeAE()

nodeAE.setBaseURI('appiot-mds') // setting a base URI -> all further requests will be sent to this app
var oPromise = nodeAE.get('/Things') // no need to specify the full URI -> just resource path und query options are needed
oPromise.then(
  function success (oResponse) {
    console.log(oResponse)
  },
  function error (err) {
    throw err
  }
)

Creating a Location

var NodeAE = require('sap-iot-ae-node-wrapper')

var nodeAE = new NodeAE()
// use plain http methods to send request (post, get, put, delete)
var oPromise = nodeAE.post('https://location.cfapps.eu10.hana.ondemand.com/Locations', 
  {
    'basicData': {
      'tenant': 'sap-iotaehandson'
    },
    'locationData': {
      'streetName': 'Hasso-Plattner-Ring',
      'houseNumber': '7',
      'cityName': 'Walldorf',
      'postalCode': '69190',
      'country': 'DE',
      'longitude': 8.636881,
      'latitude': 49.294464
    }
  }
)
oPromise.then(
  function success (oResponse) {
    console.log(oResponse)
  },
  function error (err) {
    throw err
  }
)

Setting additional http headers

var NodeAE = require('sap-iot-ae-node-wrapper')

var nodeAE = new NodeAE()
var oPromise = nodeAE.put('https://location.cfapps.eu10.hana.ondemand.com/Locations', 
  {
    'basicData': {
      'tenant': 'sap-iotaehandson'
    },
    'locationData': {
      'streetName': 'Hasso-Plattner-Ring',
      'houseNumber': '7',
      'cityName': 'Walldorf',
      'postalCode': '69190',
      'country': 'DE',
      'longitude': 8.636881,
      'latitude': 49.294464
    }
  },
  { 
    'If-Match': '3' // setting an additional http header
  } 
)
oPromise.then(
  function success (oResponse) {
    console.log(oResponse)
  },
  function error (err) {
    throw err
  }
)

Streaming data

var NodeAE = require('sap-iot-ae-node-wrapper')
var fs = require('fs')

// start streaming
var openingStream = nodeAE.openGetStream('/Things')
openingStream.then(
    function success (stream) {
      stream.pipe(fs.createWriteStream('./output.txt')) // will stream the data to output.txt
    },
    function error (err) {
      console.log(err)
    }
)

Streaming data into chunks

Streams data stored in AE into multiple files (with a specified size).

Use-Case: Back-up of data stored in AE. When requesting the data, there is an RAM overflow on your local machine (to much data). You can prevent this problem by streaming the data and splitting it into multiple files with a specific size (e.g. 500 MB).

var NodeAE = require('sap-iot-ae-node-wrapper')
var fs = require('fs')
var chunkingStreams = require('chunking-streams')

// init the node-wrapper
var nodeAE = new NodeAE()
nodeAE.setBaseURI('appiot-mds')

// init the chunker
var chunker = new chunkingStreams.SizeChunker({
  chunkSize: 1024 // file-size = 1024 Byte
})
var output

chunker.on('chunkStart', function (id, done) {
  output = fs.createWriteStream('./output-' + id + '.txt')
  done()
})

chunker.on('chunkEnd', function (id, done) {
  output.end()
  done()
})

chunker.on('data', function (chunk) {
  output.write(chunk.data)
})

// start streaming
var openingStream = nodeAE.openGetStream('/Things')
openingStream.then(
    function success (stream) {
      stream.pipe(chunker)
    },
    function error (err) {
      console.log(err)
    }
)
Clone this wiki locally