Simple read of a public API

This flow is a simple bare bone to read a public API at https://sabesp-api.herokuapp.com/{{datastring}}.

Datastring is something like this: "2015-01-01" (yyyy-mm-dd). The public API output is a JSON with data from 6 water reservoirs for that day.

The idea is to have a flow to show - by simple console.log() exhibition - data from several days.

The flow is kicked-off by an simple blank inject node. You set the data to control the flow - the start date and the number of days - in the second node, named Dados. This node also has some constructions for the loop. The remaining nodes are for the request and the parse.

There is a change node, called "Header off", that is key to make the several http requests.

[{"id":"2560dd5b.da9f22","type":"inject","name":"Kick-off","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":103,"y":72,"z":"ac76642a.538998","wires":[["566038a3.a99fc8"]]},{"id":"566038a3.a99fc8","type":"function","name":"Dados","func":"msg.max=5;\nmsg.i=0;\nmsg.datastring=\"02/01/2015\"\nmsg.data1 = new Date(msg.datastring);\nreturn msg;","outputs":1,"noerr":0,"x":153,"y":141,"z":"ac76642a.538998","wires":[["678867b9.987798"]]},{"id":"678867b9.987798","type":"function","name":"Separa","func":"//console.log(msg);\na1 = (\"0\"+(msg.data1.getMonth()+1)).slice(-2);\na2 = (\"0\"+msg.data1.getDate()).slice(-2);\nmsg.datastring=msg.data1.getFullYear()+\"-\"+a1+\"-\"+a2;\nmsg.data1.setDate(msg.data1.getDate()+1);\nreturn msg;","outputs":1,"noerr":0,"x":231,"y":209,"z":"ac76642a.538998","wires":[["87600cca.789ff"]]},{"id":"c433bad4.3bcc48","type":"function","name":"Next","func":"if ( (msg.i += 1) < msg.max ) return msg;","outputs":1,"noerr":0,"x":388,"y":544,"z":"ac76642a.538998","wires":[["678867b9.987798"]]},{"id":"87600cca.789ff","type":"http request","name":"","method":"GET","ret":"txt","url":"https://sabesp-api.herokuapp.com/{{datastring}}","x":318,"y":266,"z":"ac76642a.538998","wires":[["84abee58.7b541"]]},{"id":"84abee58.7b541","type":"function","name":"JSON parse","func":"msg.payload = JSON.parse(msg.payload);\n// Colocando o nome dos reservatorio uma vez\nif( msg.i === 0 ) {\n v1 =[]; // v1 matrix\n for (i=0;i<6;i++) { v1[i] = msg.payload[i].name; }\n console.log(v1);\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":416,"y":329,"z":"ac76642a.538998","wires":[["5e0e2f7b.a1f1d"]]},{"id":"915be2cb.6ea42","type":"change","name":"Header off","rules":[{"t":"set","p":"headers","to":"null"}],"action":"","property":"","from":"","to":"","reg":false,"x":561,"y":473,"z":"ac76642a.538998","wires":[["c433bad4.3bcc48"]]},{"id":"5e0e2f7b.a1f1d","type":"function","name":"Parse2","func":"v1 =[]; // v1 matrix\nfor (i=0;i<6;i++) {\n v1[2*i] = msg.payload[i].data[0].value;\n v1[2*i+1] = msg.payload[i].data[1].value;\n}\nconsole.log(v1);\nreturn msg;","outputs":1,"noerr":0,"x":487,"y":400,"z":"ac76642a.538998","wires":[["915be2cb.6ea42"]]}]
MarcioAB

Flow Info

created 2 years, 2 months ago

Node Types

Core
  • change (x1)
  • function (x5)
  • http request (x1)
  • inject (x1)

Tags

  • API
  • loop
  • simple
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option