Skip to content

Commit 982fc72

Browse files
committed
Adds HTTPResponse object and lazy loading text and JSON
1 parent f40efe3 commit 982fc72

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/cloud-code/HTTPResponse.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
export default class HTTPResponse {
3+
constructor(response) {
4+
this.status = response.statusCode;
5+
this.headers = response.headers;
6+
this.buffer = response.body;
7+
this.cookies = response.headers["set-cookie"];
8+
}
9+
10+
get text() {
11+
return this.buffer.toString('utf-8');
12+
}
13+
get data() {
14+
if (!this._data) {
15+
try {
16+
this._data = JSON.parse(this.text);
17+
} catch (e) {}
18+
}
19+
return this._data;
20+
}
21+
}

src/cloud-code/httpRequest.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var request = require("request"),
22
querystring = require('querystring'),
33
Parse = require('parse/node').Parse;
4+
HTTPResponse = require('./HTTPResponse').HTTPResponse;
45

56
var encodeBody = function(options = {}) {
67
let body = options.body;
@@ -62,15 +63,8 @@ module.exports = function(options) {
6263
}
6364
return promise.reject(error);
6465
}
65-
var httpResponse = {};
66-
httpResponse.status = response.statusCode;
67-
httpResponse.headers = response.headers;
68-
httpResponse.buffer = response.body;
69-
httpResponse.cookies = response.headers["set-cookie"];
70-
httpResponse.text = response.body.toString('utf-8');
71-
try {
72-
httpResponse.data = JSON.parse(httpResponse.text);
73-
} catch (e) {}
66+
let httpResponse = new HTTPResponse(response);
67+
7468
// Consider <200 && >= 400 as errors
7569
if (httpResponse.status < 200 || httpResponse.status >= 400) {
7670
if (callbacks.error) {

0 commit comments

Comments
 (0)