-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.js
More file actions
112 lines (94 loc) · 3.13 KB
/
job.js
File metadata and controls
112 lines (94 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var request = require('request');
// Set `OBEHAVE_API` as an environment variable for local development.
var api = process.env.OBEHAVE_API || 'https://www.obehave.io/api/v1';
var url = api + '/jobs';
/**
* Creates an OBehave job.
*
* @param target {String} url of the environment to test.
* @param apiKey {String} api key for the project.
* @param callback {Function} called once the job is created or fails.
*/
function create(target, apiKey, callback) {
request.post({
headers: {
'Authorization': `bearer ${apiKey}`
},
url: this.url,
json: {target}
}, (err, response, body) => {
if (err) {
throw('Failed to create job:' + err);
}
// GET requests require manual JSON parsing, POST requests do not.
if (response.statusCode === 401 || response.statusCode === 403) {
throw(`Access denied, invalid API key, HTTP error code ${response.statusCode}`)
}
if (response.statusCode === 400) {
var hasError = (response.body && response.body.error);
var message = hasError ? response.body.error : '';
throw(`Failed to create job due to bad request. ${message}`)
}
if (response.statusCode !== 200) {
throw(`Failed to create job, HTTP error code ${response.statusCode}`)
}
if (!body) {
throw('Failed to create job, no body returned');
}
if (!body.jobId) {
throw('Failed to create job, no jobId returned');
}
callback(err, body.jobId);
});
}
/**
* Returns the status of an OBehave job.
*
* @param jobId {String}
* @param apiKey
* @param callback
*/
function status(jobId, apiKey, callback) {
request.get({
headers: {
'Authorization': `bearer ${apiKey}`
},
url: `${this.url}/${jobId}`
}, (err, response, body) => {
if (response.statusCode === 401 || response.statusCode === 403) {
throw(`Access denied, invalid API key, HTTP error code ${response.statusCode}`)
}
if (response.statusCode === 400) {
const hasError = (response.body && response.body.error);
const message = hasError ? response.body.error : '';
throw(`Failed to create job due to bad request. ${message}`)
}
if (response.statusCode !== 200) {
throw(`Failed to read job, HTTP error code ${response.statusCode}`)
}
if (!body) {
throw('Failed to read job, no body returned');
}
if (!isJsonString(body)) {
throw('Failed to read job, body is not valid json')
}
// GET requests require manual JSON parsing, POST requests do not.
const parsed = JSON.parse(body);
if (!parsed.status) {
throw('Failed to read job, no status returned');
}
if (err) {
throw('Failed to read job');
}
callback(err, parsed.status);
})
}
function isJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
module.exports = {url, create, status};