Skip to content

Commit

Permalink
Merge pull request #86 from proppy/fix-defaultparams
Browse files Browse the repository at this point in the history
Fix defaultparams
  • Loading branch information
Burcu Dogan committed Sep 4, 2013
2 parents 642b1ab + 3ede4c2 commit bb18e92
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
9 changes: 3 additions & 6 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

var Request = require('./requests.js').Request;
var BatchRequest = require('./requests.js').BatchRequest;
var utils = require('./utils.js');

/**
* Constructs a new client with given API name and version.
Expand Down Expand Up @@ -91,11 +90,9 @@ Client.prototype.generateHelper_ = function(methodMeta) {
* @return {Request} New Request object constructed with given args.
*/
Client.prototype.newRequest = function(methodMetada, params, opt_resource) {
if (this.defaultParams) {
params = utils.extend(utils.extend({}, this.defaultParams), params);
}
return new Request(this.apiMeta, methodMetada, params, opt_resource)
.withAuthClient(this.authClient);

return new Request(this.apiMeta, methodMetada, params, opt_resource,
this.defaultParams).withAuthClient(this.authClient);
};

/**
Expand Down
8 changes: 7 additions & 1 deletion lib/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

var BatchParser = require('./exp/batchparser'),
DefaultTransporter = require('./transporters.js'),
utils = require('./utils.js'),
querystring = require('querystring'),
url = require('url'),
util = require('util');


function BaseRequest(apiMeta) {
this.transporter = new DefaultTransporter();
this.authClient = null;
Expand Down Expand Up @@ -127,7 +129,7 @@ BaseRequest.prototype.handleResponse = function(opt_fn) {
* @param {object=} opt_resource Optional resource.
*/
function Request(
apiMeta, methodMeta, opt_params, opt_body) {
apiMeta, methodMeta, opt_params, opt_body, opt_defaultParams) {
Request.super_.call(this);
this.apiMeta = apiMeta;
this.methodMeta = methodMeta;
Expand All @@ -140,6 +142,10 @@ function Request(
this.params = opt_params || {};
this.body = opt_body;
}
if (opt_defaultParams) {
this.params = utils.extend(utils.extend({}, opt_defaultParams),
this.params);
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/test.clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ describe('Clients', function() {
});
});

it('should be able to add defaultParams on new requests with no params and a body', function(done) {
new googleapis.GoogleApis()
.discover('plus', 'v1')
.execute(function(err, client) {
var req =
client.plus.withDefaultParams({a: 1, b: 'foo'}).newRequest('doIt', {has_body: true});
assert.equal(1, req.params.a);
assert.equal('foo', req.params.b);
assert.equal(undefined, req.params.body);
assert.equal(true, req.body.has_body);
done();
});
});
});

0 comments on commit bb18e92

Please sign in to comment.