1
0
mirror of synced 2024-11-24 21:36:02 +03:00

upload files methods, unussign dialog method (#10)

This commit is contained in:
Akolzin Dmitry 2020-08-24 11:17:55 +03:00 committed by GitHub
parent 05d2188453
commit 89a38c39b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 233 additions and 5034 deletions

64
.npmignore Normal file
View File

@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
.idea
out/

View File

@ -20,7 +20,7 @@ jobs:
- npm install -g codecov - npm install -g codecov
- npm run report-coverage - npm run report-coverage
- stage: gh pages - stage: gh pages
node_js: 13 node_js: 10
script: skip script: skip
before_deploy: npm run doc before_deploy: npm run doc
deploy: deploy:
@ -33,7 +33,7 @@ jobs:
branch: master branch: master
if: branch = master AND type = push AND fork = false if: branch = master AND type = push AND fork = false
- stage: npm deploy - stage: npm deploy
node_js: 13 node_js: 10
script: skip script: skip
before_deploy: npm run build before_deploy: npm run build
deploy: deploy:

View File

@ -40,11 +40,12 @@ export default class Request {
* @param {string} endpoint * @param {string} endpoint
* @param {string} method * @param {string} method
* @param {Object} data * @param {Object} data
* @param {boolean} serializable
* @returns {Promise} * @returns {Promise}
* @throws {Error} * @throws {Error}
* @private * @private
*/ */
_request(endpoint, method, data = {}) { _request(endpoint, method, data = {}, serializable = true) {
let path = this._getPath(endpoint); let path = this._getPath(endpoint);
let response = ''; let response = '';
@ -87,7 +88,14 @@ export default class Request {
}); });
if (['POST', 'PUT', 'PATCH'].includes(method)) { if (['POST', 'PUT', 'PATCH'].includes(method)) {
request.write(JSON.stringify(data)); let sendData;
if (serializable) {
sendData = JSON.stringify(data);
} else {
sendData = data;
}
request.write(sendData);
} }
request.end(); request.end();
@ -112,15 +120,16 @@ export default class Request {
* Method POST * Method POST
* @param {string} endpoint * @param {string} endpoint
* @param {Object} data * @param {Object} data
* @param {boolean} serializable
* @returns {Promise} * @returns {Promise}
* @throws {Error} * @throws {Error}
*/ */
post(endpoint, data) { post(endpoint, data, serializable = true) {
if (!data) { if (!data) {
throw new Error('Body is not be empty'); throw new Error('Body is not be empty');
} }
return this._request(endpoint, 'POST', data); return this._request(endpoint, 'POST', data, serializable);
}; };
/** /**

View File

@ -83,6 +83,21 @@ export default class Client {
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog); return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog);
}; };
/**
* Unassign dialog
* @param {Number} dialog_id - Dialog id
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
unassignDialog(dialog_id) {
if (!dialog_id) {
throw new Error('Parameter `dialog_id` is required');
}
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/unassign', {});
}
/** /**
* Close dialog * Close dialog
* @param {Number} dialog_id - Dialog id * @param {Number} dialog_id - Dialog id
@ -210,6 +225,49 @@ export default class Client {
return this._request.get(this._version + '/users', params); return this._request.get(this._version + '/users', params);
}; };
/**
* Get file information
* @param {string} file_id - File identifier
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
getFile(file_id) {
if (!file_id) {
throw new Error('Parameter `file_id` is required');
}
return this._request.get(this._version + '/files/' + file_id)
}
/**
* Upload file
*
* @param {string} data - Binary data
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
filesUpload(data) {
return this._request.post(this._version + '/files/upload', data, false);
}
/**
* Upload file by url
*
* @param {string} url - File url address
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
filesUploadByUrl(url) {
if (!url) {
throw new Error('Parameter `url` is required');
}
return this._request.post(this._version + '/files/upload_by_url', {url})
}
/** /**
* Get websocket url * Get websocket url
* @param {array<string>} events - Array of strings with websocket events * @param {array<string>} events - Array of strings with websocket events

5024
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@
"Bot", "Bot",
"Chat" "Chat"
], ],
"version": "1.0.4", "version": "1.1.0",
"scripts": { "scripts": {
"doc": "./node_modules/.bin/jsdoc lib/v1/client.js lib/consts.js -R README.md", "doc": "./node_modules/.bin/jsdoc lib/v1/client.js lib/consts.js -R README.md",
"build": "./node_modules/.bin/rollup -c", "build": "./node_modules/.bin/rollup -c",

View File

@ -1,5 +1,6 @@
import nock from 'nock' import nock from 'nock'
import chai from 'chai' import chai from 'chai'
import https from 'https'
import MgBotApiClient from '../index' import MgBotApiClient from '../index'
describe('#API client v1', function() { describe('#API client v1', function() {
@ -25,9 +26,9 @@ describe('#API client v1', function() {
}); });
it('Get empty bots list', function () { it('Get empty bots list', function () {
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/bots?id=1').reply(200, []);
api.getBots().then(function (value) { api.getBots({id: 1}).then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty; chai.expect(value).to.be.empty;
}); });
@ -156,6 +157,20 @@ describe('#API client v1', function() {
chai.expect(api.assignDialog.bind(api)).to.throw('Parameter `dialog_id` is required'); chai.expect(api.assignDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
}); });
it('Unassign dialog', function () {
nock('https://api.example.com/api/bot/v1').patch('/dialogs/1/unassign').reply(200, {
previous_responsible: {type: 'user', id: 1, assigned_at: '2019-01-22T11:50:13Z'}
});
api.unassignDialog(1).then(function (value) {
chai.expect(value).to.be.an('object');
});
});
it('Unassign dialog incorrect', function () {
chai.expect(api.unassignDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
});
it('Close dialog', function () { it('Close dialog', function () {
nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {}); nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {});
@ -336,6 +351,83 @@ describe('#API client v1', function() {
}); });
}); });
it('Get file', function () {
nock('https://api.example.com/api/bot/v1').get('/files/1').reply(200, {
id: '1',
size: 100,
type: 'image',
url: 'https://file.url'
});
api.getFile('1').then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value.id).to.be.equal('1');
chai.expect(value.size).to.be.equal(100);
chai.expect(value.type).to.be.equal('image');
chai.expect(value.url).to.be.equal('https://file.url');
});
});
it('Get file incorrect', function () {
chai.expect(api.getFile.bind(api)).to.throw('Parameter `file_id` is required');
});
it('File upload', function () {
const options = {
host: 'via.placeholder.com',
path: '/300'
};
const req = https.get(options, function (res) {
let data = Buffer.from('', 'binary');
res.on('data', function (chunk) {
data = Buffer.concat([data, Buffer.from(chunk, 'binary')])
});
res.on('end', function () {
nock('https://api.example.com/api/bot/v1').post('/files/upload', data).reply(200, {
id: '1',
size: 1132,
type: 'image'
});
api.filesUpload(data).then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value.id).to.be.equal('1');
chai.expect(value.size).to.be.equal(1132);
chai.expect(value.type).to.be.equal('image');
});
});
});
req.end();
});
it('File upload incorrect', function () {
chai.expect(api.filesUpload.bind(api)).to.throw('Body is not be empty');
});
it('File upload by url', function () {
nock('https://api.example.com/api/bot/v1').post('/files/upload_by_url', {url: 'https://fileurl.com'}).reply(200, {
id: '123',
size: 1132,
type: 'image',
url: 'https://file.url'
});
api.filesUploadByUrl('https://fileurl.com').then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value.id).to.be.equal('123');
chai.expect(value.size).to.be.equal(1132);
chai.expect(value.type).to.be.equal('image');
chai.expect(value.url).to.be.equal('https://file.url');
});
});
it('File upload by url incorrect', function () {
chai.expect(api.filesUploadByUrl.bind(api)).to.throw('Parameter `url` is required');
});
it('Get websocket data', function () { it('Get websocket data', function () {
const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew, MgBotApiClient.types().wsMessageUpdated]); const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew, MgBotApiClient.types().wsMessageUpdated]);
const expectedUrl = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated'; const expectedUrl = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';