From 567d61b920459f472c37c01a3fd9aa11fea6cc4b Mon Sep 17 00:00:00 2001 From: Jason David Miller Date: Sat, 30 Jan 2021 11:40:22 -0800 Subject: [PATCH 1/5] Add support for downloading remote images Adding additional expectingImage property to extraParams. --- ti.xhr.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ti.xhr.js b/ti.xhr.js index fd267d8..0eb5939 100644 --- a/ti.xhr.js +++ b/ti.xhr.js @@ -273,6 +273,7 @@ function addDefaultsToOptions(providedParams) { extraParams.contentType = extraParams.contentType || "application/json"; extraParams.parseJSON = (extraParams.hasOwnProperty('parseJSON')) ? extraParams.parseJSON : false; extraParams.returnXML = (extraParams.hasOwnProperty('returnXML')) ? extraParams.returnXML : false; + extraParams.expectingImage = (extraParams.hasOwnProperty('expectingImage')) ? extraParams.expectingImage : false; extraParams.debug = (extraParams.hasOwnProperty('debug')) ? extraParams.debug : false; extraParams.requestHeaders = providedParams.requestHeaders || []; return extraParams; @@ -292,6 +293,8 @@ function handleSuccess(xhr, extraParams) { try { if (extraParams.returnXML && xhr.responseXML) { result.data = xhr.responseXML; + } else if (extraParams.expectingImage) { + result.data = xhr.responseData; } else { result.data = extraParams.parseJSON ? JSON.parse(xhr.responseText) : xhr.responseText; } From 5fb7af00f4fd13a3d1772bd59ee2d33cc61f3981 Mon Sep 17 00:00:00 2001 From: Jason David Miller Date: Wed, 24 Feb 2021 11:11:01 -0800 Subject: [PATCH 2/5] Rename expectingImage to expectingFile Since responseData could be filetype other than image, let's use expectingFile instead (of expectingImage). --- ti.xhr.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ti.xhr.js b/ti.xhr.js index 0eb5939..5b51c35 100644 --- a/ti.xhr.js +++ b/ti.xhr.js @@ -273,7 +273,7 @@ function addDefaultsToOptions(providedParams) { extraParams.contentType = extraParams.contentType || "application/json"; extraParams.parseJSON = (extraParams.hasOwnProperty('parseJSON')) ? extraParams.parseJSON : false; extraParams.returnXML = (extraParams.hasOwnProperty('returnXML')) ? extraParams.returnXML : false; - extraParams.expectingImage = (extraParams.hasOwnProperty('expectingImage')) ? extraParams.expectingImage : false; + extraParams.expectingFile = (extraParams.hasOwnProperty('expectingFile')) ? extraParams.expectingFile : false; extraParams.debug = (extraParams.hasOwnProperty('debug')) ? extraParams.debug : false; extraParams.requestHeaders = providedParams.requestHeaders || []; return extraParams; @@ -293,7 +293,7 @@ function handleSuccess(xhr, extraParams) { try { if (extraParams.returnXML && xhr.responseXML) { result.data = xhr.responseXML; - } else if (extraParams.expectingImage) { + } else if (extraParams.expectingFile) { result.data = xhr.responseData; } else { result.data = extraParams.parseJSON ? JSON.parse(xhr.responseText) : xhr.responseText; From 5b267cc1eb3cc49dec99189f1a0c57fa1cd18b5a Mon Sep 17 00:00:00 2001 From: Jason David Miller Date: Wed, 24 Feb 2021 21:10:23 -0800 Subject: [PATCH 3/5] Add extraParams for error handling Add extraParams param to each method for error handling --- ti.xhr.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/ti.xhr.js b/ti.xhr.js index 5b51c35..dba5044 100644 --- a/ti.xhr.js +++ b/ti.xhr.js @@ -8,12 +8,12 @@ XHR = function() {}; // ================ // GET -// @e (object) url is required field. supports onSucces, onError and extraParams. +// @e (object) url is required field. supports onSucces, onError and extraParams. XHR.prototype.GET = function(e) { // Create some default params var onSuccess = e.onSuccess || function() {}; var onError = e.onError || function() {}; - + if (e.extraParams) { var extraParams = addDefaultsToOptions(e.extraParams); } else { @@ -40,7 +40,7 @@ XHR.prototype.GET = function(e) { // When there was an error xhr.onerror = function(err) { - onError(handleError(xhr, err)); + onError(handleError(xhr, err, extraParams)); }; xhr.send(); @@ -81,7 +81,8 @@ XHR.prototype.POST = function(e) { // When there was an error xhr.onerror = function(err) { // Check the status of this - onError(handleError(xhr, err)); + Ti.API.info('Ti.XHR :: 84'); + onError(handleError(xhr, err, extraParams)); }; xhr.send(extraParams.parseJSON ? JSON.stringify(e.data) : e.data); @@ -111,7 +112,7 @@ XHR.prototype.PUT = function(e) { // When there was an error xhr.onerror = function(err) { // Check the status of this - onError(handleError(xhr, err)); + onError(handleError(xhr, err, extraParams)); }; xhr.send(extraParams.parseJSON ? JSON.stringify(e.data) : e.data); @@ -141,7 +142,7 @@ XHR.prototype.PATCH = function(e) { // When there was an error xhr.onerror = function(err) { // Check the status of this - onError(handleError(xhr, err)); + onError(handleError(xhr, err, extraParams)); }; xhr.send(extraParams.parseJSON ? JSON.stringify(e.data) : e.data); @@ -170,7 +171,7 @@ XHR.prototype.DELETE = function(e) { // When there was an error xhr.onerror = function(err) { // Check the status of this - onError(handleError(xhr, err)); + onError(handleError(xhr, err, extraParams)); }; xhr.send(); @@ -296,9 +297,9 @@ function handleSuccess(xhr, extraParams) { } else if (extraParams.expectingFile) { result.data = xhr.responseData; } else { - result.data = extraParams.parseJSON ? JSON.parse(xhr.responseText) : xhr.responseText; + result.data = (extraParams.parseJSON) ? JSON.parse(xhr.responseText) : xhr.responseText; } - } catch(e) { + } catch (e) { result.data = xhr.responseData; } @@ -306,20 +307,23 @@ function handleSuccess(xhr, extraParams) { } // Return a standardized response -function handleError(xhr, error) { +function handleError(xhr, error, extraParams) { var result = {}; result.result = "error"; result.status = xhr.status; result.error = error.error; - + + Ti.API.info('Ti.XHR :: 317 :: extraParams.parseJSON:', extraParams.parseJSON); // Parse error result body try { if (extraParams.returnXML && xhr.responseXML) { result.data = xhr.responseXML; } else { - result.data = extraParams.parseJSON ? JSON.parse(xhr.responseText) : xhr.responseText; + Ti.API.info('Ti.XHR :: 322'); + result.data = (extraParams.parseJSON) ? JSON.parse(xhr.responseText) : xhr.responseText; } - } catch(e) { + } catch (e) { + Ti.API.info('Ti.XHR :: 326'); result.data = xhr.responseData; } return result; @@ -328,7 +332,7 @@ function handleError(xhr, error) { function initXHRRequest(method, url, extraParams) { // Create the HTTP connection var xhr = Titanium.Network.createHTTPClient({ - enableKeepAlive : false + enableKeepAlive: false }); // Open the HTTP connection @@ -336,7 +340,7 @@ function initXHRRequest(method, url, extraParams) { xhr.setRequestHeader('Content-Type', extraParams.contentType); // add extra provided request headers - if (extraParams.requestHeaders && extraParams.requestHeaders.length > 0){ + if (extraParams.requestHeaders && extraParams.requestHeaders.length > 0) { for (var i = 0; i < extraParams.requestHeaders.length; i++) { xhr.setRequestHeader(extraParams.requestHeaders[i].key, extraParams.requestHeaders[i].value); } @@ -421,7 +425,7 @@ function writeCache(data, url, ttl) { // Insert the cached object in the cache manager cacheManager[hashedURL] = { - "timestamp" : (new Date().getTime()) + (ttl * 60 * 1000) + "timestamp": (new Date().getTime()) + (ttl * 60 * 1000) }; updateCacheManager(); @@ -429,4 +433,4 @@ function writeCache(data, url, ttl) { }; // Return everything -module.exports = XHR; +module.exports = XHR; From 773da109289ea74839e8939706ca2451cf3b2545 Mon Sep 17 00:00:00 2001 From: Jason David Miller Date: Wed, 24 Feb 2021 21:11:00 -0800 Subject: [PATCH 4/5] Update ti.xhr.js --- ti.xhr.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/ti.xhr.js b/ti.xhr.js index dba5044..a719e0a 100644 --- a/ti.xhr.js +++ b/ti.xhr.js @@ -81,7 +81,6 @@ XHR.prototype.POST = function(e) { // When there was an error xhr.onerror = function(err) { // Check the status of this - Ti.API.info('Ti.XHR :: 84'); onError(handleError(xhr, err, extraParams)); }; @@ -319,11 +318,9 @@ function handleError(xhr, error, extraParams) { if (extraParams.returnXML && xhr.responseXML) { result.data = xhr.responseXML; } else { - Ti.API.info('Ti.XHR :: 322'); result.data = (extraParams.parseJSON) ? JSON.parse(xhr.responseText) : xhr.responseText; } } catch (e) { - Ti.API.info('Ti.XHR :: 326'); result.data = xhr.responseData; } return result; From 9f01fd36ed301f9d4d1fbf3961c1f1c6c738eca3 Mon Sep 17 00:00:00 2001 From: Jason David Miller Date: Wed, 24 Feb 2021 21:12:08 -0800 Subject: [PATCH 5/5] Update ti.xhr.js --- ti.xhr.js | 1 - 1 file changed, 1 deletion(-) diff --git a/ti.xhr.js b/ti.xhr.js index a719e0a..55f5e02 100644 --- a/ti.xhr.js +++ b/ti.xhr.js @@ -312,7 +312,6 @@ function handleError(xhr, error, extraParams) { result.status = xhr.status; result.error = error.error; - Ti.API.info('Ti.XHR :: 317 :: extraParams.parseJSON:', extraParams.parseJSON); // Parse error result body try { if (extraParams.returnXML && xhr.responseXML) {