Skip to content

Instantly share code, notes, and snippets.

@gsf
Created November 3, 2011 15:11
Show Gist options
  • Save gsf/1336733 to your computer and use it in GitHub Desktop.
Save gsf/1336733 to your computer and use it in GitHub Desktop.
Node.js Summon portal
var crypto = require('crypto');
var http = require('http');
var querystring = require('querystring');
var url = require('url');
var util = require('util');
var accessID = 'xxxxx';
var key = 'xxxxxxxxxx';
var host = 'api.summon.serialssolutions.com';
var summon = http.createClient(80, host);
summon.on('error', function(e) {
throw new Error('Client error: '+e);
});
http.createServer(function(request, response) {
util.log(request.method+' http://'+request.headers.host+request.url);
var urlParts = url.parse(request.url);
var accept = 'application/xml';
var date = (new Date()).toGMTString();
var path = '/search';
var query;
var unQuery;
if (urlParts.query) {
var queryParts = urlParts.query.split('&');
var params = [];
for (var i=0; i<queryParts.length; i++) {
var param = queryParts[i];
if (param && (param.indexOf('=') != -1)) {
param = param.replace('+', '%20');
params.push(param);
}
}
params.sort();
query = params.join('&');
unQuery = querystring.unescape(query);
} else {
query = unQuery = '';
}
var idString = accept+'\n'+date+'\n'+host+'\n'+path+'\n'+unQuery+'\n';
var hmac = crypto.createHmac('sha1', key);
var hash = hmac.update(idString);
var digest = hash.digest('base64');
var summonRequest = summon.request('GET', path+'?'+query, {
host: host,
accept: accept,
'x-summon-date': date,
'x-summon-session-id': 'summon api test',
authorization: 'Summon '+accessID+';'+digest
});
summonRequest.end();
summonRequest.on('response', function(summonResponse) {
if (summonResponse.statusCode != 200) {
response.writeHead(summonResponse.statusCode,
{'Content-Type': 'text/html'});
} else {
response.writeHead(200, {'Content-Type': 'application/xml'});
}
summonResponse.setEncoding('utf8');
summonResponse.on('data', function(chunk) {
response.write(chunk);
});
summonResponse.on('end', function() {
response.end();
});
});
}).listen(8124);
util.log('Listening at http://127.0.0.1:8124/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment