Skip to content

Commit 5a95d05

Browse files
authored
Merge pull request #168 from cameron4music/misc/get-host-by-client
refactored REST gateway base_url action method
2 parents 8029bd3 + 39a166d commit 5a95d05

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/GDS/Gateway/RESTv1.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ class RESTv1 extends \GDS\Gateway
2424
const MODE_UNSPECIFIED = 'UNSPECIFIED';
2525

2626
/**
27-
* REST API Base Endpoint
27+
* Client config keys.
2828
*/
29-
private $base_url = 'https://datastore.googleapis.com';
29+
const CONFIG_CLIENT_BASE_URL = 'base_url';
30+
31+
/**
32+
* Default Base URL to use.
33+
*/
34+
const DEFAULT_BASE_URL = 'https://datastore.googleapis.com';
3035

3136
/**
3237
* @var ClientInterface
@@ -92,14 +97,16 @@ protected function initHttpClient()
9297
$obj_stack = HandlerStack::create();
9398
$obj_stack->push(ApplicationDefaultCredentials::getMiddleware(['https://www.googleapis.com/auth/datastore']));
9499

100+
$str_base_url = self::DEFAULT_BASE_URL;
101+
95102
if (getenv("DATASTORE_EMULATOR_HOST") !== FALSE) {
96-
$this->base_url = getenv("DATASTORE_EMULATOR_HOST");
103+
$str_base_url = getenv("DATASTORE_EMULATOR_HOST");
97104
}
98105

99106
// Create the HTTP client
100107
return new Client([
101108
'handler' => $obj_stack,
102-
'base_url' => $this->base_url,
109+
'base_url' => $str_base_url,
103110
'auth' => 'google_auth' // authorize all requests
104111
]);
105112
}
@@ -474,6 +481,22 @@ public function beginTransaction($bol_cross_group = FALSE)
474481
return null;
475482
}
476483

484+
/**
485+
* Get the base url from the client object.
486+
*
487+
* Note: If for some reason the client's base URL is not set then we will return the default endpoint.
488+
*
489+
* @return string
490+
*/
491+
protected function getBaseUrl() {
492+
$str_base_url = $this->obj_http_client->getConfig(self::CONFIG_CLIENT_BASE_URL);
493+
if (!empty($str_base_url)) {
494+
return $str_base_url;
495+
}
496+
497+
return self::DEFAULT_BASE_URL;
498+
}
499+
477500
/**
478501
* Build a URL for a Datastore action
479502
*
@@ -482,7 +505,7 @@ public function beginTransaction($bol_cross_group = FALSE)
482505
*/
483506
private function actionUrl($str_action)
484507
{
485-
return $this->base_url . '/v1/projects/' . $this->str_dataset_id . ':' . $str_action;
508+
return $this->getBaseUrl() . '/v1/projects/' . $this->str_dataset_id . ':' . $str_action;
486509
}
487510

488511
}

src/GDS/Mapper/RESTv1.php

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ protected function extractDatetimeValue($obj_property)
9999
protected function extractStringListValue($obj_property)
100100
{
101101
$arr_values = [];
102+
103+
if (!isset($obj_property->arrayValue->values)) {
104+
return $arr_values;
105+
}
106+
102107
foreach((array)$obj_property->arrayValue->values as $obj_value) {
103108
if(isset($obj_value->stringValue)) {
104109
$arr_values[] = $obj_value->stringValue;

0 commit comments

Comments
 (0)