Skip to content
Petification Develop Blog

Chapter 9. Creating ReST Interface

HaeramKim, BuildYourOwnIoTPlatform1 min read

NOTE: this notes are from “Build Your Own IoT Platform” by Anand Tamboli. And I can’t speak english very well, so some sentences or word might be inappropriate and might have some misunderstandings.

Make “ReST/GET Records Based on Condition” Funtionality

  • It’s simple; Fetch some records by topic-like, payload-like, count.
  • HTTP in node: Set method as GET and URL as /records/topic-like/:topic/payload-like/:payload/last/:count.
  • function node:
1// Create query
2
3// if no authentication filter defined or available
4// set the default value as 1
5if(!msg.req.authFilter)
6 msg.req.authFilter = 1;
7
8// wildcard used for API query is * and this needs to be converted into SQL wildcard character %
9msg.topic = "SELECT id,topic,payload,timestamp" +
10 " FROM thingsData WHERE" +
11 " topic LIKE '" + msg.req.params.topic.
12 replace(/\*/g, "%") + "'" +
13 " AND" +
14 " payload LIKE '" + msg.req.params.payload.
15 replace(/\*/g, "%") + "'" +
16 " AND deleted=0" +
17 " AND (" + msg.req.authFilter + ")" +
18 " ORDER BY ID DESC" +
19 " LIMIT " + msg.req.params.count + ";";
20
21return msg;
  • Note that we set default of authFilter to 1. It means we’re gonna allow all un-authorized user for now.
  • Check " AND (" + msg.req.authFilter + ")" + part: If authFilter is 1, this means true, so this query will works normally, but if it’s 0, this means false, so this query will fetch nothing.

Deleting from DB

  • Note that all the APIs has same structure and procedure; all the differences between them are just ReST API endpoints and DB query.
  • There are two ways to accomplish this: Recoverable and Permanent.

Recoverable Deletation

  • Recoverable deletation is just updating value of deleted column to 1.
  • Since this is just a updating the DB, so i think it’s better to use PATCH HTTP method instead of GET method.

Permanent Deletation

  • Permanent deletation is deleting the instance inside of DB.
  • Unlike Recoverable Deletation, i’m decide to use DELETE HTTP method to do it.

Microservice Utilities

ReST/GET Current Timetamp

  • This functionality coordinates with M2 API.
  • Set ReST Endpoint to /timestamp, method to GET.
  • And set function node to:
1msg.payload = {
2 timestamp: (new Date()).getTime().toString()
3};
4return msg;

ReST/GET Random Code

  • This functionality coordinates with M3 API.
  • Set ReST Endpoint to /randomcode/:len, method to GET.
    • :len represents length of the random code.
  • And set function node to:
1var randomString = function(length) {
2 var text = "";
3 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
4 for(var i = 0; i < length; i++) {
5 text += possible.charAt(Math.floor(Math.random() * possible.length));
6 }
7 return text;
8}
9
10msg.payload = {
11 code: randomString(msg.req.params.len)
12};
13
14return msg;

ReST/GET UUID

  • This functionality coordinates with M4 API.
  • For this functionality, we have to install node-red-contrib-uuid plugin to generate UUID.
  • After that, all we have to do is just pass the generated UUID to HTTP response.
  • So, set ReST Endpoint to /uuid, method to GET.
  • And set function node to:
1// Prepare response
2msg.payload = {
3 uuid: msg.payload
4};
5
6return msg;

And other…

  • To implement M5, M6 API, we need to use SendGrid and Twilio.
  • But to use both of ‘em, we have to pay some money. So, let’s skip these for now.
© 2022 by Petification Develop Blog. All rights reserved.
Theme by LekoArts