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

View file

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