-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import nock from 'nock'; | ||
import { FileClient } from '../lib'; | ||
import { BASE_URL } from './common'; | ||
import { Auth } from '@vonage/auth'; | ||
import mockFs from 'mock-fs'; | ||
import { readFileSync, existsSync } from 'fs'; | ||
|
||
const FILE_PATH = '/path'; | ||
|
||
const key = readFileSync(`${__dirname}/private.test.key`).toString(); | ||
|
||
describe('File tests', () => { | ||
let client; | ||
let scope; | ||
|
||
beforeEach(function () { | ||
mockFs({ | ||
[FILE_PATH]: {}, | ||
}); | ||
|
||
client = new FileClient( | ||
new Auth({ | ||
privateKey: key, | ||
applicationId: 'my-application', | ||
}), | ||
); | ||
|
||
scope = nock(BASE_URL, { | ||
reqheaders: { | ||
authorization: (value) => value.startsWith('Bearer '), | ||
}, | ||
}).persist(); | ||
}); | ||
|
||
afterEach(function () { | ||
client = null; | ||
scope = null; | ||
nock.cleanAll(); | ||
mockFs.restore(); | ||
}); | ||
|
||
test('Can download file with url', async () => { | ||
const content = "Ford, I think I'm a couch"; | ||
const file = `${FILE_PATH}/my-file.txt`; | ||
scope | ||
.get(`/v1/files/00000000-0000-0000-0000-000000000001`) | ||
.reply(200, content); | ||
|
||
expect(existsSync(file)).toBeFalsy(); | ||
|
||
expect( | ||
await client.downloadFile( | ||
`https://api.nexmo.com/v1/files/00000000-0000-0000-0000-000000000001`, | ||
file, | ||
), | ||
).toBeUndefined(); | ||
|
||
expect(existsSync(file)).toBeTruthy(); | ||
expect(readFileSync(file).toString()).toEqual(content); | ||
expect(nock.isDone()).toBeTruthy(); | ||
}); | ||
|
||
test('Can download file with id', async () => { | ||
const content = "Ford, I think I'm a couch"; | ||
const file = `${FILE_PATH}/my-file.txt`; | ||
scope | ||
.get(`/v1/files/00000000-0000-0000-0000-000000000001`) | ||
.reply(200, content); | ||
|
||
expect(existsSync(file)).toBeFalsy(); | ||
|
||
expect( | ||
await client.downloadFile('00000000-0000-0000-0000-000000000001', file), | ||
).toBeUndefined(); | ||
|
||
expect(existsSync(file)).toBeTruthy(); | ||
expect(readFileSync(file).toString()).toEqual(content); | ||
expect(nock.isDone()).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Client } from './client'; | ||
import { AuthenticationType } from './enums/AuthenticationType'; | ||
import { writeFileSync } from 'fs'; | ||
import debug from 'debug'; | ||
|
||
const log = debug('vonage:server-client'); | ||
|
||
export class FileClient extends Client { | ||
protected authType = AuthenticationType.JWT; | ||
|
||
async downloadFile(file: string, path: string): Promise<void> { | ||
log(`Downloading file: ${file}`); | ||
let fileId = file; | ||
try { | ||
const fileURL = new URL(file); | ||
fileId = fileURL.pathname.split('/').pop(); | ||
} catch (_) { | ||
log(`Not a url`); | ||
} | ||
|
||
log(`File Id ${fileId}`); | ||
const resp = await this.sendGetRequest<string>( | ||
`${this.config.apiHost}/v1/files/${fileId}`, | ||
); | ||
|
||
log(`Saving to ${path}`); | ||
writeFileSync(path, resp.data); | ||
log('File saved'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters