Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: allow default method options for service objects #958

Merged
merged 1 commit into from
Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/common/service-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'use strict';

var exec = require('methmeth');
var extend = require('extend');
var is = require('is');

/**
Expand Down Expand Up @@ -49,6 +50,9 @@ var util = require('./util.js');
* name of a Storage bucket or Pub/Sub topic.
* @param {object=} config.methods - A map of each method name that should be
* inherited.
* @param {object} config.methods[].reqOpts - Default request options for this
* particular method. A common use case is when `setMetadata` requires a
* `PUT` method to override the default `PATCH`.
* @param {object} config.parent - The parent service instance. For example, an
* instance of Storage if the object is Bucket.
*/
Expand All @@ -61,6 +65,7 @@ function ServiceObject(config) {
this.parent = config.parent; // Parent class.
this.id = config.id; // Name or ID (e.g. dataset ID, bucket name, etc.)
this.createMethod = config.createMethod;
this.methods = config.methods || {};

if (config.methods) {
var allMethodNames = Object.keys(ServiceObject.prototype);
Expand Down Expand Up @@ -130,10 +135,12 @@ ServiceObject.prototype.create = function(options, callback) {
* @param {object} callback.apiResponse - The full API response.
*/
ServiceObject.prototype.delete = function(callback) {
var reqOpts = {
var methodConfig = this.methods.delete || {};

var reqOpts = extend({
method: 'DELETE',
uri: ''
};
}, methodConfig.reqOpts);

callback = callback || util.noop;

Expand Down Expand Up @@ -225,9 +232,11 @@ ServiceObject.prototype.get = function(config, callback) {
ServiceObject.prototype.getMetadata = function(callback) {
var self = this;

var reqOpts = {
var methodConfig = this.methods.getMetadata || {};

var reqOpts = extend({
uri: ''
};
}, methodConfig.reqOpts);

// The `request` method may have been overridden to hold any special behavior.
// Ensure we call the original `request` method.
Expand Down Expand Up @@ -257,11 +266,13 @@ ServiceObject.prototype.setMetadata = function(metadata, callback) {

callback = callback || util.noop;

var reqOpts = {
var methodConfig = this.methods.setMetadata || {};

var reqOpts = extend(true, {
method: 'PATCH',
uri: '',
json: metadata
};
}, methodConfig.reqOpts);

// The `request` method may have been overridden to hold any special behavior.
// Ensure we call the original `request` method.
Expand Down
91 changes: 91 additions & 0 deletions test/common/service-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ describe('ServiceObject', function() {
assert.strictEqual(serviceObject.createMethod, CONFIG.createMethod);
});

it('should localize the methods', function() {
var methods = {};

var config = extend({}, CONFIG, {
methods: methods
});

var serviceObject = new ServiceObject(config);

assert.strictEqual(serviceObject.methods, methods);
});

it('should default methods to an empty object', function() {
assert.deepEqual(serviceObject.methods, {});
});

it('should clear out methods that are not asked for', function() {
var config = extend({}, CONFIG, {
methods: {
Expand Down Expand Up @@ -207,6 +223,27 @@ describe('ServiceObject', function() {
serviceObject.delete(assert.ifError);
});

it('should extend the request options with defaults', function(done) {
var method = {
reqOpts: {
method: 'override',
qs: {
custom: true
}
}
};

ServiceObject.prototype.request = function(reqOpts_) {
assert.strictEqual(reqOpts_.method, method.reqOpts.method);
assert.deepEqual(reqOpts_.qs, method.reqOpts.qs);
done();
};

var serviceObject = new ServiceObject(CONFIG);
serviceObject.methods.delete = method;
serviceObject.delete();
});

it('should not require a callback', function() {
ServiceObject.prototype.request = function(reqOpts, callback) {
callback();
Expand Down Expand Up @@ -384,6 +421,27 @@ describe('ServiceObject', function() {
serviceObject.getMetadata();
});

it('should extend the request options with defaults', function(done) {
var method = {
reqOpts: {
method: 'override',
qs: {
custom: true
}
}
};

ServiceObject.prototype.request = function(reqOpts_) {
assert.strictEqual(reqOpts_.method, method.reqOpts.method);
assert.deepEqual(reqOpts_.qs, method.reqOpts.qs);
done();
};

var serviceObject = new ServiceObject(CONFIG);
serviceObject.methods.getMetadata = method;
serviceObject.getMetadata();
});

it('should execute callback with error & apiResponse', function(done) {
var error = new Error('Error.');
var apiResponse = {};
Expand Down Expand Up @@ -445,6 +503,39 @@ describe('ServiceObject', function() {
serviceObject.setMetadata(metadata);
});

it('should extend the request options with defaults', function(done) {
var metadataDefault = {
a: 'b'
};

var metadata = {
c: 'd'
};

var method = {
reqOpts: {
method: 'override',
qs: {
custom: true
},
json: metadataDefault
}
};

var expectedJson = extend(true, {}, metadataDefault, metadata);

ServiceObject.prototype.request = function(reqOpts_) {
assert.strictEqual(reqOpts_.method, method.reqOpts.method);
assert.deepEqual(reqOpts_.qs, method.reqOpts.qs);
assert.deepEqual(reqOpts_.json, expectedJson);
done();
};

var serviceObject = new ServiceObject(CONFIG);
serviceObject.methods.setMetadata = method;
serviceObject.setMetadata(metadata);
});

it('should execute callback with error & apiResponse', function(done) {
var error = new Error('Error.');
var apiResponse = {};
Expand Down
2 changes: 1 addition & 1 deletion test/common/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ util.makeAuthenticatedRequestFactory = function() {
}
};

describe('ServiceObject', function() {
describe('Service', function() {
var Service;
var service;

Expand Down