From 7f9588d4482b8b0b8765edca2aa9c52a1d5ce1e8 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Wed, 23 Jul 2014 16:36:28 -0700 Subject: [PATCH 1/2] pubsub: Implementing subscription listing. --- lib/pubsub/index.js | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/pubsub/index.js b/lib/pubsub/index.js index bd7a3b62d90..eb05cbb7d11 100644 --- a/lib/pubsub/index.js +++ b/lib/pubsub/index.js @@ -154,7 +154,42 @@ function Connection(opts) { }); } -// TODO(jbd): List subscriptions. +/** + * Lists subscriptions. + * @param {string} query.filterByName Returns subscriptions that are + * subscribed to the topic provided. + * @param {string} query.pageToken Page token. + * @param {Number} query.maxResults Max number of results to return. + * @param {Function} callback Callback function. + */ +Connection.prototype.listSubscriptions = function(query, callback) { + var that = this; + if (arguments.length < 2) { + callback = query; + query = {}; + } + var q = util.extend({}, query); + q.query = q.filterByTopic + ? 'pubsub.googleapis.com/topic in (' + this.fullTopicName_(q.filterByTopic) + ')' + : 'cloud.googleapis.com/project in (' + this.fullProjectName_() + ')'; + delete q.filterByTopic; + + this.makeReq('GET', 'subscriptions', q, true, function(err, result) { + if (err) { + return callback(err); + } + var items = result.subscription || []; + var subscriptions = items.map(function(item) { + return new Subscription(that, item.name); + }); + var nextQuery = null; + if (result.nextPageToken) { + nextQuery = q; + nextQuery.pageToken = result.nextPageToken; + } + callback(null, subscriptions, nextQuery); + }); +}; /** * Subscribe with the provided options. @@ -219,6 +254,16 @@ Connection.prototype.fullTopicName_ = function(name) { }); }; +/** + * Returns the fully qualified project name. + * Full name is in /projects/ form. + */ +Connection.prototype.fullProjectName_ = function() { + return util.format('/projects/{projectId}', { + projectId: this.id + }); +}; + // TOOD(jbd): Don't duplicate, unify this with bucket.makeReq. Connection.prototype.makeReq = function(method, path, q, body, callback) { var reqOpts = { From 546e08b41bdeef34728eaa273108568494ea3989 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Thu, 24 Jul 2014 09:56:20 -0700 Subject: [PATCH 2/2] Fixing wrong parameter name on jsdoc. --- lib/pubsub/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pubsub/index.js b/lib/pubsub/index.js index eb05cbb7d11..30ef4715b63 100644 --- a/lib/pubsub/index.js +++ b/lib/pubsub/index.js @@ -156,11 +156,11 @@ function Connection(opts) { /** * Lists subscriptions. - * @param {string} query.filterByName Returns subscriptions that are - * subscribed to the topic provided. - * @param {string} query.pageToken Page token. - * @param {Number} query.maxResults Max number of results to return. - * @param {Function} callback Callback function. + * @param {string} query.filterByTopic Returns subscriptions that are + * subscribed to the topic provided. + * @param {string} query.pageToken Page token. + * @param {Number} query.maxResults Max number of results to return. + * @param {Function} callback Callback function. */ Connection.prototype.listSubscriptions = function(query, callback) { var that = this;