|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Client = require('node-rest-client').Client; |
| 4 | +var ServiceFactory = require('./services/servicefactory.js'); |
| 5 | +var Customer = require('./customer/customer.js'); |
| 6 | + |
| 7 | +var NITRAPI_LIVE_URL = 'https://api.nitrado.net/'; |
| 8 | + |
| 9 | +class Nitrapi { |
| 10 | + constructor (accessToken, locale = null, url = NITRAPI_LIVE_URL) { |
| 11 | + this.client = new Client(); |
| 12 | + this.nitrapiUrl = url; |
| 13 | + this.options = { |
| 14 | + access_token: accessToken |
| 15 | + }; |
| 16 | + if (locale !== null) { |
| 17 | + this.options.locale = locale; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + ping (success, failure) { |
| 22 | + this.dataGet('ping', {}, success, failure); |
| 23 | + } |
| 24 | + |
| 25 | + getService (success, failure, id) { |
| 26 | + var api = this; |
| 27 | + this.dataGet('services/' + id, {}, function (data) { |
| 28 | + ServiceFactory.buildService(api, data.service, success, failure); |
| 29 | + }, failure); |
| 30 | + } |
| 31 | + |
| 32 | + getServices (success, failure) { |
| 33 | + var api = this; |
| 34 | + this.dataGet('services', {}, function (data) { |
| 35 | + ServiceFactory.buildServices(api, data.services, success, failure); |
| 36 | + }, failure); |
| 37 | + } |
| 38 | + |
| 39 | + getAdmin (success, failure) { |
| 40 | + // TODO |
| 41 | + failure('not yet implemented'); |
| 42 | + } |
| 43 | + |
| 44 | + getCustomer (success, failure) { |
| 45 | + this.dataGet('user', {}, function (data) { |
| 46 | + success(new Customer(this, data.user)); |
| 47 | + }, failure); |
| 48 | + } |
| 49 | + |
| 50 | + dataGet (endpoint, params, success, failure) { |
| 51 | + this.req('get', endpoint, params, success, failure); |
| 52 | + } |
| 53 | + dataPost (endpoint, params, success, failure) { |
| 54 | + this.req('post', endpoint, params, success, failure); |
| 55 | + } |
| 56 | + dataDelete (endpoint, params, success, failure) { |
| 57 | + this.req('delete', endpoint, params, success, failure); |
| 58 | + } |
| 59 | + req (method, endpoint, params, success, failure) { |
| 60 | + for (var name in this.options) { |
| 61 | + if (this.options.hasOwnProperty(name)) { |
| 62 | + params[name] = this.options[name]; |
| 63 | + } |
| 64 | + } |
| 65 | + var args = { |
| 66 | + parameters: params |
| 67 | + }; |
| 68 | + |
| 69 | + var func = this.client.get; |
| 70 | + if (method === 'post') { |
| 71 | + func = this.client.post; |
| 72 | + } else if (method === 'delete') { |
| 73 | + func = this.client.delete; |
| 74 | + } |
| 75 | + |
| 76 | + var req = func(this.nitrapiUrl + endpoint, args, function (data) { |
| 77 | + if (data.status === 'success') { |
| 78 | + if (typeof data.data === 'undefined') { |
| 79 | + success(data.message); |
| 80 | + } else { |
| 81 | + success(data.data); |
| 82 | + } |
| 83 | + } else { |
| 84 | + failure(data.message); |
| 85 | + } |
| 86 | + }); |
| 87 | + req.on('requestTimeout', function (req) { |
| 88 | + console.log('request has expired'); |
| 89 | + failure('request has expired'); |
| 90 | + req.abort(); |
| 91 | + }); |
| 92 | + |
| 93 | + req.on('responseTimeout', function () { |
| 94 | + console.log('response has expired'); |
| 95 | + failure('response has expired'); |
| 96 | + }); |
| 97 | + |
| 98 | + req.on('error', function (err) { |
| 99 | + console.log('ERROR: ' + err); |
| 100 | + failure(err); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +module.exports = Nitrapi; |
0 commit comments