index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict'
  2. var request = require('request');
  3. function ZT1Client(url,authToken)
  4. {
  5. this.url = url;
  6. this.authToken = authToken;
  7. }
  8. ZT1Client.prototype._jsonGet = function(getPath,callback)
  9. {
  10. request({
  11. url: this.url + getPath,
  12. method: 'GET',
  13. headers: {
  14. 'X-ZT1-Auth': this.authToken
  15. }
  16. },function(error,response,body) {
  17. if (error)
  18. return callback(error,{});
  19. if (response.statusCode !== 200)
  20. return callback(new Error('server responded with '+response.statusCode),{});
  21. return callback(null,(typeof body === 'string') ? JSON.parse(body) : null);
  22. });
  23. };
  24. ZT1Client.prototype.status = function(callback)
  25. {
  26. request({
  27. url: this.url + 'controller',
  28. method: 'GET',
  29. headers: {
  30. 'X-ZT1-Auth': this.authToken
  31. }
  32. },function(error,response,body) {
  33. if (error)
  34. return callback(error,{});
  35. var controllerStatus = {};
  36. if (typeof body === 'string')
  37. controllerStatus = JSON.parse(body);
  38. request({
  39. url: this.url + 'status',
  40. method: 'GET',
  41. headers: {
  42. 'X-ZT1-Auth': this.authToken
  43. }
  44. },function(error,response,body) {
  45. if (error)
  46. return callback(error,{});
  47. if (response.statusCode !== 200)
  48. return callback(new Error('server responded with '+response.statusCode),{});
  49. var nodeStatus = JSON.parse(body);
  50. for(var k in controllerStatus)
  51. nodeStatus[k] = controllerStatus[k];
  52. return callback(null,nodeStatus);
  53. }.bind(this));
  54. }.bind(this));
  55. };
  56. ZT1Client.prototype.networks = function(callback)
  57. {
  58. this._jsonGet('network',callback);
  59. };
  60. ZT1Client.prototype.controllerNetworks = function(callback)
  61. {
  62. this._jsonGet('controller/network',callback);
  63. };
  64. exports.ZT1Client = ZT1Client;