forked from timneutkens/openprovider-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (72 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const request = require('superagent')
const xml2js = require('xml2js')
const builder = new xml2js.Builder()
const parser = new xml2js.Parser()
class openprovider {
/**
* Initialize request object
* @param config
*/
constructor (config) {
this.requestObject = {
openXML: {
credentials: {
username: config.username
}
}
}
if (typeof config.hash !== 'undefined') {
this.requestObject.openXML.credentials.hash = config.hash
} else if (typeof config.password !== 'undefined') {
this.requestObject.openXML.credentials.password = config.password
}
if (config.cli) {
parser.options.explicitArray = false
}
}
/**
* Create openprovider xml command
* @param command
* @param params
*/
command (command, params) {
// Don't mutate the request object.
let requestObject = Object.assign({}, this.requestObject)
requestObject.openXML[command] = params
return builder.buildObject(requestObject)
}
/**
* Request a given openprovider command
* @param command
* @param params
* @param callback
*/
request (command, params, callback) {
const xml = this.command(command, params)
return new Promise(function (resolve, reject) {
return request
.post('https://api.openprovider.eu')
.accept('xml')
.timeout(10000)
.send(xml)
.then(function (response) {
parser.parseString(response.text, function (error, result) {
if (typeof callback === 'function') {
callback(error, result)
}
if (error) {
reject(error)
}
resolve(result)
})
})
.catch(function (error) {
if (typeof callback === 'function') {
callback(error, null)
}
reject(error)
})
})
}
}
module.exports = openprovider