Created
September 8, 2014 08:23
-
-
Save glynnbird/bdf3d9a788662481f560 to your computer and use it in GitHub Desktop.
BlueMix Session-Cache sample code
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
// parse BlueMix configuration from environment variables, if present | |
var services = process.env.VCAP_SERVICES, | |
url = require('url'), | |
http = require('http'), | |
credentials = null; | |
// load BlueMix credentials from environment | |
if(typeof services != 'undefined') { | |
services = JSON.parse(services); | |
console.log(services); | |
credentials = services['SessionCache-1.0'][0].credentials; | |
credentials.parsed = url.parse(credentials.restResource); | |
console.log(credentials); | |
} | |
var getHeaders = function() { | |
var retval = { | |
"Content-Type": "application/json", | |
"Authorization": 'Basic ' + new Buffer(credentials.username + ':' + credentials.password).toString('base64') | |
}; | |
return retval; | |
}; | |
var put = function(key, value, callback) { | |
if(credentials == null) { | |
return callback(true,null); | |
} | |
var options = { | |
hostname: credentials.parsed.hostname, | |
port: 80, | |
path: credentials.parsed.pathname+ "/" + credentials.gridName + "/" + encodeURIComponent(key), | |
method: 'POST', | |
headers: getHeaders(), | |
rejectUnauthorized: false, | |
agent: false | |
}; | |
console.log("POST REQUEST",options); | |
var req = http.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log("POST reply",chunk); | |
}); | |
res.on('error',function(c) { | |
console.log('post error: '+c); | |
}); | |
res.on('end',function() { | |
console.log('post status '+res.statusCode); | |
console.log(res); | |
callback(null,null); | |
}); | |
}); | |
req.write(JSON.stringify(value)); | |
req.end(); | |
}; | |
var get = function(key, callback) { | |
if(credentials == null) { | |
return callback(true,null); | |
} | |
var options = { | |
hostname: credentials.parsed.hostname, | |
port: 80, | |
path: credentials.parsed.pathname+ "/" + credentials.gridName + "/" + encodeURIComponent(key), | |
method: 'GET', | |
rejectUnauthorized: false, | |
agent: false | |
}; | |
console.log("GET REQUEST",options); | |
var req = http.request(options, function(res) { | |
var result = ""; | |
res.on('data', function (chunk) { | |
result += chunk; | |
}); | |
res.on('error',function(c) { | |
console.log('get error: '+c); | |
}); | |
res.on('end',function() { | |
if (res.statusCode == 200) { | |
console.log("STATUS CODE 200!",result) | |
callback(null, JSON.parse(result)); | |
} | |
else { | |
callback(true, null); //error case | |
} | |
}); | |
}); | |
}; | |
put("test",{a:1,b:"bob",c:true}, function(err,data) { | |
console.log(err,data); | |
get("test", function(err, data) { | |
console.log(err,data); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment