Skip to content

Commit e21f6dc

Browse files
authored
fix(php): Use api config to generate hosts (#133)
1 parent 5854261 commit e21f6dc

File tree

9 files changed

+18
-45
lines changed

9 files changed

+18
-45
lines changed

clients/algoliasearch-client-php/lib/Api/AbtestingApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function createWithConfig(AbTestingConfig $config)
6666
// If a list of hosts was passed, we ignore the cache
6767
$clusterHosts = ClusterHosts::create($hosts);
6868
} else {
69-
$clusterHosts = ClusterHosts::createForAnalytics($config->getAppId());
69+
$clusterHosts = ClusterHosts::create('analytics.'.$config->getRegion().'.algolia.com');
7070
}
7171

7272
$apiWrapper = new ApiWrapper(

clients/algoliasearch-client-php/lib/Api/AnalyticsApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function createWithConfig(AnalyticsConfig $config)
6666
// If a list of hosts was passed, we ignore the cache
6767
$clusterHosts = ClusterHosts::create($hosts);
6868
} else {
69-
$clusterHosts = ClusterHosts::createForAnalytics($config->getAppId());
69+
$clusterHosts = ClusterHosts::create('analytics.'.$config->getRegion().'.algolia.com');
7070
}
7171

7272
$apiWrapper = new ApiWrapper(

clients/algoliasearch-client-php/lib/Api/InsightsApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function createWithConfig(InsightsConfig $config)
6565
// If a list of hosts was passed, we ignore the cache
6666
$clusterHosts = ClusterHosts::create($hosts);
6767
} else {
68-
$clusterHosts = ClusterHosts::createForInsights($config->getAppId());
68+
$clusterHosts = ClusterHosts::create('insights.'.$config->getRegion().'.algolia.io');
6969
}
7070

7171
$apiWrapper = new ApiWrapper(

clients/algoliasearch-client-php/lib/Api/PersonalizationApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function createWithConfig(PersonalizationConfig $config)
6666
// If a list of hosts was passed, we ignore the cache
6767
$clusterHosts = ClusterHosts::create($hosts);
6868
} else {
69-
$clusterHosts = ClusterHosts::createForRecommendation($config->getAppId());
69+
$clusterHosts = ClusterHosts::create('personalization.'.$config->getRegion().'.algolia.com');
7070
}
7171

7272
$apiWrapper = new ApiWrapper(

clients/algoliasearch-client-php/lib/Api/QuerySuggestionsApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function createWithConfig(QuerySuggestionsConfig $config)
6666
// If a list of hosts was passed, we ignore the cache
6767
$clusterHosts = ClusterHosts::create($hosts);
6868
} else {
69-
$clusterHosts = ClusterHosts::createForQuerySuggestions($config->getAppId());
69+
$clusterHosts = ClusterHosts::create('query-suggestions.'.$config->getRegion().'.algolia.com');
7070
}
7171

7272
$apiWrapper = new ApiWrapper(

clients/algoliasearch-client-php/lib/Api/RecommendApi.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ public function __construct(ApiWrapperInterface $apiWrapper, RecommendConfig $co
3838
}
3939

4040
/**
41-
* Instantiate the client with basic credentials and region
41+
* Instantiate the client with basic credentials
4242
*
4343
* @param string $appId Application ID
4444
* @param string $apiKey Algolia API Key
45-
* @param string $region Region
4645
*/
47-
public static function create($appId = null, $apiKey = null, $region = null)
46+
public static function create($appId = null, $apiKey = null)
4847
{
49-
$allowedRegions = explode('-', 'us-de');
50-
$config = RecommendConfig::create($appId, $apiKey, $region, $allowedRegions);
51-
52-
return static::createWithConfig($config);
48+
return static::createWithConfig(RecommendConfig::create($appId, $apiKey));
5349
}
5450

5551
/**
@@ -61,11 +57,16 @@ public static function createWithConfig(RecommendConfig $config)
6157
{
6258
$config = clone $config;
6359

60+
$cacheKey = sprintf('%s-clusterHosts-%s', __CLASS__, $config->getAppId());
61+
6462
if ($hosts = $config->getHosts()) {
6563
// If a list of hosts was passed, we ignore the cache
6664
$clusterHosts = ClusterHosts::create($hosts);
67-
} else {
68-
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId());
65+
} elseif (false === ($clusterHosts = ClusterHosts::createFromCache($cacheKey))) {
66+
// We'll try to restore the ClusterHost from cache, if we cannot
67+
// we create a new instance and set the cache key
68+
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId())
69+
->setCacheKey($cacheKey);
6970
}
7071

7172
$apiWrapper = new ApiWrapper(

clients/algoliasearch-client-php/lib/RetryStrategy/ClusterHosts.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,6 @@ public static function createFromAppId($applicationId)
6464
return static::create($read, $write);
6565
}
6666

67-
public static function createForAnalytics($region)
68-
{
69-
return static::create('analytics.'.$region.'.algolia.com');
70-
}
71-
72-
public static function createForInsights($region)
73-
{
74-
return static::create('insights.'.$region.'.algolia.io');
75-
}
76-
77-
public static function createForRecommendation($region)
78-
{
79-
return static::create('recommendation.'.$region.'.algolia.com');
80-
}
81-
82-
public static function createForQuerySuggestions($region)
83-
{
84-
return static::create('query-suggestions.'.$region.'.algolia.com');
85-
}
86-
8767
public static function createFromCache($cacheKey)
8868
{
8969
if (!Algolia::isCacheEnabled()) {

openapitools.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@
227227
"modelPackage": "Model\\Search",
228228
"additionalProperties": {
229229
"configClassname": "SearchConfig",
230-
"clusterHostsMethod": "createFromAppId",
231230
"useCache": true,
232231
"variableNamingConvention": "camelCase",
233232
"packageName": "algoliasearch-client-php"
@@ -246,9 +245,7 @@
246245
"modelPackage": "Model\\Recommend",
247246
"additionalProperties": {
248247
"configClassname": "RecommendConfig",
249-
"clusterHostsMethod": "createFromAppId",
250-
"hasRegionalHost": true,
251-
"allowedRegions": "us-de",
248+
"useCache": true,
252249
"variableNamingConvention": "camelCase",
253250
"packageName": "algoliasearch-client-php"
254251
}
@@ -266,7 +263,6 @@
266263
"modelPackage": "Model\\Personalization",
267264
"additionalProperties": {
268265
"configClassname": "PersonalizationConfig",
269-
"clusterHostsMethod": "createForRecommendation",
270266
"hasRegionalHost": true,
271267
"allowedRegions": "us-eu",
272268
"variableNamingConvention": "camelCase",
@@ -289,7 +285,6 @@
289285
"modelPackage": "Model\\Analytics",
290286
"additionalProperties": {
291287
"configClassname": "AnalyticsConfig",
292-
"clusterHostsMethod": "createForAnalytics",
293288
"hasRegionalHost": true,
294289
"allowedRegions": "us-de",
295290
"variableNamingConvention": "camelCase",
@@ -313,7 +308,6 @@
313308
"modelPackage": "Model\\Insights",
314309
"additionalProperties": {
315310
"configClassname": "InsightsConfig",
316-
"clusterHostsMethod": "createForInsights",
317311
"hasRegionalHost": true,
318312
"allowedRegions": "us-de",
319313
"variableNamingConvention": "camelCase",
@@ -337,7 +331,6 @@
337331
"modelPackage": "Model\\ABTesting",
338332
"additionalProperties": {
339333
"configClassname": "AbTestingConfig",
340-
"clusterHostsMethod": "createForAnalytics",
341334
"hasRegionalHost": true,
342335
"allowedRegions": "us-de",
343336
"variableNamingConvention": "camelCase",
@@ -361,7 +354,6 @@
361354
"modelPackage": "Model\\QuerySuggestions",
362355
"additionalProperties": {
363356
"configClassname": "QuerySuggestionsConfig",
364-
"clusterHostsMethod": "createForQuerySuggestions",
365357
"hasRegionalHost": true,
366358
"allowedRegions": "us-eu",
367359
"variableNamingConvention": "camelCase",

templates/php/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
9292
} elseif (false === ($clusterHosts = ClusterHosts::createFromCache($cacheKey))) {
9393
// We'll try to restore the ClusterHost from cache, if we cannot
9494
// we create a new instance and set the cache key
95-
$clusterHosts = ClusterHosts::{{clusterHostsMethod}}($config->getAppId())
95+
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId())
9696
->setCacheKey($cacheKey);
9797
}
9898
{{/useCache}}
@@ -102,7 +102,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
102102
// If a list of hosts was passed, we ignore the cache
103103
$clusterHosts = ClusterHosts::create($hosts);
104104
} else {
105-
$clusterHosts = ClusterHosts::{{clusterHostsMethod}}($config->getAppId());
105+
$clusterHosts = ClusterHosts::create('{{host}}.'.$config->getRegion().'.algolia.{{topLevelDomain}}');
106106
}
107107
{{/useCache}}
108108

0 commit comments

Comments
 (0)