Skip to content

Commit

Permalink
feat: add gmail watch sample (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 20, 2018
1 parent 57016c3 commit 1917e9f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
67 changes: 67 additions & 0 deletions samples/gmail/watch.js
@@ -0,0 +1,67 @@
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const { google } = require('googleapis');
const sampleClient = require('../sampleclient');

const gmail = google.gmail({
version: 'v1',
auth: sampleClient.oAuth2Client
});

/**
* NOTE: Before using this API, you need to do a few things.
* 1. Create a new Pub/Sub topic. You can use the command:
* gcloud pubsub topics create gmail
* 2. Go to the Cloud Developer Console, and give the gmail
* service account gmail-api-push@system.gserviceaccount.com
* Pub/Sub Publisher rights to your newly created topic.
* https://console.cloud.google.com/cloudpubsub/topicList?project=${PROJECT_NAME}
*/
function runSample (callback) {
gmail.users.watch({
userId: 'me',
resource: {
// Replace with `projects/${PROJECT_ID}/topics/${TOPIC_NAME}`
topicName: `projects/el-gato/topics/gmail`
}
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
}

if (module === require.main) {
const scopes = [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.metadata',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.readonly'
];
sampleClient.authenticate(scopes, err => {
if (err) {
throw err;
}
runSample(() => { /* add complete */ });
});
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
};
13 changes: 12 additions & 1 deletion test/samples/test.samples.gmail.ts
Expand Up @@ -24,7 +24,8 @@ nock.disableNetConnect();
// tslint:disable: no-any
const samples: any = {
list: require('../../../samples/gmail/list'),
labels: require('../../../samples/gmail/labels')
labels: require('../../../samples/gmail/labels'),
watch: require('../../../samples/gmail/watch')
};

for (const p in samples) {
Expand Down Expand Up @@ -62,4 +63,14 @@ describe('gmail samples', () => {
done();
});
});

it('should add a user watch', done => {
const scope =
nock(Utils.baseUrl).post(`/gmail/v1/users/me/watch`).reply(200, {});
samples.watch.runSample((data: {}) => {
assert(data);
scope.done();
done();
});
});
});

0 comments on commit 1917e9f

Please sign in to comment.