Skip to content

Commit

Permalink
adding wit.getSamples & wit.backup
Browse files Browse the repository at this point in the history
  • Loading branch information
bashz committed Feb 21, 2019
1 parent 25bd6e4 commit 58c4c3e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
run.js
npm-debug.log
.nyc_output
backup.zip
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wit-api",
"version": "0.0.5",
"version": "0.0.6",
"description": "Implementation of wit api, provides methods to easily manage your wit app",
"main": "index.js",
"keywords": [
Expand Down
47 changes: 47 additions & 0 deletions src/Sample.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
const fs = require('fs')
const req = require('request')

const actions = function(id) {
return {
get: {
method: 'GET',
uri: '/samples'
},
add: {
method: 'POST',
uri: '/samples'
},
delete: {
method: 'DELETE',
uri: '/samples'
},
export: {
method: 'GET',
uri: 'export'
}
}
}

module.exports = function (request) {
return {
get: function (limit = 10, offset = 0, entities = [], values = [], isNegative = false) {
let payload = actions().get
payload.qs = {
limit,
offset,
entity_ids: entities,
entity_values: values,
negative: isNegative
}
return new Promise((resolve, reject) => {
request(payload, (err, res) => {
if (err) {
return reject(err)
}
return resolve(res)
})
})
},
add: function (...args) {
let payload = actions().add
let body = Array.isArray(args[0]) ? args[0] : [{text: args[0], entities: args[1]}]
Expand Down Expand Up @@ -46,6 +75,24 @@ module.exports = function (request) {
return resolve(res)
})
})
},
export: function (filePath = './backup.zip') {
let payload = actions().export
return new Promise((resolve, reject) => {
request(payload, (err, res) => {
if (err) {
return reject(err)
}
req(res)
.on('error', (err) => {
return reject(err)
})
.on('response', (response) => {
return resolve(response)
})
.pipe(fs.createWriteStream(filePath))
})
})
}
}
}
2 changes: 2 additions & 0 deletions src/Wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const Wit = function(token, timeout) {
const trainer = sample(this.doRequest)
this.train = trainer.add
this.forget = trainer.delete
this.getSamples = trainer.get
this.backup = trainer.export

this.app = function(name, data) {
return new App(name, this.doRequest, data)
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ describe('Wit', function () {
}).catch(e => {
expect(e).to.be.an(Error)
expect(e).to.have.property('code')
expect(e.code).to.be('unknown')
expect(e.code).to.be('no-auth')
expect(e.message).not.to.be.empty()
})
})
Expand Down

0 comments on commit 58c4c3e

Please sign in to comment.