Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.

Commit fdd90d9

Browse files
committed
BREAKING CHANGE: return body instead of resp.body from methods when running in non-cli mode
1 parent 1f59388 commit fdd90d9

File tree

13 files changed

+84
-92
lines changed

13 files changed

+84
-92
lines changed

cli/index.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,6 @@ foundMethod.method(argv, function _methodCb(methodErr, methodResp) {
119119

120120
process.exit(1);
121121
}
122-
// if content is gzipped, then body is empty object, and content is in 'text'
123-
if (methodResp && methodResp.text) {
124-
try {
125-
methodResp.body = JSON.parse(methodResp.text);
126-
} catch (err) {
127-
console.error(err);
128-
methodResp.body = {};
129-
}
130-
}
131-
if (methodResp != null) process.stdout.write(safeJSON(methodResp.body || {})); // null means suppress output here
122+
123+
if (methodResp != null) process.stdout.write(safeJSON(methodResp || {})); // null means suppress output here
132124
});

lib/authorize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function emailAuth(authEmail, authPassword, cache, cb) {
2525
noHeaders,
2626
function clientCb(clientErr, clientResp) {
2727
if (clientErr) return cb(clientErr, clientResp);
28-
return cb(null, clientResp.body.id);
28+
return cb(null, clientResp.id);
2929
}
3030
);
3131
}

lib/jobs/artifactsGet.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,33 @@ function artifactsGet(params, cb) {
8383
// XXX TODO aws creds cache
8484
// XXX TODO fully shadow local aws creds
8585

86-
if (!data || !data.body || !data.body.Credentials) {
86+
if (!data || !data.Credentials) {
8787
err = { error: 'Error: no credentials for artifacts folder.' };
8888
console.log(err.error);
8989
return cb(err);
9090
}
91-
if (!data.body.bucket) {
91+
if (!data.bucket) {
9292
err = { error: 'Error: no artifacts bucket.' };
9393
console.log(err.error);
9494
return cb(err);
9595
}
96-
if (!data.body.folder) {
96+
if (!data.folder) {
9797
err = { error: 'Error: no artifacts folder.' };
9898
console.log(err.error);
9999
return cb(err);
100100
}
101101
AWS.config = new AWS.Config({
102-
accessKeyId: data.body.Credentials.AccessKeyId,
103-
secretAccessKey: data.body.Credentials.SecretAccessKey,
104-
sessionToken: data.body.Credentials.SessionToken,
102+
accessKeyId: data.Credentials.AccessKeyId,
103+
secretAccessKey: data.Credentials.SecretAccessKey,
104+
sessionToken: data.Credentials.SessionToken,
105105
});
106106
var s3 = new AWS.S3();
107107
var options = {
108-
Bucket: data.body.bucket,
109-
Prefix: data.body.folder,
108+
Bucket: data.bucket,
109+
Prefix: data.folder,
110110
};
111111

112-
var folder = data.body.folder + '/';
112+
var folder = data.folder + '/';
113113
var wildcard = false;
114114
var files = params.files;
115115
if (files && typeof files === 'string') {
@@ -172,8 +172,8 @@ function artifactsGet(params, cb) {
172172
}
173173
);
174174
var optionsGet = {
175-
Bucket: data.body.bucket,
176-
Key: data.body.folder + '/' + fileName,
175+
Bucket: data.bucket,
176+
Key: data.folder + '/' + fileName,
177177
};
178178
promises.push(new Promise((resolve) => {
179179
if (global.paperspace_cli && !json) {

lib/jobs/artifactsList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function artifactsList(params, cb) {
4040
params.jobId = projectConfig.getLastJobId(null, params.jobId);
4141
return method(artifactsList, params, function artifactsListCb(err, data) {
4242
if (global.paperspace_cli && !json) {
43-
if (data && data.body && data.body.length) {
44-
data.body.forEach(function itemFunc(item) {
43+
if (data && data.length) {
44+
data.forEach(function itemFunc(item) {
4545
if (item.file) {
4646
var message = item.file;
4747
if (item.size) message += '\t' + item.size + ' bytes';

lib/jobs/clone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function clone(params, cb) {
3838
params.jobId = projectConfig.getLastJobId(null, params.jobId);
3939
return method(clone, params, function _methodCb(err, resp) {
4040
if (err) return cb(err);
41-
if (resp.body) projectConfig.setLastJobId(resp.body.project, resp.body.id);
41+
if (resp) projectConfig.setLastJobId(resp.project, resp.id);
4242
return cb(err, resp);
4343
});
4444
}

lib/jobs/create.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,28 +196,28 @@ function create(params, cb) {
196196
function maybeTailLogs(err, resp) {
197197
if (global.paperspace_cli && !json && tail) {
198198
if (err) return cb(err);
199-
if (!resp.body || !resp.body.id) return new Error('Job create failed; job id not found.');
200-
var jobId = resp.body.id;
201-
console.log('New jobId: ' + resp.body.id);
202-
console.log('Job ' + resp.body.state);
203-
if (resp.body.state === 'Pending') console.log('Waiting for job to run...');
199+
if (!resp.id) return new Error('Job create failed; job id not found.');
200+
var jobId = resp.id;
201+
console.log('New jobId: ' + resp.id);
202+
console.log('Job ' + resp.state);
203+
if (resp.state === 'Pending') console.log('Waiting for job to run...');
204204
return jobs_waitfor({ jobId: jobId, state: 'Running' }, function _waitforCb(err, resp) {
205205
if (err) return cb(err);
206-
if (!resp.body || !resp.body.state) return new Error('Job state not found.');
207-
var state = resp.body.state;
206+
if (!resp.state) return new Error('Job state not found.');
207+
var state = resp.state;
208208
console.log('Job ' + state);
209209
if (state !== 'Stopped' && state !== 'Failed' && state !== 'Running') {
210-
if (state === 'Error' || resp.body.jobError) console.log('Error: ' + resp.body.jobError);
210+
if (state === 'Error' || resp.jobError) console.log('Error: ' + resp.jobError);
211211
return cb();
212212
}
213213
console.log('Awaiting logs...');
214214
return jobs_logs({ jobId: jobId, tail: true }, function _logsCb(err) {
215215
if (err) return cb(err);
216216
return jobs_show({ jobId: jobId }, function _showCb(err, resp) {
217217
if (err) return cb(err);
218-
if (!resp.body || !resp.body.state) return new Error('Job state not found.');
219-
console.log('Job ' + resp.body.state + (resp.body.exitCode || resp.body.exitCode === 0 ? ', exitCode ' + resp.body.exitCode : ''));
220-
if (resp.body.state === 'Error' || resp.body.jobError) console.log('Error: ' + resp.body.jobError);
218+
if (!resp.state) return new Error('Job state not found.');
219+
console.log('Job ' + resp.state + (resp.exitCode || resp.exitCode === 0 ? ', exitCode ' + resp.exitCode : ''));
220+
if (resp.state === 'Error' || resp.jobError) console.log('Error: ' + resp.jobError);
221221
return cb();
222222
});
223223
});
@@ -274,7 +274,7 @@ function create(params, cb) {
274274
params.workspaceFileName = path.basename(params.workspace);
275275
return method(create, params, function _methodCb(err, resp) {
276276
if (err) return cb(err);
277-
if (resp.body) projectConfig.setLastJobId(resp.body.project, resp.body.id);
277+
if (resp) projectConfig.setLastJobId(resp.project, resp.id);
278278
return maybeTailLogs(err, resp);
279279
});
280280
});
@@ -349,7 +349,7 @@ function create(params, cb) {
349349
params.workspaceFileName = path.basename(params.workspace);
350350
return method(create, params, function _methodCb(err, resp) {
351351
if (err) return cb(err);
352-
if (resp.body) projectConfig.setLastJobId(resp.body.project, resp.body.id);
352+
if (resp) projectConfig.setLastJobId(resp.project, resp.id);
353353
return maybeTailLogs(err, resp);
354354
});
355355
}
@@ -363,7 +363,7 @@ function create(params, cb) {
363363
delete params.workspace;
364364
return method(create, params, function _methodCb(err, resp) {
365365
if (err) return cb(err);
366-
if (resp.body) projectConfig.setLastJobId(resp.body.project, resp.body.id);
366+
if (resp) projectConfig.setLastJobId(resp.project, resp.id);
367367
return maybeTailLogs(err, resp);
368368
});
369369
}

lib/jobs/logs.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,19 @@ var assign = require('lodash.assign');
3636
function parseLogs(data, allLogs, json) {
3737
var line = 0;
3838
var eof = false;
39-
data.body = []; // WARNING: will loose any real body if response type changes
40-
if (data.text && data.text.length) {
41-
try {
42-
data.body = JSON.parse(data.text);
43-
} catch (e) {
44-
console.error(e);
45-
data.body = [];
46-
}
47-
data.body.forEach(function itemFunc(item, index) {
48-
if (item.message) {
39+
if (data.length) {
40+
data.forEach(function itemFunc(item, index) {
41+
if (typeof item.message === 'string') {
4942
if (item.message.endsWith('\r')) item.message = item.message.slice(0, -1);
50-
if (item.message === 'PSEOF') {
51-
eof = true;
52-
data.body.splice(index, 1); // remove the PSEOF marker
53-
}
54-
else {
55-
if (item.line) line = item.line;
56-
if (global.paperspace_cli && !json) {
57-
console.log(item.message);
58-
} else if (allLogs) allLogs.push(item);
59-
}
43+
if (item.message === 'PSEOF') eof = true;
44+
if (item.line) line = item.line;
45+
if (global.paperspace_cli && !json && !eof) {
46+
console.log(item.message);
47+
} else if (allLogs) allLogs.push(item);
6048
} else if (item.line) line = item.line;
6149
});
6250
}
63-
return { count: data.body.length, line: line, eof: eof };
51+
return { count: data.length + 0, line: line, eof: eof };
6452
}
6553

6654
function logs(params, cb) {
@@ -115,7 +103,7 @@ function logs(params, cb) {
115103
if (global.paperspace_cli && !json) {
116104
return cb();
117105
}
118-
return cb(null, { body: allLogs, line: params.line, eof: result.eof } );
106+
return cb(null, allLogs);
119107
});
120108
}
121109

lib/jobs/waitfor.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ function waitfor(params, cb) {
7171
default:
7272
return cb(new Error('state must be either Pending, Running, Stopped, Failed, or Error'));
7373
}
74-
return method(waitfor, params, function _cb(err, resp) {
74+
return method(waitfor, params, function _cb(err, job) {
7575
if (err) {
7676
return cb(err);
7777
}
78-
var job = resp.body;
7978
if (job && (job.state === targetState || job.state === 'Error'
8079
|| (job.state === 'Stopped' && params.state === 'Failed')
8180
|| (job.state === 'Failed' && params.state === 'Stopped'))) {
82-
return cb(null, resp);
81+
return cb(null, job);
8382
}
8483
var interval = setTimeout(function _interval() {
8584
return waitfor(params, cb);

lib/machines/waitfor.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,12 @@ function waitfor(params, cb) {
8080
default:
8181
return cb(new Error('state must be either off or ready'));
8282
}
83-
return method(waitfor, params, function _cb(err, resp) {
83+
return method(waitfor, params, function _cb(err, machine) {
8484
if (err) {
8585
return cb(err);
8686
}
87-
var machine = resp.body;
8887
if (machine && machine.state === targetState) {
89-
return cb(null, resp);
88+
return cb(null, machine);
9089
}
9190
var interval = setTimeout(function _interval() {
9291
return waitfor(params, cb);

lib/request.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,25 @@ function request(
9898
}
9999

100100
return req.end(function _requestCallback(err, res) {
101-
if (cb) return cb(err, res);
101+
// if content is gzipped, then body is empty object, and content is in 'text'
102+
var body;
103+
if (res && res.text) {
104+
try {
105+
body = JSON.parse(res.text);
106+
} catch (parseErr) {
107+
console.error(parseErr);
108+
console.error('res: ' + res);
109+
body = {};
110+
}
111+
} else if (res.body) { // res.body is expected to be text/json
112+
body = res.body;
113+
} else body = {};
102114

103-
return res;
115+
// only return body in callback
116+
if (cb) return cb(err, body);
117+
118+
// no callback provided; return the body
119+
return body;
104120
});
105121
}
106122

0 commit comments

Comments
 (0)