diff --git a/lib/feedly.js b/lib/feedly.js index f6db633..01a12bf 100644 --- a/lib/feedly.js +++ b/lib/feedly.js @@ -205,6 +205,29 @@ }); }; + Feedly.prototype._request2 = function(callback, path, method, body) { + var u; + if (method == null) { + method = 'GET'; + } + if (body == null) { + body = null; + } + u = url.parse(this.options.base); + u.pathname = path; + return this._getAuth().then(function(auth) { + return utils.qrequest2({ + method: method, + uri: url.format(u), + headers: { + Authorization: "OAuth " + auth + }, + body: body, + callback: callback + }); + }); + }; + Feedly.prototype._requestURL = function(callback, path, method, body) { var u; if (method == null) { @@ -628,6 +651,17 @@ })(this)); }; + Feedly.prototype.OPML = function(cb) { + return this._request(cb, '/v3/opml'); + }; + + Feedly.prototype.importOPML = function(opml, cb) { + if ((opml == null) || (typeof opml === 'function')) { + throw new Error("opml required"); + } + return this._request2(cb, '/v3/opml', 'POST', opml); + }; + return Feedly; })(); diff --git a/lib/utils.js b/lib/utils.js index 408edb6..9a9e029 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -88,6 +88,29 @@ return d.promise; }; + exports.qrequest2 = function(options) { + var cb, d; + if (options == null) { + throw new Error("options not optional"); + } + d = q.defer(); + cb = options.callback; + if (cb != null) { + delete options.callback; + d.promise.nodeify(cb); + } + request(options, function(er, res, body) { + if (er != null) { + return d.reject(er); + } else if (res.statusCode !== 200) { + return d.reject(new Error("HTTP error: " + res.statusCode + "\nFrom: " + options.uri + "\n" + (JSON.stringify(body)))); + } else { + return d.resolve(body); + } + }); + return d.promise; + }; + if (!Array.prototype.find) { Object.defineProperty(Array.prototype, "find", { enumerable: false,