Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #84 from joshrp/add_proxy

Added ability to specify a proxy server for requests.
  • Loading branch information...
commit f609039436271d702dfcd05777ebb831463785b1 2 parents 082feb1 + c8da77c
@pksunkara authored
View
18 README.md 100644 → 100755
@@ -72,6 +72,22 @@ client.get('/user', {}, function (err, status, body, headers) {
});
```
+
+## Request Options
+
+Request options can be set by setting defaults on the client. (e.g. Proxies)
+
+```js
+var client = github.client();
+
+client.requestDefaults['proxy'] = 'https://myproxy.com:1085'
+```
+These options are passed though to `request`, see their API here: https://github.com/mikeal/request#requestoptions-callback
+
+### Proxies
+You can set proxies dynamically by using the example above, but Octonode will respect environment proxies by default. Just set this using:
+`export HTTP_PROXY='https://myproxy.com:1085'` if you are using the command line
+
__Many of the below use cases use parts of the above code__
## Authentication
@@ -298,7 +314,7 @@ ghme.keys(1);
This query supports [pagination](#pagination).
```js
-ghme.starred(callback); //array of repos
+ghme.starred(callback); //array of repos
```
#### Check if you have starred a repository (GET /user/starred/pksunkara/octonode)
View
58 lib/octonode/client.js
@@ -1,14 +1,14 @@
// Generated by CoffeeScript 1.6.3
(function() {
- var Client, Gist, HttpError, Issue, Me, Org, Pr, Repo, Search, Team, User, request, url,
+ var Client, Gist, HttpError, Issue, Me, Org, Pr, Repo, Search, Team, User, extend, request, url,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__slice = [].slice;
- this.request = request = require('request');
+ request = require('request');
- this.url = url = require('url');
+ url = require('url');
Me = require('./me');
@@ -26,6 +26,8 @@
Issue = require('./issue');
+ extend = require('util-extend');
+
Search = require('./search');
HttpError = (function(_super) {
@@ -46,6 +48,13 @@
this.token = token;
this.options = options;
this.limit = __bind(this.limit, this);
+ this.requestOptions = __bind(this.requestOptions, this);
+ this.requestDefaults = {
+ headers: {
+ 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
+ },
+ proxy: process.env.HTTP_PROXY
+ };
}
Client.prototype.me = function() {
@@ -84,6 +93,10 @@
return new Issue(repo, number, this);
};
+ Client.prototype.requestOptions = function(params) {
+ return extend(this.requestDefaults, params);
+ };
+
Client.prototype.buildUrl = function(path, pageOrQuery, per_page) {
var query;
if (path == null) {
@@ -145,13 +158,10 @@
var callback, params, path, _i,
_this = this;
path = arguments[0], params = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), callback = arguments[_i++];
- return request({
+ return request(this.requestOptions({
uri: this.buildUrl.apply(this, [path].concat(__slice.call(params))),
- method: 'GET',
- headers: {
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- }
- }, function(err, res, body) {
+ method: 'GET'
+ }), function(err, res, body) {
if (err) {
return callback(err);
}
@@ -163,14 +173,11 @@
var callback, params, path, _i,
_this = this;
path = arguments[0], params = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), callback = arguments[_i++];
- return request({
+ return request(this.requestOptions({
uri: this.buildUrl.apply(this, [path].concat(__slice.call(params))),
method: 'GET',
- followRedirect: false,
- headers: {
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- }
- }, function(err, res, body) {
+ followRedirect: false
+ }), function(err, res, body) {
if (err) {
return callback(err);
}
@@ -180,15 +187,14 @@
Client.prototype.post = function(path, content, callback) {
var _this = this;
- return request({
+ return request(this.requestOptions({
uri: this.buildUrl(path),
method: 'POST',
body: JSON.stringify(content),
headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
+ 'Content-Type': 'application/json'
}
- }, function(err, res, body) {
+ }), function(err, res, body) {
if (err) {
return callback(err);
}
@@ -198,15 +204,14 @@
Client.prototype.put = function(path, content, callback) {
var _this = this;
- return request({
+ return request(this.requestOptions({
uri: this.buildUrl(path),
method: 'PUT',
body: JSON.stringify(content),
headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
+ 'Content-Type': 'application/json'
}
- }, function(err, res, body) {
+ }), function(err, res, body) {
if (err) {
return callback(err);
}
@@ -216,15 +221,14 @@
Client.prototype.del = function(path, content, callback) {
var _this = this;
- return request({
+ return request(this.requestOptions({
uri: this.buildUrl(path),
method: 'DELETE',
body: JSON.stringify(content),
headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
+ 'Content-Type': 'application/json'
}
- }, function(err, res, body) {
+ }), function(err, res, body) {
if (err) {
return callback(err);
}
View
3  package.json
@@ -34,7 +34,8 @@
],
"dependencies": {
"request": "2.27.x",
- "randomstring": "1.x.x"
+ "randomstring": "1.x.x",
+ "util-extend": "1.x.x"
},
"devDependencies": {
"nock": "0.7.x",
View
39 src/octonode/client.coffee
@@ -5,8 +5,8 @@
#
# Requiring modules
-@request = request = require 'request'
-@url = url = require 'url'
+request = require 'request'
+url = require 'url'
Me = require './me'
User = require './user'
@@ -16,6 +16,7 @@ Gist = require './gist'
Team = require './team'
Pr = require './pr'
Issue = require './issue'
+extend = require 'util-extend'
Search = require './search'
@@ -27,6 +28,10 @@ class HttpError extends Error
class Client
constructor: (@token, @options) ->
+ @requestDefaults =
+ headers:
+ 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
+ proxy: process.env.HTTP_PROXY
# Get authenticated user instance for client
me: ->
@@ -63,6 +68,9 @@ class Client
issue: (repo, number) ->
new Issue repo, number, @
+ requestOptions: (params) =>
+ return extend @requestDefaults, params
+
# Github api URL builder
buildUrl: (path = '/', pageOrQuery = null, per_page = null) ->
if pageOrQuery? and typeof pageOrQuery == 'object'
@@ -97,63 +105,56 @@ class Client
# Github api GET request
get: (path, params..., callback) ->
- request
+ request @requestOptions(
uri: @buildUrl path, params...
method: 'GET'
- headers:
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- , (err, res, body) =>
+ ), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api GET request
getNoFollow: (path, params..., callback) ->
- request
+ request @requestOptions(
uri: @buildUrl path, params...
method: 'GET'
followRedirect: false
- headers:
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- , (err, res, body) =>
+ ), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api POST request
post: (path, content, callback) ->
- request
+ request @requestOptions(
uri: @buildUrl path
method: 'POST'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- , (err, res, body) =>
+ ), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api PUT request
put: (path, content, callback) ->
- request
+ request @requestOptions(
uri: @buildUrl path
method: 'PUT'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- , (err, res, body) =>
+ ), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api DELETE request
del: (path, content, callback) ->
- request
+ request @requestOptions(
uri: @buildUrl path
method: 'DELETE'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
- 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
- , (err, res, body) =>
+ ), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
Please sign in to comment.
Something went wrong with that request. Please try again.