Skip to content

Instantly share code, notes, and snippets.

@picsoung
Last active February 4, 2016 14:07
Show Gist options
  • Save picsoung/271cfe669753fd6c38ba to your computer and use it in GitHub Desktop.
Save picsoung/271cfe669753fd6c38ba to your computer and use it in GitHub Desktop.
var threescale = require('3scale');
var Client = threescale.Client;
client = new Client("YOUR_PROVIDER_KEY");
var authenticate = function(user_key,callback){
client.authrep_with_user_key({"user_key": user_key, "usage": { "hits": 1 } }, function(resp){
if(resp.is_success()){
callback(null,resp.is_success());
}else{
callback("403, unauthorized");
}
});
};
exports.handler = function(event, context) {
if(event.user_key){
authenticate(event.user_key, function(err,res){
if(err){
context.fail(err);
}else{
context.succeed({"message":"hello world"});
}
});
}else{
context.fail("user_key missing")
}
}
var aws = require('aws-sdk');
var http = require('http');
var lambda = new aws.Lambda({
region: 'us-west-2'
});
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var backend = "http://api-sentiment.3scale.net/v1"
lambda.invoke({
FunctionName: '3scale_auth',
Payload: JSON.stringify(event, null, 2)
}, function(error, data) {
if (error) {
console.log('error',error)
context.done('error', error);
}
if(data.Payload){
var d = JSON.parse(data.Payload)
if(d.errorMessage){
context.fail(d.errorMessage)
}else{ //3scale auth passed
// Endpoint LOGIC
var url = backend + event.resourcePath
if(event.word){
url = url.replace("{word}", event.word);
console.log(url)
}else if (event.sentence){
url = url.replace("{sentence}", event.sentence);
}
http.get(url, function(res) { //call backend endpoint
var body = '';
res.on('data',function(chunk){
body += chunk;
});
res.on('end',function(){
context.succeed(JSON.parse(body))
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
context.fail(e)
})
}
}
});
};
var aws = require('aws-sdk');
var http = require('http');
var lambda = new aws.Lambda({
region: 'us-west-2'
});
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var backend = "YOUR_BACKEND_URL";
lambda.invoke({
FunctionName: '3scale_auth',
Payload: JSON.stringify(event, null, 2)
}, function(error, data) {
if (error) {
console.log('error',error)
context.done('error', error);
}
if(data.Payload){
var d = JSON.parse(data.Payload)
if(d.errorMessage){
context.fail(d.errorMessage)
}else{ //3scale auth passed
// Endpoint LOGIC
var url = backend + event.resourcePath
http.get(url, function(res) { //call backend endpoint
var body = '';
res.on('data',function(chunk){
body += chunk;
});
res.on('end',function(){
context.succeed(JSON.parse(body))
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
context.fail(e)
})
}
}
});
};
{
"user_key":"$input.params('user_key')",
"body" : "$input.json('$')",
"resourcePath":"$context.resourcePath"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment