Implement External -Adapters
For the demo purpose, we will setup our external adapter to pull the data from weather API. Follow along the following tutorial to understand how it works.
Setting up external initiator requires the following steps to be performed
Git clone the repository
cd & npm install
Include the API endpoint & Parse the payload
Run the server
Step 1 -
Step 2 -
Step 3 -
Step 4 -
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 this:
Note - The external adapter that you are running, should be the same URL endpoint mapped in Bridge.
Let's see what's in "index.js"
We are importing 'external-adapter' from goplugin npm using importing 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 an error occurs.
"customParams" is a variable to declare parameters to pass on in URL - this can be customized accordingly.
"createRequest" is the method actually doing the heavy lifting.
URL is getting defined since for the weather node the endpoint is getting passed via smart contract the "endpoint" is accessed via "input.data.endpoint".
<host> to be replaced with a respective domain name or IP address where we are fetching the data
api -> router name. Again for our weather node, we have API as rrouter name for your case it can be a 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 the API endpoint and customize the output .. In this example, we are checking "input.data.envCheck".
Note that, envCheck is a parameter we are passing via smart contract(Check weather node)
Based on envCheck, the loop iterates and scans the value from the output payload
Finally we are constructing 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/
Last updated