Last active
October 11, 2016 09:18
-
-
Save mynamebrody/ab99c5f53467b9b481c1 to your computer and use it in GitHub Desktop.
The AWS Lambda Function to order alcohol via Drizly
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
var https = require('https'); | |
var CONFIG = { | |
base_url: 'sandbox.drizly.com', | |
partner_token: 'API_TOKEN', | |
token: 'USER_TOKEN', | |
delivery_address_id: 654321, | |
saved_credit_card_id: 123456, | |
latitude: 40.0155, | |
longitude: -105.284 | |
}, | |
ITEMS = {}; | |
function orderDrizly(event, context) { | |
var clickType = event.clickType; | |
switch(clickType.toLowerCase()) { | |
case "single": { | |
ITEMS = { | |
id_1: 287212, | |
quantity_1: 1, | |
id_2: 248290, | |
quantity_2: 1 | |
}; | |
break; | |
} | |
case "double": { | |
ITEMS = { | |
id_1: 287212, | |
quantity_1: 2, | |
id_2: 248290, | |
quantity_2: 2 | |
}; | |
break; | |
} | |
case "long": { | |
ITEMS = { | |
id_1: 241416, | |
quantity_1: 1, | |
id_2: 244950, | |
quantity_2: 1 | |
}; | |
break; | |
} | |
} | |
var data = { | |
partner_token: CONFIG.partner_token, | |
token: CONFIG.token, | |
delivery_address_id: CONFIG.delivery_address_id, | |
saved_credit_card_id: CONFIG.saved_credit_card_id, | |
delivery_location: { | |
latitude: CONFIG.latitude, | |
longitude: CONFIG.longitude | |
}, | |
items: {}, | |
order_comment: 'This was ordered via Amazon AWS IoT Button' | |
}; | |
data.items[ITEMS.id_1] = ITEMS.quantity_1; | |
data.items[ITEMS.id_2] = ITEMS.quantity_2; | |
data = JSON.stringify(data); | |
var headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(data) | |
}; | |
var options = { | |
host: CONFIG.base_url, | |
path: '/api/v3/checkout/process', | |
method: 'POST', | |
headers: headers | |
}; | |
var req = https.request(options, function(res) { | |
console.log(res.statusCode); | |
if (res.statusCode == 200 || res.statusCode == 201) { | |
context.succeed(event); | |
} else if (context) { | |
context.fail(event); | |
} | |
res.on('data', function (chunk) { | |
console.log("" + chunk); | |
}); | |
}); | |
req.write(data); | |
req.end(); | |
} | |
exports.handler = orderDrizly; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment