exports.handler = (event, context, callback) => {
// Streamdata Dependencies
var streamdataio = require('streamdataio-js-sdk/dist/bundles/streamdataio-node');
var AuthStrategy = require('streamdataio-js-sdk-auth');
// All The Other Dependencies
var jsonPatch = require('fast-json-patch');
var print = require('node-print');
var AWS = require('aws-sdk');
function server()
{
// targetUrl is the JSON API you wish to stream
var targetUrl = process.env.targetUrl;
// q is the query parameter to search by
var query = process.env.query;
targetUrl = targetUrl + '?q=' + query;
// appToken is the Streamdata.io token
var appToken = process.env.appToken;
// oauthToken is the Twitter Oauth Token
var oauthToken = process.env.oauthToken;
// userAgent is used to identify your client
var userAgent = process.env.userAgent;
var privateKey = '';
var headers = ['Authorization: Bearer ' + oauthToken,'User-Agent: ' + userAgent];
var eventSource = streamdataio.createEventSource(targetUrl, appToken, headers, AuthStrategy.newSignatureStrategy(appToken, privateKey));
var result = [];
eventSource
// the standard 'open' callback will be called when connection is established with the server
.onOpen(function ()
{
console.log("connected!");
})
// the streamdata.io specific 'data' event will be called when a fresh Json data set
// is pushed by Streamdata.io coming from the API
.onData(function (data)
{
console.log("data received");
// memorize the fresh data set
result = data;
console.log(result);
// if some criteria is met
criteria = '';
if(criteria == 'met')
{
// An object of options to indicate where to post to
var post_options = {
host: 'example.com',
port: '80',
path: '/webhook-location/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(result)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(result);
post_req.end();
}
})
// the streamdata.io specific 'patch' event will be called when a fresh Json patch
// is pushed by streamdata.io from the API. This patch has to be applied to the
// latest data set provided.
.onPatch(function (patch)
{
// display the patch
console.log("patch: ", patch);
// apply the patch to data using json patch API
jsonPatch.applyPatch(result, patch);
// if some criteria is met
criteria = '';
if(criteria == 'met')
{
// An object of options to indicate where to post to
var post_options = {
host: 'example.com',
port: '80',
path: '/webhook-location/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(result)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(result);
post_req.end();
}
})
// the standard 'error' callback will be called when an error occur with the evenSource
// for example with an invalid token provided
.onError(function (error)
{
console.log('ERROR!', error);
eventSource.close();
});
eventSource.open();
}
console.log('starting');
server();
// TODO implement
callback(null, 'Hello from Lambda');
};
Try streaming any JSON REST API within 30 sec
curl -v "https://proxy.streamdata.io/http://stockmarket.streamdata.io/prices/"
There are 0 comments.
Have something to say?
Drop a comment below.