UNPKG

dormouse

Version:

Javascript API for Dormouse

187 lines (162 loc) 4.86 kB
var Connection, appendAPIKey, handleResponse, http, libutils, parseResponse, path, successful_statuses, _, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; path = require('path'); _ = require('underscore'); http = require('http-browserify'); libutils = require('./libutils'); Connection = (function() { var api_key, host, port; function Connection() {} Connection.get = function(get_path, options, callback) { var req; if (typeof options === 'function') { callback = options; options = {}; } get_path = libutils.formatUrl({ path: get_path, query: appendAPIKey(options) }); req = http.request({ method: 'GET', host: Connection.host(), port: Connection.port(), path: get_path }, function(res) { return handleResponse(res, callback); }); return req.end(); }; Connection.post = function(post_path, options, body, callback) { var raw_body, raw_length, req; post_path = libutils.formatUrl({ path: post_path, query: appendAPIKey(options) }); raw_body = JSON.stringify(body); raw_length = typeof Buffer !== "undefined" && Buffer !== null ? Buffer.byteLength(raw_body) : raw_body.length; req = http.request({ method: 'POST', host: Connection.host(), port: Connection.port(), path: post_path, headers: { 'Content-Type': 'application/json', 'Content-Length': raw_length } }, function(res) { return handleResponse(res, callback); }); return req.end(raw_body); }; Connection.put = function(put_path, options, body, callback) { var raw_body, raw_length, req; put_path = libutils.formatUrl({ path: put_path, query: appendAPIKey(options) }); raw_body = JSON.stringify(body); raw_length = typeof Buffer !== "undefined" && Buffer !== null ? Buffer.byteLength(raw_body) : raw_body.length; req = http.request({ method: 'PUT', host: Connection.host(), port: Connection.port(), path: put_path, headers: { 'Content-Type': 'application/json', 'Content-Length': raw_length } }, function(res) { return handleResponse(res, callback); }); return req.end(raw_body); }; Connection["delete"] = function(delete_path, options, callback) { var req; delete_path = libutils.formatUrl({ path: delete_path, query: appendAPIKey(options) }); req = http.request({ method: 'DELETE', host: Connection.host(), port: Connection.port(), path: delete_path }, function(res) { return handleResponse(res, callback); }); return req.end(); }; host = 'dormou.se'; port = 80; Connection.server = function(setter) { var matched; if (setter) { matched = setter.match(/^((https?):\/\/)?([A-Za-z0-9\.]+)(:(\d+))?\/?$/); if (matched) { host = matched[3] || 'dormou.se'; port = matched[5] || 80; } else { throw new Error('Improperly formatted url passed to Dormouse.server(...)'); } } return "http://" + host + ":" + port + "/"; }; Connection.host = function(setter) { if (setter) host = setter; return host; }; Connection.port = function(setter) { if (setter) port = setter; return port; }; api_key = ''; Connection.api_key = function(setter) { if (setter) api_key = setter; if (!api_key) { throw new Error('You cannot make API calls without an api_key. Set it using Dormouse.api_key(...)'); } return api_key; }; return Connection; })(); appendAPIKey = function(options) { return _.extend(options, { api_key: Connection.api_key() }); }; handleResponse = function(res, callback) { var data; data = ''; res.on('data', function(buf) { return data += buf; }); res.on('end', function() { if (callback) return parseResponse(res, data, callback); }); return res.on('error', function(err) { return console.log('HTTP error', res.statusCode, data, err); }); }; successful_statuses = [200, 201, 202]; parseResponse = function(res, raw_response, callback) { var _ref; raw_response = raw_response.trim(); if (_ref = res.statusCode, __indexOf.call(successful_statuses, _ref) >= 0) { if (raw_response) { try { return callback(null, JSON.parse(raw_response)); } catch (err) { if (console) return console.error('Response JSON parsing error', err); } } else { return callback(null, { success: true }); } } else { if (console) console.info('Request failed', raw_response); return callback(new Error(raw_response), null); } }; exports.Connection = Connection;