This project provides node.js based REST API server according to the OIC (Open Interconnect) core specification.
The project is experimental at the moment and APIs provided are work in progress and subject to changes.
npm install -g iot-rest-api-server
Run with: $ iot-rest-api-server
.
npm install iot-rest-api-server
Must have a
package.json
file in your current directory. Also you may want to add the--save
flag tonpm install
so thatpackage.json
knows your project requiresiot-rest-api-server
.
Run with: $ ./node_modules/iot-rest-api-server/bin/iot-rest-api-server.js
.
Start the API server
$ iot-rest-api-server
The command line options
$ iot-rest-api-server -h
Options
-h, --help
-v, --verbose
-p, --port number
-s, --https
More verbose logging
Listen to specific port. Default is 8000
Use https with TLS instead of plain http. In order to use https the config
directory needs to contain the following certificate and private key files (in
PEM format)
certificate.pem
private.key
You can use the config/generate-key-and-cert.sh
to generate the files for
testing only purposes. The certificate is self signed and browsers do not
recognise it so you will get warnings.
The recommended way to use the https is to get proper certificate from know
certificate authority and corresponding private key and place those to the
config
directory.
/api/system
/api/oic
npm install node-gyp -g
The REST APIs are documented in the doc folder using the
RAML modeling language. You also need the raml2html
node
module to produce the documentation:
npm install -g raml2html
The API documentation can be generated with
raml2html doc/name-of-the-raml-file > api.html
For example
raml2html doc/oic.wk.res.raml > oic-res.html
The .html
file can be then opened by a browser. The .html
file contains the
full documentation of the REST API including all the REST methods (GET, POST,
DELETE, etc) supported, query parameters (like ?id=foo) and the JSON formats in
each API.
The following examples assumes the iot-rest-api-server runs on IP address: 192.168.0.1, port 8000.
Get the system status:
http://192.168.0.1:8000/api/system
Discover all the OIC enabled devices on the local network:
http://192.168.0.1:8000/api/oic/res
See the more detailed API documentation in the chapter above.
The test directory contains the following small test utilities:
Send HTTP GET to /api/oic
endpoint. Environment variables API_SERVER_HOST
and API_SERVER_PORT
are used to construct the authority part of the URL.
export API_SERVER_HOST=10.211.55.3
# Discover
./test/oic-get /res
# Retrieve (href and di are from the discovery above)
./test/oic-get <href>?di=<di>
# Observe
./test/oic-get <href>?di=<di>&obs=1
Send HTTP POST to /api/oic
endpoint. Environment variables API_SERVER_HOST
and API_SERVER_PORT
are used to construct the authority part of the URL.
First parameter is uri
from the discover (/res
) and second is JSON files
with the properties that are being set.
API_SERVER_HOST=10.211.55.3
./test/oic-post <href>?di=<di> <file-name-for-body>
Performs the OIC discovery. On discovered resources performs a) GET or b) OBSERVE operations
./test/oic-api-tester.js -?
# Start observing all found resources
./test/oic-api-tester.js -h 10.0.0.1 -p 8000 -o
If you are running Chrome and want to see the JSON objects in nicely formatted way, install the JSONView extension.
Another great tool for REST API development and testing is Postman, another Chrome extension.