From 3c4fcc6ede1c7d7978d454c37a609b4863124369 Mon Sep 17 00:00:00 2001 From: "@iamcoder" Date: Fri, 26 Sep 2014 23:17:21 -0400 Subject: [PATCH 01/10] error when validate sender status is false there is a problem when the call to obtain the status of a sender return false, the callback for the promise is never reached because the errorCase function is throwing an exception that never is catched with my changes now no matter the result of the status, the respond is always reached function to validate the new changes propossed ++++++++++++++++ function isAvalidSender(email) { var result = dyn.senders.status(email).then(function(x) { console.log('CALLBACK!!!'); console.log('response %s', JSON.stringify(x)); if (x) { return true; } return false; }); } --- src/dyn-js.coffee | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dyn-js.coffee b/src/dyn-js.coffee index 026b17e..618e5b3 100644 --- a/src/dyn-js.coffee +++ b/src/dyn-js.coffee @@ -217,8 +217,11 @@ callWithError = (funProm, description, successFilter, successCase, errorCase) -> log.silly 'dyn', "api call returned successfully : #{JSON.stringify(x[1])}" successCase(x[1]) else + try log.info 'dyn', "api call returned error : #{JSON.stringify(x[1])}" errorCase x[1] + catch error + log.warn error , (x) -> log.warn 'dyn', "unexpected error : #{JSON.stringify(x[1])}" errorCase x @@ -248,7 +251,7 @@ extractZones = (x) -> {zone:v[3]} throwMessages = (x) -> throw (x.msgs || "unknown exception when calling api") -throwMsgMessages = (x) -> throw (x?.response?.message || "unknown exception when calling api") +throwMsgMessages = (x) -> return x?.response?.message || "unknown exception when calling api" Dyn = (opts) -> traffic_defaults = _.defaults opts?.traffic || {}, { From 8d77f6c38d7faffb4115b8a255f11733f947ad5b Mon Sep 17 00:00:00 2001 From: "@iamcoder" Date: Fri, 26 Sep 2014 23:20:58 -0400 Subject: [PATCH 02/10] remove try catch I forgot to remove try catch --- src/dyn-js.coffee | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/dyn-js.coffee b/src/dyn-js.coffee index 618e5b3..66756ad 100644 --- a/src/dyn-js.coffee +++ b/src/dyn-js.coffee @@ -157,7 +157,7 @@ crudRecipients = (type) -> _status: {path:"/recipients/status", method: "GET"} crudSendMail = (type) -> - crudMessaging "/send/", + crudMessaging "/send/", _create: {path:"/send"} crudSuppressions = (type) -> @@ -217,11 +217,10 @@ callWithError = (funProm, description, successFilter, successCase, errorCase) -> log.silly 'dyn', "api call returned successfully : #{JSON.stringify(x[1])}" successCase(x[1]) else - try + log.info 'dyn', "api call returned error : #{JSON.stringify(x[1])}" errorCase x[1] - catch error - log.warn error + , (x) -> log.warn 'dyn', "unexpected error : #{JSON.stringify(x[1])}" errorCase x From c9f9ad1aa222da2482a595ee5165938f5aaeca0a Mon Sep 17 00:00:00 2001 From: "@iamcoder" Date: Fri, 26 Sep 2014 23:32:02 -0400 Subject: [PATCH 03/10] changing to throwResponse for sender status in order to no affect other operations, I just added throwResponse definition and changed the call for messaging.senders.status messaging.senders.status = (email) -> callWithError messaging.senders._status._call(messaging, {}, _.defaults({emailaddress:email}, {apikey:messaging.defaults.apikey})), "senders.status", msgIsOk, extractMsgData, throwResponse the level of impact is minor and the response is reached by the client whom call the action response {"status":404,"message":"Not Found","data":{"emailaddress":"whatever@veo.tv"}} response {"ready":1} --- src/dyn-js.coffee | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dyn-js.coffee b/src/dyn-js.coffee index 66756ad..9558b67 100644 --- a/src/dyn-js.coffee +++ b/src/dyn-js.coffee @@ -220,7 +220,7 @@ callWithError = (funProm, description, successFilter, successCase, errorCase) -> log.info 'dyn', "api call returned error : #{JSON.stringify(x[1])}" errorCase x[1] - + , (x) -> log.warn 'dyn', "unexpected error : #{JSON.stringify(x[1])}" errorCase x @@ -250,7 +250,8 @@ extractZones = (x) -> {zone:v[3]} throwMessages = (x) -> throw (x.msgs || "unknown exception when calling api") -throwMsgMessages = (x) -> return x?.response?.message || "unknown exception when calling api" +throwMsgMessages = (x) -> throw (x?.response?.message || "unknown exception when calling api") +throwResponse =(x) -> return x?.response || "unknown exception when calling api" Dyn = (opts) -> traffic_defaults = _.defaults opts?.traffic || {}, { @@ -363,7 +364,7 @@ Dyn = (opts) -> messaging.senders.create = (email, seeding) -> callWithError messaging.senders._create._call(messaging, {}, _.defaults({emailaddress:email,seeding:seeding||'0'}, {apikey:messaging.defaults.apikey})), "senders.create", msgIsOk, extractMsgData, throwMsgMessages messaging.senders.update = (email, seeding) -> callWithError messaging.senders._update._call(messaging, {}, _.defaults({emailaddress:email}, {apikey:messaging.defaults.apikey})), "senders.update", msgIsOk, extractMsgData, throwMsgMessages messaging.senders.details = (email) -> callWithError messaging.senders._details._call(messaging, {}, _.defaults({emailaddress:email}, {apikey:messaging.defaults.apikey})), "senders.details", msgIsOk, extractMsgData, throwMsgMessages - messaging.senders.status = (email) -> callWithError messaging.senders._status._call(messaging, {}, _.defaults({emailaddress:email}, {apikey:messaging.defaults.apikey})), "senders.status", msgIsOk, extractMsgData, throwMsgMessages + messaging.senders.status = (email) -> callWithError messaging.senders._status._call(messaging, {}, _.defaults({emailaddress:email}, {apikey:messaging.defaults.apikey})), "senders.status", msgIsOk, extractMsgData, throwResponse messaging.senders.dkim = (email, dkim) -> callWithError messaging.senders._dkim._call(messaging, {}, _.defaults({emailaddress:email,dkim:dkim}, {apikey:messaging.defaults.apikey})), "senders.dkim", msgIsOk, extractMsgData, throwMsgMessages messaging.senders.destroy = (email) -> callWithError messaging.senders._destroy._call(messaging, {}, _.defaults({emailaddress:email}, {apikey:messaging.defaults.apikey})), "senders.destroy", msgIsOk, extractMsgData, throwMsgMessages From 79cc097cb27adef0e1b2e3f3cf9b5eabe75a2b71 Mon Sep 17 00:00:00 2001 From: toddtarsi Date: Tue, 18 Nov 2014 15:38:19 -0600 Subject: [PATCH 04/10] No more uncaught exceptions on JSON parse --- lib/dyn-js.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/dyn-js.js b/lib/dyn-js.js index fce11be..b51bc40 100644 --- a/lib/dyn-js.js +++ b/lib/dyn-js.js @@ -69,8 +69,14 @@ return res.on('end', function() { var response; log.silly('dyn', "response : " + data); - response = JSON.parse(data); - return cc(null, response, res); + try { + response = JSON.parse(data); + } + catch(ignore) {} + if (response) { + return cc(null, response, res); + } + return cc('Invalid JSON response: ' + data); }); }); req.on('error', function(e) { From e2b6ab2e216539dd881576e08d79abed69b22e1c Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Thu, 3 Sep 2015 17:01:41 -0700 Subject: [PATCH 05/10] Updated the coffee-script & the build results. Added coffee-script to the dev dependencies. Added an npm script target to run it, in lieu of the ruby one-liner. Ran it on the coffeescript source with pull requests merged. --- lib/dyn-js.js | 42 ++++++++++++++++++++---------------------- package.json | 11 +++++++---- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/dyn-js.js b/lib/dyn-js.js index b51bc40..a80a914 100644 --- a/lib/dyn-js.js +++ b/lib/dyn-js.js @@ -1,7 +1,7 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.10.0 (function() { 'use strict'; - var Dyn, callWithError, crudAccounts, crudGslb, crudGslbRegion, crudHttpRedirect, crudMessaging, crudRecipients, crudRecord, crudReportsBounces, crudReportsClicks, crudReportsComplaints, crudReportsDelivered, crudReportsIssues, crudReportsOpens, crudReportsSentMail, crudSendMail, crudSenders, crudSuppressions, crudTraffic, crudZone, extract, extractData, extractMsgData, extractMsgs, extractRecords, extractZones, failBool, https, identity, isOk, log, makePromise, msgIsOk, okBool, q, qs, throwMessages, throwMsgMessages, _, _request_q; + var Dyn, _, _request_q, callWithError, crudAccounts, crudGslb, crudGslbRegion, crudHttpRedirect, crudMessaging, crudRecipients, crudRecord, crudReportsBounces, crudReportsClicks, crudReportsComplaints, crudReportsDelivered, crudReportsIssues, crudReportsOpens, crudReportsSentMail, crudSendMail, crudSenders, crudSuppressions, crudTraffic, crudZone, extract, extractData, extractMsgData, extractMsgs, extractRecords, extractZones, failBool, https, identity, isOk, log, makePromise, msgIsOk, okBool, q, qs, throwMessages, throwMsgMessages, throwResponse; _ = require('underscore'); @@ -69,14 +69,8 @@ return res.on('end', function() { var response; log.silly('dyn', "response : " + data); - try { - response = JSON.parse(data); - } - catch(ignore) {} - if (response) { - return cc(null, response, res); - } - return cc('Invalid JSON response: ' + data); + response = JSON.parse(data); + return cc(null, response, res); }); }); req.on('error', function(e) { @@ -103,8 +97,8 @@ return _.reduce(_.keys(methods), function(a, x) { a[x] || (a[x] = {}); a[x]._path = function(dyn, data) { - var cpath, _ref; - cpath = (custom != null ? (_ref = custom[x]) != null ? _ref.path : void 0 : void 0) || path; + var cpath, ref; + cpath = (custom != null ? (ref = custom[x]) != null ? ref.path : void 0 : void 0) || path; return _.template(cpath)(data); }; a[x]._call = function(dyn, pathData, bodyData) { @@ -129,15 +123,15 @@ return _.reduce(allKeys, function(a, x) { a[x] || (a[x] = {}); a[x]._path = function(dyn, data) { - var cpath, _ref; - cpath = (custom != null ? (_ref = custom[x]) != null ? _ref.path : void 0 : void 0) || path; + var cpath, ref; + cpath = (custom != null ? (ref = custom[x]) != null ? ref.path : void 0 : void 0) || path; data.e = escape; return _.template(cpath)(data); }; a[x]._call = function(dyn, pathData, bodyData) { - var method, _ref; + var method, ref; log.silly('dyn', "api call : " + x + " -> " + path); - method = (custom != null ? (_ref = custom[x]) != null ? _ref.method : void 0 : void 0) || methods[x]; + method = (custom != null ? (ref = custom[x]) != null ? ref.method : void 0 : void 0) || methods[x]; if (method === 'GET') { return _request_q(dyn, method, a[x]._path(dyn, pathData) + "?" + qs.stringify(bodyData)); } else { @@ -475,8 +469,8 @@ }; extractMsgData = function(x) { - var _ref; - return x != null ? (_ref = x.response) != null ? _ref.data : void 0 : void 0; + var ref; + return x != null ? (ref = x.response) != null ? ref.data : void 0 : void 0; }; okBool = function() { @@ -521,8 +515,12 @@ }; throwMsgMessages = function(x) { - var _ref; - throw (x != null ? (_ref = x.response) != null ? _ref.message : void 0 : void 0) || "unknown exception when calling api"; + var ref; + throw (x != null ? (ref = x.response) != null ? ref.message : void 0 : void 0) || "unknown exception when calling api"; + }; + + throwResponse = function(x) { + return (x != null ? x.response : void 0) || "unknown exception when calling api"; }; Dyn = function(opts) { @@ -674,7 +672,7 @@ a[type].replace = (function(fqdn, records) { var arg; arg = {}; - arg["" + x + "Records"] = records; + arg[x + "Records"] = records; return callWithError(traffic.record[type]._update._call(traffic, { zone: traffic.defaults.zone, fqdn: fqdn, @@ -857,7 +855,7 @@ emailaddress: email }, { apikey: messaging.defaults.apikey - })), "senders.status", msgIsOk, extractMsgData, throwMsgMessages); + })), "senders.status", msgIsOk, extractMsgData, throwResponse); }; messaging.senders.dkim = function(email, dkim) { return callWithError(messaging.senders._dkim._call(messaging, {}, _.defaults({ diff --git a/package.json b/package.json index b21340b..cc54e34 100644 --- a/package.json +++ b/package.json @@ -25,17 +25,18 @@ "node": ">= 0.8.x" }, "devDependencies": { + "coffee-script": "~1.10.0", "grunt": "~0.4.4", "grunt-contrib-clean": "~0.5.0", "grunt-contrib-coffee": "~0.10.1" }, "bugs": { - "url" : "https://github.com/dyninc/dyn-js/issues" + "url": "https://github.com/dyninc/dyn-js/issues" }, "license": "MIT", "repository": { - "type" : "git", - "url" : "https://github.com/dyninc/dyn-js" + "type": "git", + "url": "https://github.com/dyninc/dyn-js" }, "dependencies": { "npmlog": "~0.0.6 ", @@ -48,5 +49,7 @@ "dns", "dynamic dns" ], - "main": "lib/dyn-js.js" + "scripts": { + "compile": "coffee -o lib -c src" + } } From 35898eaabc2802dbad82d326524ea82895f2b106 Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Thu, 3 Sep 2015 17:15:16 -0700 Subject: [PATCH 06/10] Dropped concat-stream into the stream reading. Now we concatenate buffers from readable streams properly, and we handle the errors they might emit, who knows. JSON.parse() can throw exceptions. Catch them. --- lib/dyn-js.js | 37 +++++++++++++++++++++++++------------ package.json | 1 + src/dyn-js.coffee | 40 +++++++++++++++++++++++++--------------- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/lib/dyn-js.js b/lib/dyn-js.js index a80a914..9cef5d3 100644 --- a/lib/dyn-js.js +++ b/lib/dyn-js.js @@ -1,7 +1,7 @@ // Generated by CoffeeScript 1.10.0 (function() { 'use strict'; - var Dyn, _, _request_q, callWithError, crudAccounts, crudGslb, crudGslbRegion, crudHttpRedirect, crudMessaging, crudRecipients, crudRecord, crudReportsBounces, crudReportsClicks, crudReportsComplaints, crudReportsDelivered, crudReportsIssues, crudReportsOpens, crudReportsSentMail, crudSendMail, crudSenders, crudSuppressions, crudTraffic, crudZone, extract, extractData, extractMsgData, extractMsgs, extractRecords, extractZones, failBool, https, identity, isOk, log, makePromise, msgIsOk, okBool, q, qs, throwMessages, throwMsgMessages, throwResponse; + var Dyn, _, _request_q, callWithError, concat, crudAccounts, crudGslb, crudGslbRegion, crudHttpRedirect, crudMessaging, crudRecipients, crudRecord, crudReportsBounces, crudReportsClicks, crudReportsComplaints, crudReportsDelivered, crudReportsIssues, crudReportsOpens, crudReportsSentMail, crudSendMail, crudSenders, crudSuppressions, crudTraffic, crudZone, extract, extractData, extractMsgData, extractMsgs, extractRecords, extractZones, failBool, https, identity, isOk, log, makePromise, msgIsOk, okBool, q, qs, safeparse, throwMessages, throwMsgMessages, throwResponse; _ = require('underscore'); @@ -13,10 +13,22 @@ qs = require('querystring'); + concat = require('concat-stream'); + _.templateSettings = { interpolate: /\{\{(.+?)\}\}/g }; + safeparse = function(str) { + var error, ex; + try { + return JSON.parse(str); + } catch (error) { + ex = error; + return str; + } + }; + _request_q = function(dyn, method, path, body, isTraffic) { var cc, defer, headers, host, opts, port, req; log.verbose('dyn', "invoking via https : " + method + " " + path); @@ -59,19 +71,20 @@ }; log.silly('dyn', "request : " + (JSON.stringify(opts))); req = https.request(opts, function(res) { - var data; - data = ''; - res.on('readable', function() { - var chunk; - chunk = res.read(); - return data += chunk.toString('ascii'); - }); - return res.on('end', function() { + var concatStream, handleDone, handleError; + handleError = function(err) { + log.silly('dyn', err.message); + return cc(err); + }; + handleDone = function(buffer) { var response; - log.silly('dyn', "response : " + data); - response = JSON.parse(data); + log.silly('dyn', "response : " + (buffer.toString())); + response = safeparse(buffer); return cc(null, response, res); - }); + }; + concatStream = concat(handleDone); + res.on('error', handleError); + return res.pipe(concatStream); }); req.on('error', function(e) { log.warn('dyn', "error : " + (JSON.stringify(e))); diff --git a/package.json b/package.json index cc54e34..570dde7 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "url": "https://github.com/dyninc/dyn-js" }, "dependencies": { + "concat-stream": "~1.5.0", "npmlog": "~0.0.6 ", "q": "~1.0.1", "underscore": "~1.6.0" diff --git a/src/dyn-js.coffee b/src/dyn-js.coffee index 9558b67..d8cf5da 100644 --- a/src/dyn-js.coffee +++ b/src/dyn-js.coffee @@ -1,13 +1,20 @@ 'use strict' -_ = require 'underscore' -q = require 'q' -https = require 'https' -log = require 'npmlog' -qs = require 'querystring' +_ = require 'underscore' +q = require 'q' +https = require 'https' +log = require 'npmlog' +qs = require 'querystring' +concat = require 'concat-stream' _.templateSettings = { interpolate: /\{\{(.+?)\}\}/g } +safeparse = (str) -> + try + return JSON.parse(str) + catch ex + return str + _request_q = (dyn, method, path, body, isTraffic) -> log.verbose 'dyn', "invoking via https : #{method} #{path}" defer = q.defer() @@ -41,17 +48,20 @@ _request_q = (dyn, method, path, body, isTraffic) -> opts = {hostname:host,port:port,method:method,path:path,headers:headers} log.silly 'dyn', "request : #{JSON.stringify(opts)}" req = https.request opts, (res) -> - # log.silly 'dynres', arguments - data = '' - res.on 'readable', -> - # log.silly 'dynres', arguments - chunk = res.read() - # log.silly 'dyn', "partial : #{chunk}" - data += chunk.toString('ascii') - res.on 'end', -> - log.silly 'dyn', "response : #{data}" - response = JSON.parse(data) + + handleError = (err) -> + log.silly 'dyn', err.message + cc(err) + + handleDone = (buffer) -> + log.silly 'dyn', "response : #{buffer.toString()}" + response = safeparse(buffer) cc(null, response, res) + + concatStream = concat(handleDone) + res.on('error', handleError) + res.pipe(concatStream) + req.on 'error', (e) -> log.warn 'dyn', "error : #{JSON.stringify(e)}" cc(e) From 553c03b064a3b8e271748304b6b151057d765906 Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Thu, 3 Sep 2015 17:17:59 -0700 Subject: [PATCH 07/10] Remove the apparently unused Gruntfile & ruby build script. The npm `compile` script target does all that's needed here. --- Gruntfile.coffee | 26 -------------------------- package.json | 5 +---- scripts/compile.rb | 3 --- 3 files changed, 1 insertion(+), 33 deletions(-) delete mode 100644 Gruntfile.coffee delete mode 100644 scripts/compile.rb diff --git a/Gruntfile.coffee b/Gruntfile.coffee deleted file mode 100644 index 130de8d..0000000 --- a/Gruntfile.coffee +++ /dev/null @@ -1,26 +0,0 @@ -module.exports = (grunt) -> - grunt.initConfig - pkg: grunt.file.readJSON 'package.json' - - clean: - default: - src: ['lib'] - - - coffee: - compile: - options: - bare: true - join: false - files: [ - expand: true - cwd: 'src' - src: '**/*.coffee' - dest: 'lib' - ext: '.js' - ] - - grunt.loadTasks('tasks') - grunt.loadNpmTasks('grunt-contrib-clean') - grunt.loadNpmTasks('grunt-contrib-coffee') - grunt.registerTask('default', ['clean', 'coffee']) diff --git a/package.json b/package.json index 570dde7..b237892 100644 --- a/package.json +++ b/package.json @@ -25,10 +25,7 @@ "node": ">= 0.8.x" }, "devDependencies": { - "coffee-script": "~1.10.0", - "grunt": "~0.4.4", - "grunt-contrib-clean": "~0.5.0", - "grunt-contrib-coffee": "~0.10.1" + "coffee-script": "~1.10.0" }, "bugs": { "url": "https://github.com/dyninc/dyn-js/issues" diff --git a/scripts/compile.rb b/scripts/compile.rb deleted file mode 100644 index 632b908..0000000 --- a/scripts/compile.rb +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/env ruby - -`coffee -o lib -c src` From e83b778408d4ebcab13899e38a16c59743c7c487 Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Thu, 3 Sep 2015 17:20:16 -0700 Subject: [PATCH 08/10] Document the script target. --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dbc1dbc..0290853 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Using Messaging is as easy as: return log.info('RESULT', "finished : " + (JSON.stringify(x))); }); }, fail); - + async.series([ function() { return dyn.accounts.create("example@foo.com", "secret", "bar", "1234567890").then(function(x) { @@ -105,7 +105,7 @@ Using Messaging is as easy as: return log.info('RESULT', "finished : " + (JSON.stringify(x))); }); }, fail); - + async.series([ function() { return dyn.recipients.activate("foo@bars.com").then(function(x) { @@ -123,7 +123,7 @@ Using Messaging is as easy as: return log.info('RESULT', "finished : " + (JSON.stringify(x))); }); }, fail); - + async.series([ function() { return dyn.send_mail.create("foo@bars.com", "recipient@destination.com", "hello, new js api", "it works!").then(function(x) { @@ -151,6 +151,10 @@ Using Messaging is as easy as: * See the "examples" folder for more comprehensive examples +## Building + +To compile the coffee-script, first install dev dependencies: `npm install`. Then run `npm run compile`. + ## License (The MIT License) From ed07590331d3f197ce3ba35d7cf93adabe961923 Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Thu, 3 Sep 2015 20:49:47 -0700 Subject: [PATCH 09/10] Added support for node removal & job info. --- lib/dyn-js.js | 32 +++++++++++++++++++++++++++++++- src/dyn-js.coffee | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/dyn-js.js b/lib/dyn-js.js index 9cef5d3..e3737ab 100644 --- a/lib/dyn-js.js +++ b/lib/dyn-js.js @@ -1,7 +1,7 @@ // Generated by CoffeeScript 1.10.0 (function() { 'use strict'; - var Dyn, _, _request_q, callWithError, concat, crudAccounts, crudGslb, crudGslbRegion, crudHttpRedirect, crudMessaging, crudRecipients, crudRecord, crudReportsBounces, crudReportsClicks, crudReportsComplaints, crudReportsDelivered, crudReportsIssues, crudReportsOpens, crudReportsSentMail, crudSendMail, crudSenders, crudSuppressions, crudTraffic, crudZone, extract, extractData, extractMsgData, extractMsgs, extractRecords, extractZones, failBool, https, identity, isOk, log, makePromise, msgIsOk, okBool, q, qs, safeparse, throwMessages, throwMsgMessages, throwResponse; + var Dyn, _, _request_q, callWithError, concat, crudAccounts, crudGslb, crudGslbRegion, crudHttpRedirect, crudJob, crudMessaging, crudNode, crudRecipients, crudRecord, crudReportsBounces, crudReportsClicks, crudReportsComplaints, crudReportsDelivered, crudReportsIssues, crudReportsOpens, crudReportsSentMail, crudSendMail, crudSenders, crudSuppressions, crudTraffic, crudZone, extract, extractData, extractMsgData, extractMsgs, extractRecords, extractZones, failBool, https, identity, isOk, log, makePromise, msgIsOk, okBool, q, qs, safeparse, throwMessages, throwMsgMessages, throwResponse; _ = require('underscore'); @@ -195,6 +195,22 @@ }); }; + crudJob = function() { + return crudTraffic("/Job/", { + _get: { + path: "/Job/{{id}}/" + } + }); + }; + + crudNode = function() { + return crudTraffic("/Node/", { + _destroy: { + path: "/Node/{{zone}}/{{fqdn}}/" + } + }); + }; + crudHttpRedirect = function() { return crudTraffic("/HTTPRedirect/", { _get: { @@ -608,6 +624,20 @@ thaw: true }), "zone.thaw", isOk, extractData, throwMessages); }; + traffic.job = crudJob(); + traffic.job.get = function(id) { + return callWithError(traffic.job._get._call(traffic, { + zone: traffic.defaults.zone, + id: id + }, {}), "job.get", isOk, extractData, throwMessages); + }; + traffic.node = crudNode(); + traffic.node.destroy = function(fqdn) { + return callWithError(traffic.node._destroy._call(traffic, { + zone: traffic.defaults.zone, + fqdn: fqdn + }, {}), "node.destroy", isOk, extractMsgs, throwMessages); + }; traffic.session = crudTraffic("/Session/"); traffic.session.create = function() { return callWithError(traffic.session._create._call(traffic, {}, _.pick(traffic.defaults, 'customer_name', 'user_name', 'password')), "session.create", isOk, function(x) { diff --git a/src/dyn-js.coffee b/src/dyn-js.coffee index d8cf5da..3568126 100644 --- a/src/dyn-js.coffee +++ b/src/dyn-js.coffee @@ -120,6 +120,14 @@ crudZone = -> _update: {path:"/Zone/{{zone}}/"} _destroy: {path:"/Zone/{{zone}}/"} +crudJob = -> + crudTraffic "/Job/", + _get: {path:"/Job/{{id}}/"} + +crudNode = -> + crudTraffic "/Node/", + _destroy: {path:"/Node/{{zone}}/{{fqdn}}/"} + crudHttpRedirect = -> crudTraffic "/HTTPRedirect/", _get: {path:"/HTTPRedirect/{{zone}}/{{fqdn}}"} @@ -309,6 +317,18 @@ Dyn = (opts) -> traffic.zone.freeze = -> callWithError traffic.zone._update._call(traffic, {zone:traffic.defaults.zone}, {freeze:true}), "zone.freeze", isOk, extractData, throwMessages traffic.zone.thaw = -> callWithError traffic.zone._update._call(traffic, {zone:traffic.defaults.zone}, {thaw:true}), "zone.thaw", isOk, extractData, throwMessages + traffic.job = crudJob() + traffic.job.get = (id) -> + callWithError traffic.job._get._call(traffic, { + zone:traffic.defaults.zone, + id:id }, {}), + "job.get", isOk, extractData, throwMessages + + traffic.node = crudNode() + traffic.node.destroy = (fqdn) -> callWithError traffic.node._destroy._call(traffic, + {zone:traffic.defaults.zone, fqdn: fqdn}, {}), + "node.destroy", isOk, extractMsgs, throwMessages + traffic.session = crudTraffic "/Session/" traffic.session.create = -> callWithError(traffic.session._create._call(traffic, {}, _.pick(traffic.defaults, 'customer_name', 'user_name', 'password')), "session.create", isOk, (x) -> traffic.defaults.token = x.data.token From f5c47b378df53ba2625ce1db6fde8d5c477318cb Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Fri, 21 Oct 2016 14:22:01 -0700 Subject: [PATCH 10/10] Repair extractRecords() If x.data is an array, we want to parse it as a list of strings. If x.data is already an object, we want to return it. This makes API calls like `dyn.record._CNAME.get()` actually work. While I was there, bumped deps. --- lib/dyn-js.js | 3 +++ package.json | 4 ++-- src/dyn-js.coffee | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/dyn-js.js b/lib/dyn-js.js index e3737ab..91d2e4b 100644 --- a/lib/dyn-js.js +++ b/lib/dyn-js.js @@ -514,6 +514,9 @@ if (!(x && x.data)) { return []; } + if (!Array.isArray(x.data)) { + return x.data; + } return _(x.data).map(function(r) { var v; v = r.split("/"); diff --git a/package.json b/package.json index b237892..7ec7752 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,8 @@ "url": "https://github.com/dyninc/dyn-js" }, "dependencies": { - "concat-stream": "~1.5.0", - "npmlog": "~0.0.6 ", + "concat-stream": "~1.5.2", + "npmlog": "~2.0.4", "q": "~1.0.1", "underscore": "~1.6.0" }, diff --git a/src/dyn-js.coffee b/src/dyn-js.coffee index 3568126..9d085d5 100644 --- a/src/dyn-js.coffee +++ b/src/dyn-js.coffee @@ -257,6 +257,7 @@ failBool = -> false extractRecords = (x) -> return [] unless x && x.data + return x.data unless Array.isArray(x.data) _(x.data).map (r) -> v = r.split("/") {type:v[2].replace(/Record$/, ""),zone:v[3],fqdn:v[4],id:v[5]}