Comparing version
810
index.js
const request = require('request'), | ||
models = require('./models'); | ||
module.exports = { | ||
apiKey: '', | ||
accountUsers: [], | ||
smartLocks: [], | ||
models: models, | ||
setApiKey: function(key) { | ||
module.exports.apiKey = key; | ||
}, | ||
function nukiApi() { | ||
let self = { | ||
apiKey: '', | ||
accountUsers: [], | ||
smartLocks: [], | ||
models: models, | ||
setApiKey: function(key) { | ||
self.apiKey = key; | ||
}, | ||
getAccountUsers: function (reload = false) { | ||
return new Promise(function (resolve, reject) { | ||
if(reload || module.exports.accountUsers.length == 0) { | ||
getAccountUsers: function (reload = false) { | ||
return new Promise(function (resolve, reject) { | ||
if(reload || self.accountUsers.length == 0) { | ||
var options = { | ||
url: "https://api.nuki.io/account/user", | ||
method: "GET", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + self.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 200) { | ||
self.accountUsers = JSON.parse(body); | ||
return resolve(self.accountUsers); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(self.accountUsers); | ||
} | ||
}); | ||
}, | ||
getSmartLocks: function (reload = false, nukiApiKey = null) { | ||
return new Promise(function (resolve, reject) { | ||
if(reload || self.smartLocks.length == 0) { | ||
let key = (nukiApiKey == null) ? self.apiKey : nukiApiKey; | ||
let options = { | ||
url: "https://api.nuki.io/smartlock", | ||
method: "GET", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + key | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 200) { | ||
self.smartLocks = JSON.parse(body); | ||
return resolve(self.smartLocks); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(self.smartLocks); | ||
} | ||
}); | ||
}, | ||
getSmartLockUsers: function (smartlockId, reload = false) { | ||
return new Promise(function (resolve, reject) { | ||
if(self.smartLocks.length == 0) { | ||
self.getSmartLocks(true).then(function () { | ||
proceed(); | ||
}); | ||
} else { | ||
proceed(); | ||
}; | ||
function proceed() { | ||
var lock = self.smartLocks.filter(function (lock) { | ||
return lock.smartlockId == smartlockId; | ||
})[0]; | ||
if(lock == null) | ||
return reject("No Lock Found!") | ||
if(reload || lock.users == null) { | ||
var options = { | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + '/auth', | ||
method: "GET", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + self.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 200) { | ||
self.findSmartlockById(smartlockId).then(function (smartLock) { | ||
smartLock.users = JSON.parse(body); | ||
return resolve(smartLock.users); | ||
}); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(lock.users); | ||
} | ||
}; | ||
}); | ||
}, | ||
findAccountUserById: function (accountUserId) { | ||
return new Promise(function (resolve, reject) { | ||
if(self.accountUsers.length == 0) { | ||
self.getAccountUsers().then(function (users) { | ||
return resolve(filterUsersById(accountUserId)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterUsersById(accountUserId)); | ||
}; | ||
}); | ||
function filterUsersById(accountUserId) { | ||
var filteredUsers = self.accountUsers.filter(function (user) { | ||
return user.accountUserId == accountUserId; | ||
}); | ||
if(filteredUsers.length > 0) | ||
return filteredUsers[0]; | ||
return null; | ||
} | ||
}, | ||
findAccountUserByName: function (name) { | ||
return new Promise(function (resolve, reject) { | ||
if(self.accountUsers.length == 0) { | ||
self.getAccountUsers().then(function (users) { | ||
return resolve(filterUsersByName(name)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterUsersByName(name)); | ||
}; | ||
}); | ||
function filterUsersByName(name) { | ||
var filteredUsers = self.accountUsers.filter(function (user) { | ||
return user.name == name; | ||
}); | ||
if(filteredUsers.length > 0) | ||
return filteredUsers[0]; | ||
return null; | ||
} | ||
}, | ||
findSmartlockById: function (smartlockId) { | ||
return new Promise(function (resolve, reject) { | ||
if(self.smartLocks.length == 0) { | ||
self.getSmartLocks().then(function (locks) { | ||
return resolve(filterLocksById(smartlockId)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterLocksById(smartlockId)); | ||
}; | ||
}); | ||
function filterLocksById(smartlockId) { | ||
var filteredLocks = self.smartLocks.filter(function (lock) { | ||
return lock.smartlockId == smartlockId; | ||
}); | ||
if(filteredLocks.length > 0) | ||
return filteredLocks[0]; | ||
return null; | ||
} | ||
}, | ||
findSmartlockByName: function (name) { | ||
return new Promise(function (resolve, reject) { | ||
if(self.smartLocks.length == 0) { | ||
self.getSmartLocks().then(function (locks) { | ||
return resolve(filterLocksByName(name)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterLocksByName(name)); | ||
}; | ||
}); | ||
function filterLocksByName(name) { | ||
var filteredLocks = self.smartLocks.filter(function (lock) { | ||
return lock.name == name; | ||
}); | ||
if(filteredLocks.length > 0) | ||
return filteredLocks[0]; | ||
return null; | ||
} | ||
}, | ||
findAccountUserByAuthId: function(smartlockId, authId) { | ||
return new Promise(function (resolve, reject) { | ||
self.getSmartLockUsers(smartlockId).then(function (users) { | ||
var users = users.filter(function (user) { | ||
return user.id == authId; | ||
}); | ||
return resolve(users); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
findAccountUserAuthByName: function(smartlockId, name) { | ||
return new Promise(function (resolve, reject) { | ||
self.getSmartLockUsers(smartlockId).then(function (users) { | ||
var users = users.filter(function (user) { | ||
return user.name == name; | ||
}); | ||
return resolve(users); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
createNukiUser: function (accountUser) { | ||
return new Promise(function (resolve, reject) { | ||
self.findAccountUserByName(accountUser.name).then(function(result) { | ||
if(result == null) { | ||
var body = JSON.stringify(accountUser); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/account/user", | ||
method: "PUT", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + self.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
self.getAccountUsers(true).then(function () { | ||
return resolve(self.findAccountUserByName(accountUser.name)); | ||
}); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(result); | ||
/* | ||
self.findAccountUserByName(accountUser.name).then(function (user) { | ||
return resolve(user); | ||
}); | ||
*/ | ||
} | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
updateNukiUser: function (accountUserId, accountUser) { | ||
return new Promise(function (resolve, reject) { | ||
self.findAccountUserByName(accountUser.name).then(function(result) { | ||
if(result != null) { | ||
var body = JSON.stringify(accountUser); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/account/user/" + accountUserId, | ||
method: "POST", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + self.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
self.getAccountUsers(true).then(function () { | ||
return resolve(self.findAccountUserByName(accountUser.name)); | ||
}); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
self.createNukiUser(accountUser).then(function (result) { | ||
return resolve(result); | ||
}).catch(function(error) { | ||
return reject(error); | ||
}); | ||
}; | ||
}).catch(function (error) { | ||
reject(error); | ||
}); | ||
}); | ||
}, | ||
removeNukiUser: function (accountUserId) { | ||
return new Promise(function (resolve, reject) { | ||
self.findAccountUserById(accountUserId).then(function(result) { | ||
if(result != null) { | ||
var options = { | ||
url: "https://api.nuki.io/account/user/" + accountUserId, | ||
method: "DELETE", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + self.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(true); | ||
} | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
grantAccess: function (smartlockId, smartlockAuth) { | ||
return new Promise(function (resolve, reject) { | ||
self.findAccountUserAuthByName(smartlockId, smartlockAuth.name).then(function(result) { | ||
if(result.length == 0 || result.length == null) { | ||
var body = JSON.stringify(smartlockAuth); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + "/auth", | ||
method: "PUT", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + self.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
self.getSmartLockUsers(smartlockId); | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
self.updateAccess(smartlockId, result.id, smartlockAuth).then(function (result) { | ||
return resolve(result); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}; | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
updateAccess: function (smartlockId, authId, smartlockAuth) { | ||
return new Promise(function (resolve, reject) { | ||
var body = JSON.stringify(smartlockAuth); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/account/user", | ||
method: "GET", | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + "/auth/" + authId, | ||
method: "POST", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
} | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + self.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 200) { | ||
module.exports.accountUsers = JSON.parse(body); | ||
return resolve(module.exports.accountUsers); | ||
if(response.statusCode == 204) { | ||
self.getSmartLockUsers(smartlockId); | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(module.exports.accountUsers); | ||
} | ||
}); | ||
}, | ||
getSmartLocks: function (reload = false) { | ||
return new Promise(function (resolve, reject) { | ||
if(reload || module.exports.smartLocks.length == 0) { | ||
}); | ||
}, | ||
revokeAccess: function (smartlockId, authId) { | ||
return new Promise(function (resolve, reject) { | ||
var options = { | ||
url: "https://api.nuki.io/smartlock", | ||
method: "GET", | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + "/auth/" + authId, | ||
method: "DELETE", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + self.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 200) { | ||
module.exports.smartLocks = JSON.parse(body); | ||
return resolve(module.exports.smartLocks); | ||
if(response.statusCode == 204) { | ||
self.getSmartLockUsers(smartlockId); | ||
return resolve(true); | ||
} else { | ||
@@ -61,21 +428,14 @@ return reject(new Error(response.statusMessage)); | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(module.exports.smartLocks); | ||
} | ||
}); | ||
}, | ||
getSmartLockUsers: function (smartlockId, reload = false) { | ||
return new Promise(function (resolve, reject) { | ||
var lock = module.exports.smartLocks.filter(function (lock) { | ||
return lock.smartlockId == smartlockId; | ||
})[0]; | ||
}); | ||
}, | ||
if(lock == null) | ||
return reject("No Lock Found!") | ||
getLog: function (smartlockId = null) { | ||
return new Promise(function (resolve, reject) { | ||
var url = "https://api.nuki.io/smartlock/log/"; | ||
if(smartlockId != null) | ||
url += smartlockId; | ||
if(reload || lock.users == null) { | ||
var options = { | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + '/auth', | ||
url: url, | ||
method: "GET", | ||
@@ -85,3 +445,3 @@ headers: { | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
'Authorization': 'Bearer ' + self.apiKey | ||
} | ||
@@ -91,6 +451,3 @@ }; | ||
if(response.statusCode == 200) { | ||
module.exports.findSmartlockById(smartlockId).then(function (smartLock) { | ||
smartLock.users = JSON.parse(body); | ||
return resolve(smartLock.users); | ||
}); | ||
return resolve(JSON.parse(body)); | ||
} else { | ||
@@ -102,344 +459,9 @@ return reject(new Error(response.statusMessage)); | ||
request(options, callback); | ||
} else { | ||
return resolve(lock.users); | ||
} | ||
}); | ||
}, | ||
findAccountUserById: function (accountUserId) { | ||
return new Promise(function (resolve, reject) { | ||
if(module.exports.accountUsers.length == 0) { | ||
module.exports.getAccountUsers().then(function (users) { | ||
return resolve(filterUsersById(accountUserId)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterUsersById(accountUserId)); | ||
}; | ||
}); | ||
function filterUsersById(accountUserId) { | ||
var filteredUsers = module.exports.accountUsers.filter(function (user) { | ||
return user.accountUserId == accountUserId; | ||
}); | ||
if(filteredUsers.length > 0) | ||
return filteredUsers[0]; | ||
return null; | ||
} | ||
}, | ||
findAccountUserByName: function (name) { | ||
return new Promise(function (resolve, reject) { | ||
if(module.exports.accountUsers.length == 0) { | ||
module.exports.getAccountUsers().then(function (users) { | ||
return resolve(filterUsersByName(name)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterUsersByName(name)); | ||
}; | ||
}); | ||
}; | ||
function filterUsersByName(name) { | ||
var filteredUsers = module.exports.accountUsers.filter(function (user) { | ||
return user.name == name; | ||
}); | ||
if(filteredUsers.length > 0) | ||
return filteredUsers[0]; | ||
return null; | ||
} | ||
}, | ||
findSmartlockById: function (smartlockId) { | ||
return new Promise(function (resolve, reject) { | ||
if(module.exports.smartLocks.length == 0) { | ||
module.exports.getSmartLocks().then(function (locks) { | ||
return resolve(filterLocksById(smartlockId)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterLocksById(smartlockId)); | ||
}; | ||
}); | ||
return self; | ||
}; | ||
function filterLocksById(smartlockId) { | ||
var filteredLocks = module.exports.smartLocks.filter(function (lock) { | ||
return lock.smartlockId == smartlockId; | ||
}); | ||
if(filteredLocks.length > 0) | ||
return filteredLocks[0]; | ||
return null; | ||
} | ||
}, | ||
findSmartlockByName: function (name) { | ||
return new Promise(function (resolve, reject) { | ||
if(module.exports.smartLocks.length == 0) { | ||
module.exports.getSmartLocks().then(function (locks) { | ||
return resolve(filterLocksByName(name)); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}) | ||
} else { | ||
return resolve(filterLocksByName(name)); | ||
}; | ||
}); | ||
function filterLocksByName(name) { | ||
var filteredLocks = module.exports.smartLocks.filter(function (lock) { | ||
return lock.name == name; | ||
}); | ||
if(filteredLocks.length > 0) | ||
return filteredLocks[0]; | ||
return null; | ||
} | ||
}, | ||
findAccountUserByAuthId: function(smartlockId, authId) { | ||
return new Promise(function (resolve, reject) { | ||
module.exports.getSmartLockUsers(smartlockId).then(function (users) { | ||
var users = users.filter(function (user) { | ||
return user.id == authId; | ||
}); | ||
return resolve(users); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
findAccountUserAuthByName: function(smartlockId, name) { | ||
return new Promise(function (resolve, reject) { | ||
module.exports.getSmartLockUsers(smartlockId).then(function (users) { | ||
var users = users.filter(function (user) { | ||
return user.name == name; | ||
}); | ||
return resolve(users); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
createNukiUser: function (accountUser) { | ||
return new Promise(function (resolve, reject) { | ||
module.exports.findAccountUserByName(accountUser.name).then(function(result) { | ||
if(result == null) { | ||
var body = JSON.stringify(accountUser); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/account/user", | ||
method: "PUT", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
module.exports.getAccountUsers(true).then(function () { | ||
return resolve(module.exports.findAccountUserByName(accountUser.name)); | ||
}); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(module.exports.findAccountUserByName(accountUser.name)); | ||
} | ||
}).catch(function (error) { | ||
reject(error); | ||
}); | ||
}); | ||
}, | ||
updateNukiUser: function (accountUserId, accountUser) { | ||
return new Promise(function (resolve, reject) { | ||
module.exports.findAccountUserByName(accountUser.name).then(function(result) { | ||
if(result != null) { | ||
var body = JSON.stringify(accountUser); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/account/user/" + accountUserId, | ||
method: "POST", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
module.exports.getAccountUsers(true).then(function () { | ||
return resolve(module.exports.findAccountUserByName(accountUser.name)); | ||
}); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
module.exports.createNukiUser(accountUser).then(function (result) { | ||
return resolve(result); | ||
}).catch(function(error) { | ||
return reject(error); | ||
}); | ||
}; | ||
}).catch(function (error) { | ||
reject(error); | ||
}); | ||
}); | ||
}, | ||
removeNukiUser: function (accountUserId) { | ||
return new Promise(function (resolve, reject) { | ||
module.exports.findAccountUserById(accountUserId).then(function(result) { | ||
if(result != null) { | ||
var options = { | ||
url: "https://api.nuki.io/account/user/" + accountUserId, | ||
method: "DELETE", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
return resolve(true); | ||
} | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
grantAccess: function (smartlockId, smartlockAuth) { | ||
return new Promise(function (resolve, reject) { | ||
module.exports.findAccountUserAuthByName(smartlockId, smartlockAuth.name).then(function(result) { | ||
if(result.length == 0 || result.length == null) { | ||
var body = JSON.stringify(smartlockAuth); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + "/auth", | ||
method: "PUT", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
module.exports.getSmartLockUsers(smartlockId); | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
} else { | ||
module.exports.updateAccess(smartlockId, result.id, smartlockAuth).then(function (result) { | ||
return resolve(result); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}; | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}, | ||
updateAccess: function (smartlockId, authId, smartlockAuth) { | ||
return new Promise(function (resolve, reject) { | ||
var body = JSON.stringify(smartlockAuth); | ||
var contentLength = Buffer.byteLength(body); | ||
var options = { | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + "/auth/" + authId, | ||
method: "POST", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Content-Length': contentLength, | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
}, | ||
body: body | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
module.exports.getSmartLockUsers(smartlockId); | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
}; | ||
}; | ||
request(options, callback); | ||
}); | ||
}, | ||
revokeAccess: function (smartlockId, authId) { | ||
return new Promise(function (resolve, reject) { | ||
var options = { | ||
url: "https://api.nuki.io/smartlock/" + smartlockId + "/auth/" + authId, | ||
method: "DELETE", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 204) { | ||
module.exports.getSmartLockUsers(smartlockId); | ||
return resolve(true); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
}); | ||
}, | ||
getLog: function (smartlockId = null) { | ||
return new Promise(function (resolve, reject) { | ||
var url = "https://api.nuki.io/smartlock/log/"; | ||
if(smartlockId != null) | ||
url += smartlockId; | ||
var options = { | ||
url: url, | ||
method: "GET", | ||
headers: { | ||
'User-Agent': 'none', | ||
'Accept': 'application/json', | ||
'Authorization': 'Bearer ' + module.exports.apiKey | ||
} | ||
}; | ||
function callback(error, response, body) { | ||
if(response.statusCode == 200) { | ||
return resolve(JSON.parse(body)); | ||
} else { | ||
return reject(new Error(response.statusMessage)); | ||
} | ||
}; | ||
request(options, callback); | ||
}); | ||
} | ||
}; | ||
module.exports = nukiApi; |
{ | ||
"name": "nuki-api", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Nuki Web Api", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
@@ -9,0 +8,0 @@ "author": "gijs", |
@@ -9,7 +9,8 @@ ## Synopsis | ||
``` js | ||
var nuki = require('nuki-api') | ||
let nuki = require('nuki-api'), | ||
instance = new nuki(); | ||
nuki.apiKey = 'xxxxxxxxxxxxxxxxxxxxx' | ||
instance.apiKey = 'xxxxxxxxxxxxxxxxxxxxx' | ||
nuki.getSmartLocks(true).then(function (smartlocks) { | ||
instance.getSmartLocks(true).then(function (smartlocks) { | ||
console.log(smartlocks); | ||
@@ -16,0 +17,0 @@ }); |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
25004
8.62%470
4.21%1
-50%55
1.85%