-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add method for downloading and storing files
- Loading branch information
Showing
8 changed files
with
218 additions
and
11 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,81 @@ | ||
"use strict"; | ||
|
||
var fs = require('fs'); | ||
|
||
class FilesResource { | ||
|
||
/** | ||
* The path to the `calls` resource. | ||
*/ | ||
static get PATH() { | ||
return '/v1/files'; | ||
} | ||
|
||
/** | ||
* Creates a new FilesResource. | ||
* | ||
* @param {Credentials} creds - Credentials used when interacting with the Nexmo API. | ||
* @param {Object} options - additional options for the class. | ||
*/ | ||
constructor(creds, options) { | ||
this.creds = creds; | ||
this.options = options; | ||
} | ||
|
||
/** | ||
* Get stream for a remote File | ||
* | ||
* @param {string} [fileIdOrUrl] - The unique identifier or URL for the file | ||
* @param {function} callback - function to be called when the request completes. | ||
*/ | ||
get(fileIdOrUrl, callback) { | ||
|
||
if(!fileIdOrUrl) { | ||
throw new Error('"fileIdOrUrl" is a required parameter'); | ||
} | ||
|
||
fileIdOrUrl = fileIdOrUrl.split("/").pop(-1); | ||
|
||
var config = { | ||
host:'api.nexmo.com', | ||
path:`${FilesResource.PATH}/${fileIdOrUrl}`, | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
'Authorization': `Bearer ${this.creds.generateJwt()}` | ||
} | ||
}; | ||
|
||
this.options.httpClient.request(config, callback); | ||
} | ||
|
||
/** | ||
* Save remote File locally | ||
* | ||
* @param {string} [fileIdOrUrl] - The unique identifier or URL for the file | ||
* @param {string} [file] - Filename or file descriptor | ||
* @param {function} callback - function to be called when the request completes. | ||
*/ | ||
save(fileIdOrUrl, file, callback) { | ||
this.get(fileIdOrUrl, (error, data) => { | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
this.__storeFile(data, file, callback); | ||
} | ||
}) | ||
} | ||
|
||
__storeFile(data, file, callback) { | ||
fs.writeFile(file, data, (error) => { | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
callback(null, file); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
export default FilesResource; |
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,76 @@ | ||
import chai, { | ||
expect | ||
} from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
chai.use(sinonChai); | ||
|
||
import ResourceTestHelper from './ResourceTestHelper'; | ||
|
||
import FilesResource from '../lib/FilesResource'; | ||
import HttpClient from '../lib/HttpClient'; | ||
import Credentials from '../lib/Credentials'; | ||
|
||
var creds = Credentials.parse({ | ||
applicationId: 'some-id', | ||
privateKey: __dirname + '/private-test.key' | ||
}); | ||
var emptyCallback = () => {}; | ||
|
||
describe('FileResource', () => { | ||
|
||
var httpClientStub = null; | ||
var files = null; | ||
|
||
beforeEach(() => { | ||
httpClientStub = sinon.createStubInstance(HttpClient); | ||
var options = { | ||
httpClient: httpClientStub | ||
}; | ||
files = new FilesResource(creds, options); | ||
}); | ||
|
||
it('should get a single file using a file ID', () => { | ||
const fileId = '2342342-lkjhlkjh-32423'; | ||
files.get(fileId, emptyCallback); | ||
|
||
var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, { | ||
method: 'GET', | ||
body: undefined, | ||
path: `${FilesResource.PATH}/${fileId}`, | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
'Authorization': 'Bearer ' | ||
} | ||
}); | ||
|
||
expect(httpClientStub.request) | ||
.to.have.been.calledWith( | ||
sinon.match(expectedRequestArgs), | ||
emptyCallback | ||
); | ||
}); | ||
|
||
it('should get a single file using a file URL', () => { | ||
const fileId = '2342342-lkjhlkjh-32423'; | ||
const fileUrl = `https://rest.nexmo.com/api/v1/files/${fileId}`; | ||
files.get(fileUrl, emptyCallback); | ||
|
||
var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, { | ||
method: 'GET', | ||
body: undefined, | ||
path: `${FilesResource.PATH}/${fileId}`, | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
'Authorization': 'Bearer ' | ||
} | ||
}); | ||
|
||
expect(httpClientStub.request) | ||
.to.have.been.calledWith( | ||
sinon.match(expectedRequestArgs), | ||
emptyCallback | ||
); | ||
}); | ||
}); |
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