Last active
February 13, 2019 17:41
-
-
Save bdelacretaz/8022b8a2d9119eabdbcc11fcc15d911a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// OpenWhisk minimal integration Web function example | |
// Get a car's make from the Icelandic apis.is service | |
// See https://github.com/apache/incubator-openwhisk/blob/master/docs/webactions.md | |
// | |
// Test as follows, on an OpenWhisk setup: | |
// $ wsk -i action create car car.js --web true | |
// ok: created action car | |
// $ export URL=$(wsk -i action get car --url | grep http) | |
// # Known good prefixes: aa02*, aa03*, aa12* | |
// $ export prefix=aa12 | |
// $ for i in 1 2 3 4 5 6 7 8 9; do export carId=$prefix$i ; curl -k "$URL?carId=$carId" ; echo ; done | |
// According to https://apis.is/car the car having number AA121 is a FORD - LTD (Brúnn) | |
// According to https://apis.is/car the car having number AA122 is a LADA - 2105 (Hvítur) | |
// ... | |
// | |
var request = require('request') | |
function main (params) { | |
// Icelandic Web Services FTW! See http://docs.apis.is | |
var serviceUrl = 'https://apis.is/car' | |
var defaultCarNumber = 'aa120' | |
var options = { | |
url: serviceUrl, | |
qs: {number : params.carId ? params.carId : defaultCarNumber }, | |
json: true | |
} | |
return new Promise(function (resolve, reject) { | |
request(options, function (err, resp) { | |
if (err) { | |
console.log(err) | |
return resolve({ statusCode:500, body:err}) | |
} | |
if(resp.statusCode != 200) { | |
return resolve({ statusCode:resp.statusCode, body:resp.body.error}) | |
} | |
var car = resp.body.results[0]; | |
return resolve({ body: `According to ${serviceUrl} the car having number ${car.number} is a ${car.type}`}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment