Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a get prototype

  • Loading branch information...
commit ed14c12399d582711383a786b6291fdfa60de160 1 parent 5cfe0c5
@waynegraham authored
Showing with 66 additions and 8 deletions.
  1. +66 −8 lib/main.js
View
74 lib/main.js
@@ -1,15 +1,73 @@
var http = require('http'),
- querystring = require('querystring'),
- MethodTable = (JSON.parse(require('fs').readFileSync(__dirname + 'methods.json'))).results;
+ querystring = require('querystring');
-function Zotero(api_key) {
+function Zotero(options) {
- if (typeof api_key == 'string') {
- this.api_key = api_key;
- } else {
- this.api_key = null;
- }
+ if (!(this instanceof Zotero)) return new Zotero(options);
+
+ var defaults = {
+ api_key = null,
+ // TODO get documentation defaults
+
+ headers: {
+ 'Accept' : '*/*',
+ 'Connection' : 'close',
+ 'User-Agent' : 'node-zotero';
+ },
+
+ calback_url: null,
+
+ rest_base: 'https://api.zotero.org/',
+
+ secure: false, // force use of https for login
+ cookie: 'zauth',
+ cookie_options: {},
+ cookie_secret: null
+ };
+
+ this.options = merge(defaults, options);
+
+// keygrip?
+ //
+ // oauth?
}
module.exports = zotero;
+
+Zotero.prototype.get = function(url, params, callback) {
+ if (typeof params === 'function') {
+ callback = params;
+ params = null;
+ }
+
+ if (typeof callback !== 'function') {
+ throw "Fail: Invalid Callback";
+ return this;
+ }
+
+ if (url.charAt(0) == '/') {
+ url = this.options.rest_base + url;
+ }
+
+ function(error, data, response) {
+ if (error) {
+ var err = new Error('HTTP Error '
+ + error.statusCode + ': '
+ + http.STATUS_CODE[error.statusCode]);
+ err.statusCode = error.statusCode;
+ err.data = error.data;
+ callback(err);
+ } else {
+ try {
+ var json = JSON.parse(data);
+ callback(json);
+ } catch(err) {
+ callback(err);
+ }
+ }
+ }
+ });
+
+ return this;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.