forked from OmerTu/GoogleHomeKodi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed eslint to run on all js files. Also in subdirectories. Removed duplicate line from node.js.
- Loading branch information
Showing
3 changed files
with
60 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,69 @@ | ||
/** | ||
* Custom response exception | ||
* @param {*} message Exception message. | ||
* @param {*} statusText Optional status text, retreived from the responses status text. | ||
* @param {*} status Optional status response code. | ||
*/ | ||
function ResponseException(message, statusText, status) { | ||
this.message = message; | ||
this.name = 'ResponseException'; | ||
this.statusText = statusText || this.message; | ||
this.status = status || 400; | ||
} | ||
const namespaces = require('./api-methods.js'); | ||
const ResponseException = require('../exceptions.js').ResponseException; | ||
|
||
module.exports = function(fetch) { | ||
var namespaces = require('./api-methods.js'); | ||
module.exports = (fetch) => { | ||
|
||
const addMethods = (obj) => { | ||
namespaces.forEach((namespace) => { | ||
obj[namespace.name] = namespace.methods.reduce((result, method) => { | ||
result[method] = (params, callback) => { | ||
return obj.send(`${namespace.name}.${method}`, params, callback); | ||
}; | ||
return result; | ||
}, {}); | ||
}); | ||
}; | ||
|
||
const Kodi = function(ip, port, username, password) { | ||
this.kodiAuth = `Basic ${new Buffer(`${username}:${password}`).toString('base64')}`; | ||
this.url = `http://${ip}:${port}/jsonrpc`; | ||
addMethods(this); | ||
}; | ||
|
||
function addMethods(obj) { | ||
namespaces.forEach(function(namespace) { | ||
obj[namespace.name] = namespace.methods.reduce(function(result, method) { | ||
result[method] = function(params, callback) { | ||
return obj.send(namespace.name + '.' + method, params, callback); | ||
Kodi.prototype.sendHTTP = function(body, callback) { | ||
console.log(`Command sent = ${body}`); | ||
const headers = { | ||
'Content-type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': this.kodiAuth | ||
}; | ||
return result; | ||
}, {}); | ||
}); | ||
} | ||
|
||
function Kodi(ip, port, username, password) { | ||
this.kodi_auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); | ||
this.url = 'http://' + ip + ':' + port + '/jsonrpc'; | ||
addMethods(this); | ||
} | ||
return fetch(this.url, { | ||
method: 'POST', | ||
body: body, | ||
headers: headers | ||
}) | ||
.then((response) => { | ||
if (response.ok) { | ||
return response.json(); | ||
} | ||
|
||
Kodi.prototype.sendHTTP = function(body, callback) { | ||
console.log('Command sent = ' + body); | ||
var headers = { | ||
"Content-type": "application/json", | ||
'Accept': "application/json", | ||
'Authorization': this.kodi_auth | ||
throw new ResponseException( | ||
`Error in response, ${response.statusText} with status code: ${response.status}`, | ||
response.status, | ||
response.statusText); | ||
}) | ||
.then((data) => { | ||
if (callback) { | ||
callback(data); | ||
} | ||
return data; | ||
}); | ||
}; | ||
|
||
return fetch(this.url, { | ||
method: 'POST', | ||
body: body, | ||
headers: headers | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return response.json(); | ||
} | ||
|
||
throw new ResponseException( | ||
`Error in response, ${response.statusText} with status code: ${response.status}`, | ||
response.statusText, | ||
response.status); | ||
}) | ||
.then(function(data) { | ||
if (callback) callback(data); | ||
return data; | ||
}); | ||
}; | ||
Kodi.prototype.send = function(method, params, callback) { | ||
let body = { | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: method | ||
}; | ||
|
||
Kodi.prototype.send = function(method, params, callback) { | ||
var body = { | ||
jsonrpc: "2.0", | ||
id: 1, | ||
method: method | ||
if (params) { | ||
body.params = params; | ||
} | ||
body = JSON.stringify(body); | ||
return this.sendHTTP(body, callback); | ||
}; | ||
|
||
if(params) body.params = params; | ||
body = JSON.stringify(body); | ||
return this.sendHTTP(body, callback); | ||
}; | ||
|
||
return Kodi; | ||
}; | ||
return Kodi; | ||
}; |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
module.exports = require('./api.js')(require('node-fetch')); | ||
module.exports = require('./api.js')(require('node-fetch')); |
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