Developing Apps

with

JavaScript and Remote APIS

@ColinMcDonald

Developing Apps

with

JavaScript and Remote APIS

@ColinMcDonald

Developing Apps

with

JavaScript and Shopify

@ColinMcDonald

Colin McDonald

Seasoned start-up software developer.

Co-founder & Lead (read: only) developer @ FoKo.

Shopify Apps

Traditionally are web apps.

Offline/Service worker with node.js

Shopify API - REST

Registered shopify app makes HTTP requests.

Find success in the details.

Read the documentation.

Read it again.

And read it again.

Authentication

Are you the App you say you are?

Each Shopify customer must give you permission.

Shopify Apps use OAuth 2.0.

You get a token. Keep that token.

Simplify your life

DRY approach - to an extent.

Example

Get shop info.

HTTP Request with node.js


 
var requestData = {
  hostname: shopDomain,
  path: '/admin/shop.json',
  method: 'GET',
  headers: {
    'X-Shopify-Access-Token': token,
    'Expect:': ''
  }
};
 

 
var https = require('https');
var req = https.request(requestData, function(res) {
  res.on('data', function(){ 
    // collect data
  });
  res.on('end', function(){
    // check for http statusCodes, etc
  });
});
req.on('error', function(e) { 
  // handle error in making request
});
req.end();
 

 
var ShopifyAPI = require('shopify-node-promise');
var api = new ShopifyAPI(shopDomain, token);
api.get("/admin/shop.json").then(function(result){
  console.log(result);
});
 

Purists may not like the URL Path being repeated.

All comes back to following the API docs.

Sequential API Calls

I use promises

Promises


 
api.get("/admin/shop.json").then(function(result){
  console.log(result);
}, function(e){
  console.error(e);
});
 

 
api.get("/admin/shop.json").then(function(result){
  shop = result;
  return api.get("/admin/customers.json");
}).then(function(result){
  customers = result;
  // save to local datastore
  return data.save(shop, customers);
}).then(function(saveResults){
  console.log('All Passed!');
}, function(e){
  console.error(e);
};
 

API Calls will fail

Group a set of API calls together.

API Calls will fail

Retry on certain errors.

API Calls will fail

Only continue when they pass.

Transaction Script

Organizes business logic by procedures where each procedure handles a single request...

Example: Weekly Emails

  • Get all Customers
  • Email each customer about new products

Example: Weekly Emails

  • Get all Customers
    • Did we email customer this week?
    • Email customer about new products
    • Make a note that we just emailed this customer

Testing

Finding the right balance of code-coverage.

Integration vs Unit Tests

Integration is high-level, unit is low-level.

Integration typically do not use mock objects.

mocha

&

require("chai")


 
  describe('ShopifyAPI.get', function() {

    it('should return shop object', function(done) {

      shopifyAPI.get("/admin/shop.json").then(function(result) {

        assert.equal(result.data.shop.name, config.shopName);
        done();

      }, function(error) { done(error);  });
    });
 

Thanks for participating!