Skip to content

Instantly share code, notes, and snippets.

@k0sukey
Created August 14, 2015 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0sukey/831dc06bccffe479c857 to your computer and use it in GitHub Desktop.
Save k0sukey/831dc06bccffe479c857 to your computer and use it in GitHub Desktop.
Titanium Ti.Network.HTTPClient wrapper, looks like npm/request
// Inspired by npm/request
var _ = require('alloy/underscore')._;
function initParams(uri, options, callback) {
if (typeof options === 'function') {
callback = options;
}
var params = {};
if (typeof options === 'object') {
_.extend(params, options, {
uri: uri
});
} else if (typeof uri === 'string') {
_.extend(params, {
uri: uri
});
} else {
_.extend(params, uri);
}
params.callback = callback;
return params;
}
function request(uri, options, callback) {
if (typeof uri === 'undefined') {
throw new Error('undefined is not a valid uri or options object.');
}
var params = initParams(uri, options, callback);
return new request.Request(params);
}
function verbFunc(verb) {
var method = verb === 'del' ? 'DELETE' : verb.toUpperCase();
return function(uri, options, callback){
var params = initParams(uri, options, callback);
params.method = method;
return request(params, params.callback);
};
}
request.get = verbFunc('get');
request.head = verbFunc('head');
request.post = verbFunc('post');
request.put = verbFunc('put');
request.patch = verbFunc('patch');
request.del = verbFunc('del');
function Request(options) {
var that = this;
options = options || {};
var client = Ti.Network.createHTTPClient({
timeout: options.timeout || 5000
});
function cleanup() {
client.onload = null;
client.onerror = null;
client.onreadystatechange = null;
client.onsendstream = null;
client.ondatastream = null;
client = null;
}
function querystring(form) {
var result = [];
_.each(form, function(item, index){
result.push(encodeURIComponent(index) + '=' + encodeURIComponent(item));
});
return result.join('&');
}
client.onload = function(e){
if (!e.success || e.error) {
client.onerror(e);
}
if (_.isFunction(options.callback)) {
options.callback(e.error, {
statusCode: client.status,
headers: client.allResponseHeaders
}, this.responseText);
}
cleanup();
};
client.onerror = function(e){
if (_.isFunction(options.callback)) {
options.callback(e.error, {
statusCode: client.status,
headers: client.allResponseHeaders
}, this.responseText);
}
cleanup();
};
if (_.isObject(options.headers)) {
_.each(options.headers, function(item, index){
client.setRequestHeader(index, item);
});
}
if (options.method === 'GET' || options.method === 'HEAD') {
if (/\?/.test(options.uri)) {
if (/&$/.test(options.uri)) {
options.uri += querystring(options.form);
} else {
options.uri += '&' + querystring(options.form);
}
} else {
options.uri += '?' + querystring(options.form);
}
}
client.open(options.method, options.uri);
if (options.method === 'GET' || options.method === 'HEAD') {
client.send();
} else {
client.send(options.form);
}
}
module.exports = request;
request.Request = Request;
request.initParams = initParams;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment