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

pubsub: Implementing subscription listing. #32

Merged
merged 2 commits into from
Jul 24, 2014
Merged
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
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