-
-
Save ecmadao/0a87a306d8d4b647bf3063468066bc7d to your computer and use it in GitHub Desktop.
simple basic-auth node github api
This file contains hidden or 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
| // Simple Use: | |
| // ---------- | |
| // github('/user/repos', callback); | |
| // github('/repos/:user/:repo/issues', { user: user, repo: repo }, callback); | |
| var request = require('request'); | |
| var querystring = require('querystring'); | |
| var github = function (path, options, callback) { | |
| var username = 'username'; | |
| var password = '********'; | |
| var tokens = path.match(/:[^\/]+/g); | |
| var method = options.method || 'get'; | |
| if (typeof options == 'function') { | |
| callback = options; | |
| options = {}; | |
| } | |
| delete options.method; | |
| tokens && tokens.forEach(function (token) { | |
| var key = token.substr(1); | |
| path = path.replace(token, options[key]); | |
| delete options[key]; | |
| }); | |
| if (Object.keys(options).length) path += '?' + querystring.stringify(options); | |
| request({ | |
| url: 'https://api.github.com' + path, | |
| json:true, | |
| headers: { | |
| 'Host': 'api.github.com', | |
| 'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64') | |
| } | |
| }, function (err, response, body) { callback(err, body); }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment