mojio-js-sdk
Version:
Mojio javascript REST client.
128 lines (109 loc) • 4.28 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var FormUrlencoded, Http, HttpNodeWrapper, HttpWrapperHelper, Https, constants, iHttpWrapper,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Http = require('http');
Https = require('https');
FormUrlencoded = require('form-urlencoded');
constants = require('constants');
HttpWrapperHelper = require('../helpers/HttpWrapperHelper');
iHttpWrapper = require('../interfaces/iHttpWrapper');
module.exports = HttpNodeWrapper = (function(superClass) {
var _parts, _request;
extend(HttpNodeWrapper, superClass);
function HttpNodeWrapper(token1, uri1, encoding1, requester1) {
this.token = token1;
this.uri = uri1 != null ? uri1 : 'https://api.moj.io/v1';
this.encoding = encoding1 != null ? encoding1 : false;
this.requester = requester1 != null ? requester1 : null;
this.redirect = bind(this.redirect, this);
HttpNodeWrapper.__super__.constructor.call(this);
}
_request = function(params, requester, callback) {
var action;
if ((requester != null)) {
action = requester;
} else if (params.href.slice(0, "https".length) === "https") {
action = Https.request(params);
} else {
action = Http.request(params);
}
action.on('response', function(response) {
var data;
if (typeof window === "undefined" || window === null) {
response.setEncoding('utf8');
}
data = "";
response.on('data', function(chunk) {
if (chunk) {
return data += chunk;
}
});
return response.on('end', function() {
if (response.statusCode > 299) {
response.content = data;
return callback(response, null);
} else if (data.length > 0) {
return callback(null, JSON.parse(data));
} else {
return callback(null, {
result: "ok"
});
}
});
});
if ((params != null ? params.timeout : void 0) != null) {
action.on('socket', function(socket) {
socket.setTimeout(params.timeout);
return socket.on('timeout', function() {
return callback(socket, null);
});
});
}
action.on('error', function(error) {
return callback(error, null);
});
if ((params.body != null)) {
action.write(params.body);
}
return action.end();
};
_parts = function(request, token, uri, encoding) {
var parts;
if (request.id) {
request.pid = request.id;
}
if (request.key) {
request.sid = request.key;
}
uri += HttpWrapperHelper._getPath(request.resource, request.pid, request.action, request.sid, request.object, request.tid);
parts = HttpWrapperHelper._parse(uri, request, encoding, token);
return parts;
};
HttpNodeWrapper.prototype.url = function(request) {
var parts;
parts = _parts(request, this.token, this.uri, this.encoding);
return parts.protocol + '//' + parts.hostname + parts.pathname + parts.params;
};
HttpNodeWrapper.prototype.request = function(request, callback) {
var parts;
parts = _parts(request, this.token, this.uri, this.encoding);
console.log("REQUEST:" + this.url(request));
return _request(parts, this.requester, callback);
};
HttpNodeWrapper.prototype.redirect = function(request, redirectClass) {
if (redirectClass == null) {
redirectClass = null;
}
if ((redirectClass != null)) {
return redirectClass.redirect(this.url(request));
} else {
throw new Error("Please pass in the redirecting function of the framework used in nodejs");
}
};
return HttpNodeWrapper;
})(iHttpWrapper);
}).call(this);
//# sourceMappingURL=HttpWrapper.js.map