DEV Community

Chris Fleischhacker
Chris Fleischhacker

Posted on • Originally published at chrisfleischhacker.wordpress.com on

Consume Volusion eStore API Using Meteor JS

The \server\main.js in this example performs an API call to your Volusion eStore, converts the response to JSON and inserts to MongoDB.

The \client\main.js makes the call and displays the data from MongoDB.

meteor create volusionapi

cd volusionapi

meteor add http peerlibrary:xml2js

Copy these files https://github.com/cjfleischhacker/MeteorVolusionAPI.git or create these files:

\client\main.html

`


volusion

Welcome to Meteor!

{{> vapi}}

List Courses

{{errMessage}}

{{#each courses}} {{/each}}
ID Code Name
{{ProductID}} {{ProductCode}} {{ProductName}}


`

\client\main.js


import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
Courses = new Mongo.Collection('courses');

Meteor.call(‘getJSONFromAPI’, function (err, res) {

if (err) {

console.log(err);

} else {

console.log(res.xmldata.Products);

}

});

Template.vapi.onCreated(function helloOnCreated() {

console.log(‘oncreate…’);

});

Template.vapi.helpers({

courses: function () {

return Courses.find();

}

});

\server\main.js


import { Meteor } from 'meteor/meteor';
import { xml2js } from 'meteor/peerlibrary:xml2js';
Courses = new Mongo.Collection('courses');

Meteor.methods({

‘getJSONFromAPI’: function () {

this.unblock();

var apiUrl = ‘http://store.volusion.com/net/WebService.aspx?Login=email@mystore.com&EncryptedPassword=YourVolusionAPIEncryptedPassword&EDI_Name=Generic\Products&SELECT_Columns=p.ProductCode,p.ProductName,pd.ProductDescription’

;

var response = Meteor.wrapAsync(apiCall)(apiUrl);

const result = xml2js.parseStringSync(response, { explicitArray: false, emptyTag: undefined });

for (var i = 0; i < result.xmldata.Products.length; i++) {

Courses.insert(result.xmldata.Products[i]);

}

return result;

}

});

var apiCall = function (apiUrl, callback) {

// try…catch allows you to handle errors

try {

var response = HTTP.get(apiUrl).content;

// A successful API call returns no error

callback(null, response);

} catch (error) {

// If the API responded with an error message and a payload

if (error.response) {

var errorCode = error.response.data.code;

var errorMessage = error.response.data.message;

// Otherwise use a generic error message

} else {

var errorCode = 500;

var errorMessage = ‘Cannot access the API’;

}

// Create an Error object and return it via callback

var myError = new Meteor.Error(errorCode, errorMessage);

callback(myError, null);

}

}

meteor

Top comments (0)