Last active
November 11, 2016 02:11
-
-
Save yomybaby/9ceb4a4e79a80c7607cb598d4f5c1a1b to your computer and use it in GitHub Desktop.
promise httpRequest using Q on Titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qHttp({ | |
url: 'YOUR_URL', | |
method: "POST", | |
data: { | |
"access_token" : 'YOUR_TOKEN' | |
} | |
}).then(function (args){ | |
// step 2 : other request | |
return qHttp({ | |
}) | |
}).then(function (args){ | |
// args from step2 http request | |
}).catch(function () { | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Q = require('q'); | |
var httpRequest = (function() { | |
function encodeData(obj, url) { | |
var str = [], value; | |
for (var p in obj) { | |
value = _.isObject(obj[p])?JSON.stringify(obj[p]):obj[p]; | |
str.push(Ti.Network.encodeURIComponent(p) + "=" + Ti.Network.encodeURIComponent(value)); | |
} | |
if (_.indexOf(url, "?") == -1) { | |
return url + "?" + str.join("&"); | |
} else { | |
return url + "&" + str.join("&"); | |
} | |
} | |
return function(args) { | |
var defer, | |
tiHttpClient; | |
var url = args.url; | |
var data = args.data; | |
var method = args.method || 'GET'; | |
var headers = args.headers; | |
defer = Q.defer(); | |
tiHttpClient = Ti.Network.createHTTPClient({ | |
onload: function() { | |
var parsedData; | |
var error; | |
try { | |
parsedData = JSON.parse(this.responseText || "{}"); | |
} catch (e) { | |
error = e; | |
} | |
if (error) { | |
defer.reject({ | |
status: this.status, | |
data: parsedData, | |
source: this | |
}); | |
} else { | |
defer.resolve({ | |
status: this.status, | |
data: parsedData, | |
source : this, | |
date : this.getResponseHeader('Date') | |
}); | |
} | |
}, | |
onerror: function(e) { | |
var parsedData; | |
try { | |
parsedData = JSON.parse(this.responseText || "{}"); | |
} catch (e) { | |
// error = e; | |
} | |
defer.reject({ | |
status : this.status, | |
data : parsedData, | |
source : this | |
}); | |
}, | |
onsendstream: function(e) { | |
defer.notify(e); | |
}, | |
timeout: 10000 | |
}); | |
if (method == 'GET' && data) { | |
url = encodeData(data, url); | |
} | |
tiHttpClient.open(method, url, true); | |
var sendData; | |
if (method == 'POST' || method == 'PUT') { | |
tiHttpClient.setRequestHeader('Content-Type', "application/json; charset=utf-8"); | |
sendData = (typeof data === 'object') ? JSON.stringify(data) : data; | |
} | |
_.each(headers, function(value, key) { | |
tiHttpClient.setRequestHeader(key, value); | |
}); | |
tiHttpClient.send(sendData); | |
return defer.promise; | |
}; | |
})(); | |
exports.httpRequest = httpRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment