Implement External-Adapters
For the demo purpose, we will setup our external adapter to pull the data from weather api. Follow along this tutorial to understand how this works
Setting up external initiator requires following steps to be performed
  • Git clone the repository
  • cd & npm install
  • Include the API endpoint & Parse the payload
  • Run the server
Step 1 -
1
git clone https://github.com/GoPlugin/external-adapter-template.git
Copied!
Step 2 -
1
cd weather_adapter
Copied!
Step 3 -
1
npm install
Copied!
Step 4 -
1
map the api endpoint
Copied!

Requesting Data

When an external adapter receives a request from the Plugin node, the JSON payload will include the following objects:
  • data (guaranteed to be present but may be empty)
  • meta (optional, depends on job type)
  • responseURL (optional, will be supplied if job supports asynchronous callbacks)
  • id (optional, can be nice to use for EA logging to help debug job runs)
Returning Data
When the external adapter wants to return data immediately, it must include data in the returned JSON.
An example of the response data can look like:
1
{
2
"data": {
3
"result": "20.04"
4
}
5
}
Copied!
Note - the external adapter that you are running, should be the same URL endpoint mapped in Bridge.
1
const { Requester } = require('@goplugin/external-adapter')
2
require("dotenv").config();
3
4
const customError = (data) => {
5
if (data.Response === 'Error') return true
6
return false
7
}
8
9
const customParams = {
10
endpoint: ['endpoint']
11
}
12
13
const createRequest = (input, callback) => {
14
15
const url = `http://<host>/api/${input.data.endpoint}`
16
17
const config = {
18
url
19
}
20
21
if (process.env.API_KEY) {
22
config.headers = {
23
Authorization: process.env.API_KEY
24
}
25
}
26
Requester.request(config, customError)
27
.then(response => {
28
29
if (input.data.envCheck == "WindDirection") {
30
var resultData = response.data[0]['windDirection'];
31
} else if (input.data.envCheck == "Temperature") {
32
var resultData = response.data[0]['tempC'];
33
} else if (input.data.envCheck == "WindChill") {
34
var resultData = response.data[0]['windChillC'];
35
}
36
response.data.result = resultData.toString();
37
38
const res = {
39
data: {
40
"result": response.data.result.toString()
41
}
42
}
43
callback(response.status, Requester.success(input.id, res));
44
})
45
.catch(error => {
46
callback(500, Requester.errored(input.id, error))
47
})
48
}
49
50
module.exports.createRequest = createRequest
Copied!
Let's see what's in "index.js"
  • We are importing 'external-adapter' from goplugin npm repo and importaing two methods such as Requester
  • importing "dotenv" module to access the .env file for sensitive info (API_KEY)
  • "customError" is a function to return either true or false if error occurs
  • "customParams" is a variable to declare custom parametes to pass on in URL - this can be customized accordingly
  • "createRequest" is the method actually doing heavy lifting.
    • URL is getting defined, since for weather node the endpoint is getting passed via smart contract the "endpoint" is accessed via "input.data.endpoint"
    • <host> to be replaced with respective domain name or ip address where we are fetching the data
    • api -> router name.. Again for our weather node, we have api as routername, for your case it can be different
    • config -> variable name to have url parm
    • Next, we are checking APIKEY if exists in env parm, else it will not be applied.. For authenticated API, you have to pass APIKEY
    • Requester.request will scan for the payload from api endpoint and customized the output .. In this example, we are checking "input.data.envCheck"
      • Note that, envCheck is a parameter we are passing via smart contract(Check weathernode)
      • Based on envCheck, the loop iterates and scan the value form output payload
      • Finally we are construing the response and return
When you run PM2 Start server.js, the server will by default run in port 5001 and just make sure the bridge is correctly pointed out to this port number.. For instance, this should be http://localhost:5001/
Copy link
Contents