app-automate.js 2.6 KB

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