Skip to content

Instantly share code, notes, and snippets.

@faabiosr
Created October 15, 2012 01:37
Show Gist options
  • Save faabiosr/3890404 to your computer and use it in GitHub Desktop.
Save faabiosr/3890404 to your computer and use it in GitHub Desktop.
RestClient - Titanium HttpClient with API key and callback
function RestClient(apiKey){
this.apiKey = apiKey;
this.get = function(url,callback){
var request = Ti.Network.createHTTPClient({
timeout: 20000
});
request.open('GET',url,false);
request.setRequestHeader('X-API-KEY',this.apiKey);
request.onload = function(){
callback(JSON.parse(this.responseText));
};
request.onerror = function(e){
callback(e);
}
request.send();
}
this.post = function(url,data,callback){
var request = Ti.Network.createHTTPClient({
timeout: 20000
});
request.open('POST',url,false);
request.setRequestHeader('X-API-KEY',this.apiKey);
request.onload = function(){
callback(JSON.parse(this.responseText));
};
request.onerror = function(e){
callback(e);
}
request.send(data);
}
return this;
}
module.exports = RestClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment