Browse files

Refactoring the github client. Adding basic auth.

  • Loading branch information...
1 parent ac6da79 commit 487d94d9d2fa7e10c0458ccb07b04449ab3826d2 @vesln committed Jan 10, 2012
Showing with 32 additions and 7 deletions.
  1. +32 −7 lib/github.js
View
39 lib/github.js
@@ -10,6 +10,7 @@
*/
var request = require('request');
var async = require('async');
+var conf = require('./conf');
/**
* GitHub API.
@@ -24,9 +25,9 @@ var github = module.exports;
* @type {Object}
*/
github.urls = {
- main: 'https://api.github.com',
- repos: '/users/:user/repos',
- issues: '/repos/:user/:repo/issues'
+ main: 'https://github.com/api/v2/json',
+ repos: '/repos/show/:user',
+ issues: '/issues/list/:user/:repo/open'
};
/**
@@ -39,9 +40,10 @@ github.repos = function(username, cb) {
var url = github.url('repos', { user: username });
var repos = [];
- request({ url: url, json: true }, function(err, response, body) {
+ github.request(url, function(err, response, body) {
body || (body = []);
- body.forEach(function(repo) {
+
+ body.repositories.forEach(function(repo) {
repos.push(repo.name);
});
@@ -61,13 +63,13 @@ github.issues = function(username, repos, cb) {
var issue = function(repo, done) {
var url = github.url('issues', { user: username, repo: repo });
- request({ url: url, json: true }, function(err, response, body) {
+ github.request(url, function(err, response, body) {
switch (response.statusCode) {
case 404: return done(new Error('Invalid username or repository name.'));
case 200: break;
default: body = []
}
- results[repo] = body;
+ results[repo] = body.issues;
done(err);
});
};
@@ -78,6 +80,29 @@ github.issues = function(username, repos, cb) {
};
/**
+ * Requests data from GitHub.
+ *
+ * @param {String} url
+ * @param {Function} callback
+ */
+github.request = function(url, cb) {
+ var options = {
+ url: url,
+ headers: {},
+ json: true
+ };
+ var token = conf.get('token');
+ var username = conf.get('username');;
+ var auth = null;
+
+ if (token && username) {
+ auth = new Buffer(username + '/token:' + token, 'ascii')
+ options.headers.Authorization = 'Basic ' + auth.toString('base64');
+ }
+ request(options, cb);
+};
+
+/**
* Returns a url address.
*
* @type {String} name

0 comments on commit 487d94d

Please sign in to comment.