Request an Uber
var https = require('https'); | |
// For information about Uber http status codes see: https://developer.uber.com/docs/api-reference | |
const successfully_requested_uber_status_code = 202; | |
const start_latitude = 37.7749; // Lat SF | |
const start_longitude = -122.4194; // Long SF | |
// For information about Uber tokens see: https://developer.uber.com/docs/authentication | |
const token = process.env.TOKEN; | |
function requestUber(event, context, product_id) { | |
var data = { | |
'product_id': product_id, | |
'start_latitude': start_latitude, | |
'start_longitude': start_longitude | |
}; | |
data = JSON.stringify(data); | |
var headers = { | |
'Authorization': 'Bearer ' + token, | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(data) | |
}; | |
var options = { | |
'host': 'api.uber.com', | |
'path': '/v1/requests', | |
'method': 'POST', | |
'headers': headers | |
}; | |
var req = https.request(options, function(res) { | |
var body = ''; | |
res.on('data', function (chunk) { | |
body += chunk; | |
}); | |
res.on('end', function () { | |
var parsed = JSON.parse(body); | |
// Successfully requested Uber | |
if (res.statusCode === successfully_requested_uber_status_code) { | |
console.log('Uber requested. Status code: ' + res.statusCode); | |
context.done(); | |
} | |
else { | |
console.log('Uber request failed. Status code: ' + res.statusCode); | |
context.fail(event); | |
} | |
}); | |
}); | |
req.write(data); | |
req.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment