Created
October 15, 2012 01:37
-
-
Save faabiosr/3890404 to your computer and use it in GitHub Desktop.
RestClient - Titanium HttpClient with API key and callback
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
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