Created
February 12, 2018 00:33
-
-
Save mocon/ca9cc77830f480be025e62d97d83f1b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fetch = require('node-fetch'); | |
const moment = require('moment'); | |
const QUANDL_API_BASE_URL = 'https://www.quandl.com/api/v3'; | |
const QUANDL_API_KEY = process.env.QUANDL_API_KEY; | |
/* | |
checkStockPrice() | |
To test locally: | |
`SLS_DEBUG=true sls invoke -f checkStockPrice -l -p sampleData.json --QUANDL_API_KEY ****` | |
*/ | |
module.exports.checkStockPrice = (event, context, callback) => { | |
const { | |
symbol, | |
start_date, | |
end_date, | |
transform | |
} = event.queryStringParameters; | |
if (symbol && start_date && end_date && transform) { | |
const url = `${QUANDL_API_BASE_URL}/datasets/WIKI/${symbol}/data.json?start_date=${start_date}&end_date=${end_date}&transform=${transform}&api_key=${QUANDL_API_KEY}`; | |
fetch(url) | |
.then(res => res.json()) | |
.then(json => { | |
const response = { | |
statusCode: 200, | |
headers: { 'Access-Control-Allow-Origin': '*' }, | |
body: JSON.stringify({ | |
message: `Function checkStockPrice(symbol: ${symbol}, startDate: ${start_date}, endDate: ${end_date}, transform: ${transform}) executed successfully!`, | |
response: json | |
}) | |
}; | |
callback(null, response); | |
}); | |
} else { | |
const response = { | |
statusCode: 500, | |
headers: { 'Access-Control-Allow-Origin': '*' }, | |
body: JSON.stringify({ | |
message: 'Missing query parameter.', | |
queryStringParameters: event.queryStringParameters, | |
input: event | |
}) | |
}; | |
callback(null, response); | |
} | |
}; | |
/* | |
checkStockRecent() | |
To test locally: | |
`SLS_DEBUG=true sls invoke -f checkStockRecent -l -p sampleData.json --QUANDL_API_KEY ****` | |
*/ | |
module.exports.checkStockRecent = (event, context, callback) => { | |
const { | |
symbol, | |
start_date, | |
last_days | |
} = event.queryStringParameters; | |
if (symbol && start_date && last_days) { | |
const endDate = moment(start_date).subtract(last_days, 'days'); | |
const prettyEndDate = moment(endDate).format('YYYY-MM-DD'); | |
const url = `${QUANDL_API_BASE_URL}/datasets/WIKI/${symbol}/data.json?start_date=${prettyEndDate}&end_date=${start_date}&api_key=${QUANDL_API_KEY}`; | |
fetch(url) | |
.then(res => res.json()) | |
.then(json => { | |
const response = { | |
statusCode: 200, | |
headers: { 'Access-Control-Allow-Origin': '*' }, | |
body: JSON.stringify({ | |
message: `Function checkStockRecent(), ${symbol}, ${prettyEndDate} to ${start_date} (last ${last_days} days) executed successfully!`, | |
response: json | |
}) | |
}; | |
callback(null, response); | |
}); | |
} else { | |
const response = { | |
statusCode: 500, | |
headers: { 'Access-Control-Allow-Origin': '*' }, | |
body: JSON.stringify({ | |
message: 'Missing query parameter.', | |
queryStringParameters: event.queryStringParameters, | |
input: event | |
}) | |
}; | |
callback(null, response); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment