jasonkneen / http.js secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

http.js

View http.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
var Alloy = require('alloy');
 
function Api(url, cacheable) {
this.setUrl(url);
this.setCacheable(!!cacheable);
}
 
Api.prototype = {
setUrl : function(url) {
this._url = url;
},
getUrl : function() {
return this._url;
},
setCacheable : function(cacheable) {
this._cacheable = cacheable;
},
getCacheable : function() {
return this._cacheable;
},
get : function(callback) {
this.createHttpRequest(this.getUrl(), 'GET', null, callback);
},
post : function(data, callback) {
this.createHttpRequest(this.getUrl(), 'POST', data, callback);
},
put : function(id, data, callback) {
this.createHttpRequest(this.getUrl() + id, 'PUT', data, callback);
},
destroy : function() {
this.createHttpRequest(this.getUrl() + id, 'DELETE', data, callback);
},
setRequestHeaders : function(http) {
http.setRequestHeader('Content-Type', 'application/json');
},
createHttpRequest : function(url, method, params, callback) {
var http = Ti.Network.createHTTPClient();
 
// this.setRequestHeaders(http);
 
//console.log(method);
console.log("XXX " + url);
//console.log(params);
 
http.setTimeout(6000);
 
http.open(method, url);
 
// http.setRequestHeader('Authorization', Alloy.CFG.authorization);
 
http.setRequestHeader("Content-Type", "application/json");
 
http.onload = function(e) {
// alert(this.responseData);
// Ti.API.info(this.responseText);
callback(JSON.parse(this.responseText));
};
 
http.onerror = function(e) {
//callback(JSON.parse(this.responseText));
var response = this.responseText;
 
console.log("ERROR: " + response);
 
callback({
result : {
error : "error",
response : response
}
});
 
//if (response.error_message) {
// console.log(response.error_message);
// }
};
 
if (params) {
http.send(params);
} else {
http.send();
}
}
};
 
module.exports = Api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.