Skip to content

Commit

Permalink
Extracted getRepos from index.js to codeplex.js.
Browse files Browse the repository at this point in the history
Setup a basic server in index.js for testing this.
  • Loading branch information
abe545 committed Mar 12, 2014
1 parent a9c7cfc commit 4609c7a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 33 deletions.
35 changes: 35 additions & 0 deletions lib/codeplex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var https = require('https');

module.exports = {
getRepos: getRepos
}

function getRepos(user, callback) {

var options = {
hostname: 'www.codeplex.com',
path: '/api/users/' + user + '/projects',
method: 'GET',
headers: {'x-ms-version': '2012-09-01'}
};

console.log("Fetching repos from codeplex for user: %s", user);
var req = https.request(options, function(res) {
var json = '';
res.on('data', function(d) {
json += d;
});
res.on('error', function(err) {
callback(err, null);
});
res.on('end', function() {
var repos = JSON.parse(json);
callback(null, repos);
});
});

req.on('error', function(err) {
callback(err, null);
});
req.end();
}
66 changes: 33 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
var https = require('https');
var codeplex = require('./codeplex');
var http = require('http');

var url = require('url');
var querystring = require('querystring');

http.createServer(function(request, response) {
console.log("Request handler 'codeplex' was called.");

var options = {
hostname: 'www.codeplex.com',
path: '/api/users/aheidebrecht/projects',
method: 'GET'
};

var req = https.request(options, function(res) {
var json = '';
res.on('data', function(d) {
json += d;
});

res.on('end', function() {
var str = JSON.stringify(JSON.parse(json), null, " ");
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write(str);
response.end();
});
});

req.end();

req.on('error', function(err) {
console.error(err);

response.writeHead(500, { 'Content-Type': 'text/plain' });
response.write(err);
response.end();
});

var parsed = url.parse(request.url);
var pathname = parsed.pathname;
if (pathname === '/') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('<html><head><title>codeplex api test</title></head>'+
'<body><form action="get-repos"><input type="text" name="username" /><input type="submit" /></form></body></html>');
response.end();
} else if (pathname === '/get-repos') {
var username = querystring.parse(parsed.query)["username"];

codeplex.getRepos(username, function(err, data) {
if (err) {
console.error(err);

response.writeHead(500, { 'Content-Type': 'text/plain' });
response.write(err);
response.end();
} else {
var str = JSON.stringify(data, null, " ");
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write(str);
response.end();
}
});
} else {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write(pathname);
response.end();
}
}).listen(999);

0 comments on commit 4609c7a

Please sign in to comment.