Skip to content

Commit

Permalink
Merge pull request #37 from storyblok/feature/add-api-region
Browse files Browse the repository at this point in the history
INT-157: Added API region
  • Loading branch information
joaokamun authored Feb 15, 2022
2 parents e3a892f + b522f81 commit 09d6b99
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/Storyblok/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class BaseClient
* @param string $apiVersion
* @param bool $ssl
*/
function __construct($apiKey = null, $apiEndpoint = "api.storyblok.com", $apiVersion = "v2", $ssl = false)
function __construct($apiKey = null, $apiEndpoint = null, $apiVersion = "v2", $ssl = false, $apiRegion = null)
{
$handlerStack = HandlerStack::create(new CurlHandler());
$handlerStack->push(Middleware::retry($this->retryDecider(), $this->retryDelay()));

$this->setApiKey($apiKey);
$this->client = new Guzzle([
'base_uri'=> $this->generateEndpoint($apiEndpoint, $apiVersion, $ssl),
'base_uri'=> $this->generateEndpoint($apiEndpoint, $apiVersion, $ssl, $apiRegion),
'handler' => $handlerStack
]);
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public function getTimeout()
*
* @return string
*/
private function generateEndpoint($apiEndpoint, $apiVersion, $ssl)
private function generateEndpoint($apiEndpoint, $apiVersion, $ssl, $apiRegion)
{
if ($this instanceof ManagementClient) {
$prefix = "";
Expand All @@ -192,6 +192,11 @@ private function generateEndpoint($apiEndpoint, $apiVersion, $ssl)
$protocol = "https://";
}

if (!$apiEndpoint) {
$region = $apiRegion ? "-{$apiRegion}" : "";
$apiEndpoint = "api{$region}.storyblok.com";
}

return $protocol . $apiEndpoint . "/" . $apiVersion . $prefix . "/";
}

Expand Down
4 changes: 2 additions & 2 deletions src/Storyblok/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class Client extends BaseClient
* @param string $apiVersion
* @param bool $ssl
*/
function __construct($apiKey = null, $apiEndpoint = "api.storyblok.com", $apiVersion = "v2", $ssl = false)
function __construct($apiKey = null, $apiEndpoint = null, $apiVersion = "v2", $ssl = false, $apiRegion = null)
{
parent::__construct($apiKey, $apiEndpoint, $apiVersion, $ssl);
parent::__construct($apiKey, $apiEndpoint, $apiVersion, $ssl, $apiRegion);

if (isset($_GET['_storyblok'])) {
$this->editModeEnabled = $_GET['_storyblok'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Client.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require '../vendor/autoload.php';

$client = new \Storyblok\Client('Iw3XKcJb6MwkdZEwoQ9BCQtt'); //
$client = new \Storyblok\Client('Iw3XKcJb6MwkdZEwoQ9BCQtt');

// Optionally set a cache
$client->setCache('filesytem', array('path' => 'cache'));
Expand Down

0 comments on commit 09d6b99

Please sign in to comment.