DRY up Promise creation in api.js

Since calls to ajax() are always wrapped in promises, we can internalize
that pattern in the ajax function itself.

// FREEBIE
This commit is contained in:
lilia 2015-07-13 11:52:49 -07:00
parent 3711e0a6cd
commit b40a8696b7
2 changed files with 166 additions and 212 deletions

View file

@ -38998,6 +38998,7 @@ TextSecureServer = function () {
* jsonData: JSON data sent in the request body * jsonData: JSON data sent in the request body
*/ */
function ajax(url, options) { function ajax(url, options) {
return new Promise(function (resolve, reject) {
var error = new Error(); // just in case, save stack here. var error = new Error(); // just in case, save stack here.
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open(options.type, url, true /*async*/); xhr.open(options.type, url, true /*async*/);
@ -39022,15 +39023,16 @@ TextSecureServer = function () {
try { result = JSON.parse(xhr.responseText + ''); } catch(e) {} try { result = JSON.parse(xhr.responseText + ''); } catch(e) {}
} }
if ( 0 <= xhr.status && xhr.status < 400) { if ( 0 <= xhr.status && xhr.status < 400) {
options.success(result, xhr.status); resolve(result, xhr.status);
} else { } else {
options.error(HTTPError(xhr.status, result, error.stack)); reject(HTTPError(xhr.status, result, error.stack));
} }
}; };
xhr.onerror = function() { xhr.onerror = function() {
options.error(HTTPError(xhr.status, null, error.stack)); reject(HTTPError(xhr.status, null, error.stack));
}; };
xhr.send( options.data || null ); xhr.send( options.data || null );
});
} }
function HTTPError(code, response, stack) { function HTTPError(code, response, stack) {
@ -39057,22 +39059,19 @@ TextSecureServer = function () {
param.password = textsecure.storage.get("password"); param.password = textsecure.storage.get("password");
} }
return new Promise(function (resolve, reject) { return ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
type : param.httpType, type : param.httpType,
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData), data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
contentType : 'application/json; charset=utf-8', contentType : 'application/json; charset=utf-8',
dataType : 'json', dataType : 'json',
user : param.user, user : param.user,
password : param.password, password : param.password
success : resolve, }).catch(function(e) {
error : function(e) {
var code = e.code; var code = e.code;
if (code === 200) { if (code === 200) {
// happens sometimes when we get no response // happens sometimes when we get no response
// (TODO: Fix server to return 204? instead) // (TODO: Fix server to return 204? instead)
resolve(null); return null;
return;
} }
var message; var message;
switch (code) { switch (code) {
@ -39100,9 +39099,7 @@ TextSecureServer = function () {
message = "The server rejected our query, please file a bug report."; message = "The server rejected our query, please file a bug report.";
} }
e.message = message e.message = message
reject(e); throw e;
}
});
}); });
}; };
@ -39239,18 +39236,10 @@ TextSecureServer = function () {
urlParameters : '/' + id, urlParameters : '/' + id,
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return ajax(response.location, {
ajax(response.location, {
type : "GET", type : "GET",
responseType: "arraybuffer", responseType: "arraybuffer",
contentType: "application/octet-stream", contentType : "application/octet-stream"
success : resolve,
error : function(e) {
e.message = 'Failed to download attachment';
reject(e);
}
});
}); });
}); });
}; };
@ -39262,27 +39251,15 @@ TextSecureServer = function () {
httpType : 'GET', httpType : 'GET',
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return ajax(response.location, {
ajax(response.location, {
type : "PUT", type : "PUT",
contentType : "application/octet-stream", contentType : "application/octet-stream",
data : encryptedBin, data : encryptedBin,
processData : false, processData : false,
success : function() { }).then(function() {
try {
// Parse the id as a string from the location url // Parse the id as a string from the location url
// (workaround for ids too large for Javascript numbers) // (workaround for ids too large for Javascript numbers)
var id = response.location.match(id_regex)[1]; return response.location.match(id_regex)[1];
resolve(id);
} catch(e) {
reject(e);
}
},
error : function(e) {
e.message = 'Failed to upload attachment';
reject(e);
}
});
}); });
}); });
}; };

View file

@ -53,6 +53,7 @@ TextSecureServer = function () {
* jsonData: JSON data sent in the request body * jsonData: JSON data sent in the request body
*/ */
function ajax(url, options) { function ajax(url, options) {
return new Promise(function (resolve, reject) {
var error = new Error(); // just in case, save stack here. var error = new Error(); // just in case, save stack here.
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open(options.type, url, true /*async*/); xhr.open(options.type, url, true /*async*/);
@ -77,15 +78,16 @@ TextSecureServer = function () {
try { result = JSON.parse(xhr.responseText + ''); } catch(e) {} try { result = JSON.parse(xhr.responseText + ''); } catch(e) {}
} }
if ( 0 <= xhr.status && xhr.status < 400) { if ( 0 <= xhr.status && xhr.status < 400) {
options.success(result, xhr.status); resolve(result, xhr.status);
} else { } else {
options.error(HTTPError(xhr.status, result, error.stack)); reject(HTTPError(xhr.status, result, error.stack));
} }
}; };
xhr.onerror = function() { xhr.onerror = function() {
options.error(HTTPError(xhr.status, null, error.stack)); reject(HTTPError(xhr.status, null, error.stack));
}; };
xhr.send( options.data || null ); xhr.send( options.data || null );
});
} }
function HTTPError(code, response, stack) { function HTTPError(code, response, stack) {
@ -112,22 +114,19 @@ TextSecureServer = function () {
param.password = textsecure.storage.get("password"); param.password = textsecure.storage.get("password");
} }
return new Promise(function (resolve, reject) { return ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
type : param.httpType, type : param.httpType,
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData), data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
contentType : 'application/json; charset=utf-8', contentType : 'application/json; charset=utf-8',
dataType : 'json', dataType : 'json',
user : param.user, user : param.user,
password : param.password, password : param.password
success : resolve, }).catch(function(e) {
error : function(e) {
var code = e.code; var code = e.code;
if (code === 200) { if (code === 200) {
// happens sometimes when we get no response // happens sometimes when we get no response
// (TODO: Fix server to return 204? instead) // (TODO: Fix server to return 204? instead)
resolve(null); return null;
return;
} }
var message; var message;
switch (code) { switch (code) {
@ -155,9 +154,7 @@ TextSecureServer = function () {
message = "The server rejected our query, please file a bug report."; message = "The server rejected our query, please file a bug report.";
} }
e.message = message e.message = message
reject(e); throw e;
}
});
}); });
}; };
@ -294,18 +291,10 @@ TextSecureServer = function () {
urlParameters : '/' + id, urlParameters : '/' + id,
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return ajax(response.location, {
ajax(response.location, {
type : "GET", type : "GET",
responseType: "arraybuffer", responseType: "arraybuffer",
contentType: "application/octet-stream", contentType : "application/octet-stream"
success : resolve,
error : function(e) {
e.message = 'Failed to download attachment';
reject(e);
}
});
}); });
}); });
}; };
@ -317,27 +306,15 @@ TextSecureServer = function () {
httpType : 'GET', httpType : 'GET',
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return ajax(response.location, {
ajax(response.location, {
type : "PUT", type : "PUT",
contentType : "application/octet-stream", contentType : "application/octet-stream",
data : encryptedBin, data : encryptedBin,
processData : false, processData : false,
success : function() { }).then(function() {
try {
// Parse the id as a string from the location url // Parse the id as a string from the location url
// (workaround for ids too large for Javascript numbers) // (workaround for ids too large for Javascript numbers)
var id = response.location.match(id_regex)[1]; return response.location.match(id_regex)[1];
resolve(id);
} catch(e) {
reject(e);
}
},
error : function(e) {
e.message = 'Failed to upload attachment';
reject(e);
}
});
}); });
}); });
}; };