UNPKG

zeysdk

Version:
382 lines (331 loc) 9.29 kB
var mod = {}; var fs = require('fs'); var help = require('./_help.js'); var http = require('http'); var https = require('https'); var path = require('path'); var qs = require('querystring'); var series = require('agraddy.async.series'); var zlib = require('zlib'); mod.delete = {}; mod.get = {}; mod.patch = {}; mod.post = {}; mod.put = {}; mod.delete.sync = function(url, app, token, cb) { var base = getBase(url); var options = {}; var output = ''; var protocol = getProtocol(url); var req; options.host = getHost(url); options.path = base + 'sdk/v1/sync/' + encodeURIComponent(app); options.method = 'DELETE'; options.headers = { 'Authorization': 'Bearer ' + token, 'Accept-Encoding': 'gzip' }; req = protocol.request(options, function(res) { var original_res = res; if(res.headers['content-encoding'] == 'gzip') { res = res.pipe(zlib.createGunzip()); } res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk.toString(); }); res.on('end', function () { res = original_res; if(res.statusCode == 204) { output = true; cb(null, output); } else if(res.statusCode == 403) { cb(new Error('You do not appear to have the proper permission to delete this app.')); } else if(res.statusCode == 404) { cb(new Error('The app does not exist on the server. Please rerun zeysdk link to recreate the app on the server.')); } else if(res.statusCode == 500) { cb(new Error(output)); } else { //cb(JSON.stringify(res.headers, null, 2)); cb(res.statusCode); } }); }); req.on('error', function(err) { cb(new Error('There was a timeout or some other problem communicating with the server.')); }); req.end(); }; mod.get.sync = function(url, app, token, cb) { var base = getBase(url); var options = {}; var output = ''; var protocol = getProtocol(url); var req; options.host = getHost(url); options.path = base + 'sdk/v1/sync/' + encodeURIComponent(app); options.method = 'GET'; options.headers = { 'Authorization': 'Bearer ' + token, 'Accept-Encoding': 'gzip' }; req = protocol.request(options, function(res) { var original_res = res; if(res.headers['content-encoding'] == 'gzip') { res = res.pipe(zlib.createGunzip()); } res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk.toString(); }); res.on('end', function () { res = original_res; if(res.statusCode == 200) { try { output = JSON.parse(output); cb(null, output); } catch(err) { cb(new Error('There was a problem parsing the JSON returned by the server.')); } } else if(res.statusCode == 404) { output = false; cb(null, output); } else if(res.statusCode == 500) { cb(new Error(output)); } else { //cb(JSON.stringify(res.headers, null, 2)); cb(res.statusCode); } }); }); req.on('error', function(err) { cb(new Error('There was a timeout or some other problem communicating with the server.')); }); req.end(); }; mod.patch.sync = function(url, app, token, json, cb) { var base = getBase(url); var output = ''; var options = {}; var protocol = getProtocol(url); var req; options.host = getHost(url); options.path = base + 'sdk/v1/sync/' + encodeURIComponent(app); options.method = 'PATCH'; options.headers = { 'Authorization': 'Bearer ' + token, 'Content-type': 'application/json', //'Content-Encoding': 'gzip', 'Accept-Encoding': 'gzip' }; req = protocol.request(options, function(res) { var original_res = res; if(res.headers['content-encoding'] == 'gzip') { res = res.pipe(zlib.createGunzip()); } res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk.toString(); }); res.on('end', function () { res = original_res; if(res.statusCode == 200) { try { output = JSON.parse(output); cb(null, output); } catch(err) { cb(new Error('There was a problem parsing the JSON returned by the server.')); } } else if(res.statusCode == 404) { cb(new Error('The app does not exist on the server. Please rerun zeysdk link to recreate the app on the server.')); } else if(res.statusCode == 500) { cb(new Error(output)); } else { //console.log(output); //cb(JSON.stringify(res.headers, null, 2)); cb(res.statusCode); } }); }); req.on('error', function(err) { cb(new Error('There was a timeout or some other problem communicating with the server.')); }); req.write(JSON.stringify(json)); req.end(); //zlib.gzip(JSON.stringify(json), function (err, buffer) { //req.write(buffer); //req.end(); //}); }; mod.post.login = function(url, user, password, identifier, cb) { var base = getBase(url); var data = ''; var output = ''; var options = {}; var protocol = getProtocol(url); var req; var uuidv4 = require('uuid/v4'); data = qs.stringify({ name: user, password: password, identifier: 'ZeySDK_' + identifier + '_' + uuidv4() }); options.host = getHost(url); options.path = base + 'auth/v1/login'; options.method = 'POST'; options.headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(data), 'Accept-Encoding': 'gzip' }; req = protocol.request(options, function(res) { var original_res = res; if(res.headers['content-encoding'] == 'gzip') { res = res.pipe(zlib.createGunzip()); } res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk.toString(); }); res.on('end', function () { res = original_res; if(res.statusCode == 200) { try { output = JSON.parse(output); cb(null, output); } catch(err) { cb(new Error('There was a problem parsing the JSON returned by the server.')); } } else if(res.statusCode == 500) { cb(new Error(output)); } else { cb(new Error('There was a problem logging in.')); } }); }); req.on('error', function(err) { //console.log('error'); cb(new Error('There was a problem logging in.')); }); req.write(data); req.end(); }; mod.put.run = function(url, app, token, identifier, mimetype, file, cb) { var base = getBase(url); var output = ''; var options = {}; var protocol = getProtocol(url); var req; var stream; options.host = getHost(url); options.path = base + 'sdk/v1/run' + '?' + qs.stringify({ identifier: identifier, appidentifier: app }); options.method = 'PUT'; options.headers = { 'Authorization': 'Bearer ' + token, 'Content-Type': mimetype, 'Accept-Encoding': 'gzip' }; req = protocol.request(options, function(res) { var original_res = res; if(res.headers['content-encoding'] == 'gzip') { res = res.pipe(zlib.createGunzip()); } res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk.toString(); }); res.on('end', function () { res = original_res; if(res.statusCode == 200) { try { output = JSON.parse(output); cb(null, output); } catch(err) { cb(new Error('There was a problem parsing the JSON returned by the server.')); } } else if(res.statusCode == 500) { cb(new Error(output)); } else { //cb(JSON.stringify(res.headers, null, 2)); cb(res.statusCode); } }); }); req.on('error', function(err) { cb(new Error('There was a timeout or some other problem communicating with the server.')); }); stream = fs.createReadStream(file).pipe(req); stream.on('finish', req.end); }; mod.put.sync = function(url, app, token, json, cb) { var base = getBase(url); var output = ''; var options = {}; var protocol = getProtocol(url); var req; options.host = getHost(url); options.path = base + 'sdk/v1/sync/' + encodeURIComponent(app); options.method = 'PUT'; options.headers = { 'Authorization': 'Bearer ' + token, 'Content-type': 'application/json', 'Accept-Encoding': 'gzip' }; req = protocol.request(options, function(res) { var original_res = res; if(res.headers['content-encoding'] == 'gzip') { res = res.pipe(zlib.createGunzip()); } res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk.toString(); }); res.on('end', function () { res = original_res; if(res.statusCode == 201) { try { output = JSON.parse(output); cb(null, output); } catch(err) { cb(new Error('There was a problem parsing the JSON returned by the server.')); } } else if(res.statusCode == 500) { cb(new Error(output)); } else { //console.log(output); //cb(JSON.stringify(res.headers, null, 2)); cb(res.statusCode); } }); }); req.on('error', function(err) { cb(new Error('There was a timeout or some other problem communicating with the server.')); }); req.write(JSON.stringify(json)); req.end(); }; function getBase(url) { url = url.replace(/^https?:\/\//, ''); // Remove everything until the first slash url = url.replace(/[^\/]*/, ''); // Remove any trailing slashes and then add one slash url = url.replace(/\/*$/, '/'); return url; } function getHost(url) { url = url.replace(/^https?:\/\//, ''); // Remove everything from first slash onward url = url.replace(/\/.*/, ''); return url; } function getProtocol(url) { if(url.indexOf('https') === 0) { return https; } else { return http; } } module.exports = mod;