Skip to content

Commit

Permalink
fix(php): Use api config to generate hosts (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
damcou authored Feb 10, 2022
1 parent 5854261 commit e21f6dc
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 45 deletions.
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-php/lib/Api/AbtestingApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function createWithConfig(AbTestingConfig $config)
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::createForAnalytics($config->getAppId());
$clusterHosts = ClusterHosts::create('analytics.'.$config->getRegion().'.algolia.com');
}

$apiWrapper = new ApiWrapper(
Expand Down
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-php/lib/Api/AnalyticsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function createWithConfig(AnalyticsConfig $config)
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::createForAnalytics($config->getAppId());
$clusterHosts = ClusterHosts::create('analytics.'.$config->getRegion().'.algolia.com');
}

$apiWrapper = new ApiWrapper(
Expand Down
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-php/lib/Api/InsightsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function createWithConfig(InsightsConfig $config)
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::createForInsights($config->getAppId());
$clusterHosts = ClusterHosts::create('insights.'.$config->getRegion().'.algolia.io');
}

$apiWrapper = new ApiWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function createWithConfig(PersonalizationConfig $config)
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::createForRecommendation($config->getAppId());
$clusterHosts = ClusterHosts::create('personalization.'.$config->getRegion().'.algolia.com');
}

$apiWrapper = new ApiWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function createWithConfig(QuerySuggestionsConfig $config)
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::createForQuerySuggestions($config->getAppId());
$clusterHosts = ClusterHosts::create('query-suggestions.'.$config->getRegion().'.algolia.com');
}

$apiWrapper = new ApiWrapper(
Expand Down
19 changes: 10 additions & 9 deletions clients/algoliasearch-client-php/lib/Api/RecommendApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@ public function __construct(ApiWrapperInterface $apiWrapper, RecommendConfig $co
}

/**
* Instantiate the client with basic credentials and region
* Instantiate the client with basic credentials
*
* @param string $appId Application ID
* @param string $apiKey Algolia API Key
* @param string $region Region
*/
public static function create($appId = null, $apiKey = null, $region = null)
public static function create($appId = null, $apiKey = null)
{
$allowedRegions = explode('-', 'us-de');
$config = RecommendConfig::create($appId, $apiKey, $region, $allowedRegions);

return static::createWithConfig($config);
return static::createWithConfig(RecommendConfig::create($appId, $apiKey));
}

/**
Expand All @@ -61,11 +57,16 @@ public static function createWithConfig(RecommendConfig $config)
{
$config = clone $config;

$cacheKey = sprintf('%s-clusterHosts-%s', __CLASS__, $config->getAppId());

if ($hosts = $config->getHosts()) {
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId());
} elseif (false === ($clusterHosts = ClusterHosts::createFromCache($cacheKey))) {
// We'll try to restore the ClusterHost from cache, if we cannot
// we create a new instance and set the cache key
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId())
->setCacheKey($cacheKey);
}

$apiWrapper = new ApiWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,6 @@ public static function createFromAppId($applicationId)
return static::create($read, $write);
}

public static function createForAnalytics($region)
{
return static::create('analytics.'.$region.'.algolia.com');
}

public static function createForInsights($region)
{
return static::create('insights.'.$region.'.algolia.io');
}

public static function createForRecommendation($region)
{
return static::create('recommendation.'.$region.'.algolia.com');
}

public static function createForQuerySuggestions($region)
{
return static::create('query-suggestions.'.$region.'.algolia.com');
}

public static function createFromCache($cacheKey)
{
if (!Algolia::isCacheEnabled()) {
Expand Down
10 changes: 1 addition & 9 deletions openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
"modelPackage": "Model\\Search",
"additionalProperties": {
"configClassname": "SearchConfig",
"clusterHostsMethod": "createFromAppId",
"useCache": true,
"variableNamingConvention": "camelCase",
"packageName": "algoliasearch-client-php"
Expand All @@ -246,9 +245,7 @@
"modelPackage": "Model\\Recommend",
"additionalProperties": {
"configClassname": "RecommendConfig",
"clusterHostsMethod": "createFromAppId",
"hasRegionalHost": true,
"allowedRegions": "us-de",
"useCache": true,
"variableNamingConvention": "camelCase",
"packageName": "algoliasearch-client-php"
}
Expand All @@ -266,7 +263,6 @@
"modelPackage": "Model\\Personalization",
"additionalProperties": {
"configClassname": "PersonalizationConfig",
"clusterHostsMethod": "createForRecommendation",
"hasRegionalHost": true,
"allowedRegions": "us-eu",
"variableNamingConvention": "camelCase",
Expand All @@ -289,7 +285,6 @@
"modelPackage": "Model\\Analytics",
"additionalProperties": {
"configClassname": "AnalyticsConfig",
"clusterHostsMethod": "createForAnalytics",
"hasRegionalHost": true,
"allowedRegions": "us-de",
"variableNamingConvention": "camelCase",
Expand All @@ -313,7 +308,6 @@
"modelPackage": "Model\\Insights",
"additionalProperties": {
"configClassname": "InsightsConfig",
"clusterHostsMethod": "createForInsights",
"hasRegionalHost": true,
"allowedRegions": "us-de",
"variableNamingConvention": "camelCase",
Expand All @@ -337,7 +331,6 @@
"modelPackage": "Model\\ABTesting",
"additionalProperties": {
"configClassname": "AbTestingConfig",
"clusterHostsMethod": "createForAnalytics",
"hasRegionalHost": true,
"allowedRegions": "us-de",
"variableNamingConvention": "camelCase",
Expand All @@ -361,7 +354,6 @@
"modelPackage": "Model\\QuerySuggestions",
"additionalProperties": {
"configClassname": "QuerySuggestionsConfig",
"clusterHostsMethod": "createForQuerySuggestions",
"hasRegionalHost": true,
"allowedRegions": "us-eu",
"variableNamingConvention": "camelCase",
Expand Down
4 changes: 2 additions & 2 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
} elseif (false === ($clusterHosts = ClusterHosts::createFromCache($cacheKey))) {
// We'll try to restore the ClusterHost from cache, if we cannot
// we create a new instance and set the cache key
$clusterHosts = ClusterHosts::{{clusterHostsMethod}}($config->getAppId())
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId())
->setCacheKey($cacheKey);
}
{{/useCache}}
Expand All @@ -102,7 +102,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::{{clusterHostsMethod}}($config->getAppId());
$clusterHosts = ClusterHosts::create('{{host}}.'.$config->getRegion().'.algolia.{{topLevelDomain}}');
}
{{/useCache}}

Expand Down

0 comments on commit e21f6dc

Please sign in to comment.