UNPKG

rybbon-marketo

Version:

Marketo API Wrapper

125 lines (115 loc) 4.25 kB
/* * API for integrating with Marketo */ var unirest= require ('unirest'); var qs = require('querystring'); var settings = { client_id : "", client_secret : "", rest_end_pt : "", rest_identity : "", access_token : "", expires : '' }; module.exports = { init: function (client_id, client_secret, rest_end_pt, rest_identity, access_token){ settings.client_id = client_id; settings.client_secret = client_secret; settings.rest_end_pt = rest_end_pt; settings.rest_identity = rest_identity; settings.access_token = access_token; settings.expires = Math.floor (Date.now() / 1000); if (settings.access_token != "") { this.authenticate(); } }, exec_get: function (qurl, options, cb){ var qp = qs.stringify(options); var url = settings.rest_end_pt + qurl+ '?access_token=' + settings.access_token; if (qp.length > 0) url += '&' + qp; unirest.get (url) .headers ({ 'Accept' : 'application/json', 'Content-type': 'application/json' }) .send() .end (function (response){ if (cb != undefined) cb (response.body); else console.log (JSON.stringify (response.body, null, 4)); }); }, get: function (qurl, options, cb){ if (settings.access_token == '') { var mkto = this; this.authenticate(function (err) { mkto.exec_get(qurl, options, cb); }); } else { this.exec_get (qurl, options, cb); } }, exec_post: function (qurl, options, cb) { var url = settings.rest_end_pt + qurl+ '?access_token=' + settings.access_token; unirest.post (url) .headers ({ 'Accept' : 'application/json', 'Content-type': 'application/json' }) .send(JSON.stringify (options)) .end (function (response){ if (cb != undefined) cb (response.body); else console.log (JSON.stringify (response.body, null, 4)); }); }, post: function (qurl, options, cb){ if (settings.access_token == '') { var mkto = this; this.authenticate(function (err) { mkto.exec_post(qurl, options, cb); }); } else { this.exec_post (qurl, options, cb); } }, authenticate: function (cb) { var url = settings.rest_identity + '/oauth/token?grant_type=client_credentials&client_id=' + settings.client_id + '&client_secret=' + settings.client_secret; unirest.get(url) .headers ({ 'Accept' : 'application/json', 'Content-type': 'application/json' }) .send() .end (function (response){ if (response.code == 200) { settings.access_token = response.body.access_token; settings.expires = Math.floor(Date.now()/ 1000) + response.body.expires_in; if (cb != undefined) cb (false, response.body); else console.log (JSON.stringify (response, null, 4)); } else { console.log ("Failed to authenticate!"); console.log (JSON.stringify (response, null, 4)); } }); }, getLeadByEmail: function (email, cb) { var options = { filterType: "email", filterValues: email, fields: 'company,firstName,lastName' }; this.get('/v1/leads.json', options, cb); }, getLeadFields: function (cb){ this.get ('/v1/leads/describe.json', null, cb); }, getLists: function (cb) { this.get ('/v1/lists.json', null, cb); }, addLeadToList: function (lead_id, list_id, cb) { var options = { input: [{ id: lead_id}] } this.post ('/v1/lists/' + list_id + '/leads.json', options, cb); } };