Skip to content

Instantly share code, notes, and snippets.

@iamkevingreen
Created March 2, 2016 19:38
Show Gist options
  • Save iamkevingreen/10a7ead395787aed81bf to your computer and use it in GitHub Desktop.
Save iamkevingreen/10a7ead395787aed81bf to your computer and use it in GitHub Desktop.
/**
* Shopify Client API
*/
var Shopify = {}
var API = Shopify.API = function ShopifyAPI(options) {
this.config = {
shop: options.shop,
api_key: options.api_key || null,
password: options.password || null,
debug: options.debug || false
}
this.getCredentials = function() {
return this.config;
}
this.getPrivateAppHeaders = function() {
return this.config.api_key + ":" + this.config.password;
}
this.getBaseAPIURL = function() {
var url = 'https://'+this.config.shop+'.myshopify.com/';
return url;
}
this.getBaseAPIURLWithCredentials = function() {
var url = 'https://'+this.config.api_key+':'+this.config.password+'@'+this.config.shop+'.myshopify.com/';
return url;
}
/**
* Shopify Request
*
* @param {String} method
* @param {String} endpoint
* @param {Object} options
* {
* content : '' // string to use as the http request body
* data : { }, // JSON-able object to use as http request body. overwrites content
* params : '', // URL params
* auth : '', // basic auth in the form of username:password
* headers : '', // dict of strings
* timeout : null, // timeout
* }
*
* @param {Function} callback
*/
this.request = function(method, endpoint, options, callback) {
var method = method || 'GET',
endpoint = endpoint || '',
options = options || {};
options.auth = this.getPrivateAppHeaders()
// Checks
// check(method, String)
// check(endpoint, String)
// check(options, Object)
// check(callback, Function)
var api_url = this.getBaseAPIURL()
try {
var result = HTTP.call(method, (api_url + endpoint), options )
if (200 <= result.statusCode && result.statusCode <= 299) {
return result.data;
}
} catch (e) {
console.log(e);
// network error
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment