-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted getRepos from index.js to codeplex.js.
Setup a basic server in index.js for testing this.
- Loading branch information
abe545
committed
Mar 12, 2014
1 parent
a9c7cfc
commit 4609c7a
Showing
2 changed files
with
68 additions
and
33 deletions.
There are no files selected for viewing
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
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(); | ||
} |
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
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); |