Skip to content

Commit

Permalink
enhance(api versioning): ported api versioning changes from phpclassi…
Browse files Browse the repository at this point in the history
…c/php-shopify
  • Loading branch information
nickmacdonald7 committed Aug 22, 2022
1 parent cded408 commit 88dd341
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function __construct($config, $id = null, $parentResourceUrl = '')
$this->config = $config;
$this->id = $id;

$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['AdminUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['ApiUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');

if (isset($config['AccessToken'])) {
$this->httpHeaders['X-Shopify-Access-Token'] = $config['AccessToken'];
Expand Down
26 changes: 24 additions & 2 deletions lib/ShopifySDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class ShopifySDK
*/
public static $timeAllowedForEachApiCall = .5;

/**
* @var string Default Shopify API version
*/
public static $defaultApiVersion = '2022-07';

/**
* Shop / API configurations
*
Expand Down Expand Up @@ -223,8 +228,7 @@ class ShopifySDK
public function __construct($config = array())
{
if(!empty($config)) {
$this->config = $config;
$this->setAdminUrl();
ShopifySDK::config($config);
}
}

Expand Down Expand Up @@ -285,6 +289,13 @@ public function __call($resourceName, $arguments)
*/
public function config($config)
{
/**
* Reset config to it's initial values
*/
$this->config = array(
'ApiVersion' => self::$defaultApiVersion,
);

foreach ($config as $key => $value) {
$this->config[$key] = $value;
}
Expand Down Expand Up @@ -313,6 +324,7 @@ public function setAdminUrl()

//Remove https:// and trailing slash (if provided)
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
$apiVersion = $this->config['ApiVersion'];

if(isset($this->config['ApiKey']) && isset($this->config['Password'])) {
$apiKey = $this->config['ApiKey'];
Expand All @@ -323,6 +335,7 @@ public function setAdminUrl()
}

$this->config['AdminUrl'] = $adminUrl;
$this->config['ApiUrl'] = $adminUrl . "api/$apiVersion/";

return $adminUrl;
}
Expand All @@ -336,6 +349,15 @@ public function getAdminUrl() {
return $this->config['AdminUrl'];
}

/**
* Get the api url of the configured shop
*
* @return string
*/
public function getApiUrl() {
return $this->config['ApiUrl'];
}

/**
* Maintain maximum 2 calls per second to the API
*
Expand Down

0 comments on commit 88dd341

Please sign in to comment.