From 1917e9f50450d732cf00dba5c6db80df40aa684c Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 20 Mar 2018 07:57:27 -0700 Subject: [PATCH] feat: add gmail watch sample (#1073) --- samples/gmail/watch.js | 67 ++++++++++++++++++++++++++++++ test/samples/test.samples.gmail.ts | 13 +++++- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 samples/gmail/watch.js diff --git a/samples/gmail/watch.js b/samples/gmail/watch.js new file mode 100644 index 00000000000..e344aa2458c --- /dev/null +++ b/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 +}; diff --git a/test/samples/test.samples.gmail.ts b/test/samples/test.samples.gmail.ts index af244b14007..c70fedfefb7 100644 --- a/test/samples/test.samples.gmail.ts +++ b/test/samples/test.samples.gmail.ts @@ -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) { @@ -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(); + }); + }); });