Skip to content

Commit

Permalink
Merge pull request #32 from rakyll/missing-listings
Browse files Browse the repository at this point in the history
pubsub: Implementing subscription listing.
  • Loading branch information
silvolu committed Jul 24, 2014
2 parents 5858099 + 546e08b commit 1f8ca9b
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,42 @@ function Connection(opts) {
});
}

// TODO(jbd): List subscriptions.
/**
* Lists subscriptions.
* @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;
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.
Expand Down Expand Up @@ -219,6 +254,16 @@ Connection.prototype.fullTopicName_ = function(name) {
});
};

/**
* Returns the fully qualified project name.
* Full name is in /projects/<projectId> 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 = {
Expand Down

0 comments on commit 1f8ca9b

Please sign in to comment.