Skip to content

Instantly share code, notes, and snippets.

@wyqydsyq
Created February 17, 2014 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyqydsyq/9044751 to your computer and use it in GitHub Desktop.
Save wyqydsyq/9044751 to your computer and use it in GitHub Desktop.
Shopify call
shopify = {
// call defaults
'site': false,
'token': false,
'data': {},
// make an API call, when done, calls callback(data)
'call': function(hit, callback, data, site, token){
if(typeof data === 'undefined') data = this.data;
if(typeof site === 'undefined') site = this.site;
if(typeof token === 'undefined') token = this.token;
if(site.match('.') !== -1) site += '.myshopify.com';
// make call
var https = require('https');
var data_string = JSON.stringify(data);
if(hit.match(/^(POST|GET|PUT|DELETE) \//) === null) hit = 'GET '+hit;
var method = hit.split(' ')[0];
var path = hit.split(' ')[1];
debug(data_string, 'HTTP: '+method+' '+site+path);
var call = https.request({
'method': method,
'host': site,
'path': path,
'headers': {
"X-Shopify-Access-Token": token,
"Content-Length": data_string.length,
"Content-Type": 'application/json'
}
}, function(response){
response.setEncoding('utf8');
var output = '';
response.on('data', function (res) {
output += res;
});
response.on('end', function(){
try {
var json = JSON.parse(output);
} catch(e) {
debug(output, e);
}
if(typeof callback == 'function') callback(json);
});
});
call.write(data_string);
call.end();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment