Skip to content

Commit

Permalink
Readme for binary downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Jun 1, 2017
1 parent bfa5989 commit d59a221
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,63 @@ rcsdk.platform().delete('/account/~/extension/~', {...query}).then(function(...)

If your `Promise` library supports global error handler it might be useful to log Requests and Responses there.

## Binary downloads

If you need to download a binary file from API (call recording, fax attachment), you can do it as follows:

### On NodeJS

```js
var fs = require('fs');

// read as buffer
rcsdk.platform().get('/account/~/messages/foo/content').then(function(res) {

return res.response().buffer(); // we are accessing Node Fetch's Response

}).then(function(buffer) {

fs.writeFileSync('./octocat.png', buffer);

});

// read as stream
rcsdk.platform().get('/account/~/messages/foo/content').then(function(res) {

res.response().body.pipe(fs.createWriteStream('./octocat.png')); // we are accessing Node Fetch's Response

});
```

See more here [https://github.com/bitinn/node-fetch#usage](https://github.com/bitinn/node-fetch#usage).

### In browser

```js
rcsdk.platform().get('/account/~/messages/foo/content').then(function(res) {

return res.response().blob(); // or arrayBuffer(), we are accessing WhatWG Fetch's Response

}).then(function(blob ){

var img = document.createElement('img');
img.src = URL.createObjectURL(blob);
document.getElementById('container').appendChild(img);

});
```

See more here [https://developer.mozilla.org/en-US/docs/Web/API/Response](https://developer.mozilla.org/en-US/docs/Web/API/Response).

### Generic

In any case you always can just add token to known URL of resource and download it using whatever library you want or
use directly as `<img src="..."/>`:

```js
var url = rcsdk.platform().createUrl('/account/~/messages/foo/content', {addServer: true, addToken: true});
```

## Rate Limiting

Platform class emits `rateLimitError` if server returns `429` status. You may supply an option `handleRateLimit` and
Expand Down
17 changes: 0 additions & 17 deletions typings.json

This file was deleted.

0 comments on commit d59a221

Please sign in to comment.