Skip to content

Commit

Permalink
docs: update docs and samples for drive export (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jul 20, 2019
1 parent 6947979 commit aaea13c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ async function main() {
main().catch(console.error);
```

You can also override *gaxios* options per request, such as `url`, `method`, and `encoding`.
You can also override *gaxios* options per request, such as `url`, `method`, and `responseType`.

For example:

Expand All @@ -472,7 +472,7 @@ const res = await drive.files.export({
mimeType: 'application/pdf'
}, {
// Make sure we get the binary data
encoding: null
responseType: 'stream'
});
```

Expand Down
21 changes: 11 additions & 10 deletions samples/drive/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {google} = require('googleapis');
const sampleClient = require('../sampleclient');
const fs = require('fs');
const os = require('os');
const path = require('path');

const drive = google.drive({
version: 'v3',
Expand All @@ -25,17 +26,17 @@ const drive = google.drive({

async function runSample() {
// [START main_body]
return new Promise(async (resolve, reject) => {
const fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
const dest = fs.createWriteStream(`${os.tmpdir()}/resume.pdf`);

const res = await drive.files.export(
{fileId, mimeType: 'application/pdf'},
{responseType: 'stream'}
);
const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
const destPath = path.join(os.tmpdir(), 'important.pdf');
const dest = fs.createWriteStream(destPath);
const res = await drive.files.export(
{fileId, mimeType: 'application/pdf'},
{responseType: 'stream'}
);
await new Promise((resolve, reject) => {
res.data
.on('end', () => {
console.log('Done downloading document.');
console.log(`Done downloading document: ${destPath}.`);
resolve();
})
.on('error', err => {
Expand All @@ -49,7 +50,7 @@ async function runSample() {

// if invoked directly (not tests), authenticate and run the samples
if (module === require.main) {
const scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const scopes = ['https://www.googleapis.com/auth/drive.readonly'];
sampleClient
.authenticate(scopes)
.then(runSample)
Expand Down
4 changes: 2 additions & 2 deletions test/samples/test.samples.drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ describe('Drive samples', () => {
});

it('should download the doc', async () => {
const fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
const scope = nock(Utils.baseUrl)
.get(`/drive/v3/files/${fileId}/export?mimeType=application%2Fpdf`)
.replyWithFile(200, someFile);
await samples.export.runSample();
assert(fs.existsSync(`${os.tmpdir()}/resume.pdf`));
assert(fs.existsSync(`${os.tmpdir()}/important.pdf`));
scope.done();
});

Expand Down

0 comments on commit aaea13c

Please sign in to comment.