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

chore: add gmail samples #1046

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions samples/gmail/labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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
});

function runSample (action, messageId, labelId, callback) {
if (action === 'add') {
gmail.users.messages.modify({
userId: 'me',
id: messageId,
resource: {
'addLabelIds': [labelId]
}
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
} else if (action === 'remove') {
gmail.users.messages.modify({
userId: 'me',
id: messageId,
resource: {
'removeLabelIds': [labelId]
}
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
}
}

const scopes = ['https://www.googleapis.com/auth/gmail.modify'];

if (module === require.main) {
sampleClient.authenticate(scopes, err => {
if (err) {
throw err;
}
if (process.argv.length !== 5) {
showUsage();
}
const [action, messageId, labelId] = process.argv.slice(2);
console.log(`action: ${action}`);
console.log(`messageId: ${messageId}`);
console.log(`labelId: ${labelId}`);
runSample(action, messageId, labelId, () => { /* add complete */ });
});
}

function showUsage () {
console.info('USAGE: node labels.js <add|remove> <messageId> <labelId>');
process.exit();
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
};
50 changes: 50 additions & 0 deletions samples/gmail/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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
});

function runSample (callback) {
gmail.users.messages.list({
userId: 'me'
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
}

const scopes = ['https://www.googleapis.com/auth/gmail.readonly'];

if (module === require.main) {
sampleClient.authenticate(scopes, err => {
if (err) {
throw err;
}
runSample(() => { /* sample complete */ });
});
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
};
64 changes: 64 additions & 0 deletions test/samples/test.samples.gmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.

import * as assert from 'assert';
import * as fs from 'fs';
import * as nock from 'nock';
import * as os from 'os';
import * as path from 'path';

import {Utils} from './../utils';

nock.disableNetConnect();

const samples = {
list: require('../../../samples/gmail/list'),
labels: require('../../../samples/gmail/labels')
};

for (const p in samples) {
if (samples[p]) {
samples[p].client.credentials = {access_token: 'not-a-token'};
}
}

describe('gmail samples', () => {
afterEach(() => {
nock.cleanAll();
});

it('should list emails', done => {
const scope =
nock(Utils.baseUrl).get(`/gmail/v1/users/me/messages`).reply(200, {
yes: true
});
samples.list.runSample(data => {
assert(data);
scope.done();
done();
});
});

it('should add a label', done => {
const messageId = '12345';
const labelId = 'abcde';
const scope = nock(Utils.baseUrl)
.post(`/gmail/v1/users/me/messages/${messageId}/modify`)
.reply(200, {yes: true});
samples.labels.runSample('add', messageId, labelId, data => {
assert(data);
scope.done();
done();
});
});
});