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