Skip to content

Commit

Permalink
Cleaned up api.js.
Browse files Browse the repository at this point in the history
Changed eslint to run on all js files. Also in subdirectories.
Removed duplicate line from node.js.
  • Loading branch information
p0psicles committed Dec 7, 2017
1 parent 65a03e9 commit cbeba94
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 68 deletions.
125 changes: 59 additions & 66 deletions kodi-connection/api.js
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;
};
1 change: 0 additions & 1 deletion kodi-connection/node.js
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'));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "server.js",
"scripts": {
"start": "node server.js",
"lint": "./node_modules/.bin/eslint *.js"
"lint": "./node_modules/.bin/eslint **/*.js"
},
"dependencies": {
"dotenv": "^4.0.0",
Expand Down

0 comments on commit cbeba94

Please sign in to comment.