Created
February 17, 2014 04:30
-
-
Save wyqydsyq/9044751 to your computer and use it in GitHub Desktop.
Shopify call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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