automate.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var util = require("util");
  2. var querystring = require("querystring");
  3. var BaseClient = require("./client");
  4. var extend = require("./extend");
  5. function AutomateClient(settings) {
  6. this.server = {
  7. host: "api.browserstack.com"
  8. };
  9. BaseClient.call(this, settings);
  10. }
  11. util.inherits(AutomateClient, BaseClient);
  12. // public API
  13. extend(AutomateClient.prototype, {
  14. getPlan: function(fn) {
  15. this.request({
  16. path: this.path("/plan.json")
  17. }, fn);
  18. },
  19. getBrowsers: function(fn) {
  20. this.request({
  21. path: this.path("/browsers.json")
  22. }, fn);
  23. },
  24. getProjects: function(fn) {
  25. this.request({
  26. path: this.path("/projects.json")
  27. }, this.handleResponse(fn, this.stripChildKeys("automation_project")));
  28. },
  29. getProject: function(id, fn) {
  30. this.request({
  31. path: this.path("/projects/" + id + ".json")
  32. }, this.handleResponse(fn, function(project) {
  33. project = project.project;
  34. project.builds = this.stripChildKeys("automation_build")(project.builds);
  35. return project;
  36. }.bind(this)));
  37. },
  38. getBuilds: function(options, fn) {
  39. if (typeof options === "function") {
  40. fn = options;
  41. options = {};
  42. }
  43. this.request({
  44. path: this.path("/builds.json?" + querystring.stringify(options))
  45. }, this.handleResponse(fn, this.stripChildKeys("automation_build")));
  46. },
  47. getSessions: function(buildId, options, fn) {
  48. if (typeof fn === "undefined") {
  49. fn = options;
  50. options = {};
  51. }
  52. this.request({
  53. path: this.path("/builds/" + buildId + "/sessions.json?" +
  54. querystring.stringify(options))
  55. }, this.handleResponse(fn, this.stripChildKeys("automation_session")));
  56. },
  57. getSession: function(id, fn) {
  58. this.request({
  59. path: this.path("/sessions/" + id + ".json")
  60. }, this.handleResponse(fn, this.stripKey("automation_session")));
  61. },
  62. updateSession: function(id, options, fn) {
  63. var data = JSON.stringify(options);
  64. this.request({
  65. method: "PUT",
  66. path: this.path("/sessions/" + id + ".json")
  67. }, data, this.handleResponse(fn, this.stripKey("automation_session")));
  68. },
  69. deleteSession: function(id, fn) {
  70. this.request({
  71. method: "DELETE",
  72. path: this.path("/sessions/" + id + ".json")
  73. }, fn);
  74. }
  75. });
  76. // internal API
  77. extend(AutomateClient.prototype, {
  78. path: function(path) {
  79. return "/automate" + path;
  80. },
  81. handleResponse: function(fn, modifier) {
  82. return function(error, data) {
  83. if (error) {
  84. return fn(error);
  85. }
  86. fn(null, modifier(data));
  87. };
  88. },
  89. stripKey: function(key) {
  90. return function(item) {
  91. return item[key];
  92. };
  93. },
  94. stripChildKeys: function(key) {
  95. return function(items) {
  96. return items.map(function(item) {
  97. return item[key];
  98. });
  99. };
  100. }
  101. });
  102. module.exports = {
  103. createClient: function(settings) {
  104. return new AutomateClient(settings);
  105. }
  106. };