Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clients): list available regions when region is missing #916

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions templates/java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,7 @@ public class {{classname}} extends ApiClient {
private static List<StatefulHost> getDefaultHosts(String region) throws AlgoliaRuntimeException {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();

{{^fallbackToAliasHost}}
boolean found = false;
if (region == null) {
throw new AlgoliaRuntimeException("`region` is missing");
}
for (String allowed : allowedRegions) {
if (allowed.equals(region)) {
found = true;
break;
}
}
{{/fallbackToAliasHost}}
{{#fallbackToAliasHost}}
boolean found = region == null;
boolean found = {{^fallbackToAliasHost}}false{{/fallbackToAliasHost}}{{#fallbackToAliasHost}}region == null{{/fallbackToAliasHost}};
if (region != null) {
for (String allowed : allowedRegions) {
if (allowed.equals(region)) {
Expand All @@ -121,9 +108,9 @@ public class {{classname}} extends ApiClient {
}
}
}
{{/fallbackToAliasHost}}
if (!found) {
throw new AlgoliaRuntimeException("`region` must be one of the following: {{#allowedRegions}}{{.}}{{^-last}}, {{/-last}}{{/allowedRegions}}");

if ({{^fallbackToAliasHost}}region == null || {{/fallbackToAliasHost}}!found){
throw new AlgoliaRuntimeException("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: {{#allowedRegions}}{{.}}{{^-last}}, {{/-last}}{{/allowedRegions}}");
}

String url = {{#fallbackToAliasHost}}region == null ? "{{{hostWithFallback}}}" : {{/fallbackToAliasHost}} "{{{host}}}".replace("{region}", region);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ function initAbtesting(initOptions: InitClientOptions & InitClientRegion<Abtesti
}

function initPersonalization(initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>): PersonalizationClient {
if (!initOptions.region) {
throw new Error('`region` is missing.');
}

if (
initOptions.region &&
!initOptions.region || (initOptions.region &&
(typeof initOptions.region !== 'string' ||
!personalizationRegions.includes(initOptions.region))
!personalizationRegions.includes(initOptions.region)))
) {
throw new Error(
`\`region\` must be one of the following: ${personalizationRegions.join(', ')}`
`\`region\` is required and must be one of the following: ${personalizationRegions.join(', ')}`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ if (!apiKey || typeof apiKey !== 'string') {
}

{{#hasRegionalHost}}
{{^fallbackToAliasHost}}
if (!region) {
throw new Error("`region` is missing.");
}
{{/fallbackToAliasHost}}

if (region && (typeof region !== 'string' || !REGIONS.includes(region))) {
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
if ({{^fallbackToAliasHost}}!region || {{/fallbackToAliasHost}}(region && (typeof region !== 'string' || !REGIONS.includes(region)))) {
throw new Error(`\`region\` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ${REGIONS.join(', ')}`);
}
{{/hasRegionalHost}}
19 changes: 2 additions & 17 deletions templates/php/ConfigWithRegion.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,8 @@ use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;

abstract class ConfigWithRegion extends Configuration
{
public static function create(
$appId,
$apiKey,
$region = null,
$allowedRegions = null
) {
if (
$region !== null &&
$allowedRegions !== null &&
!in_array($region, $allowedRegions, true)
) {
throw new AlgoliaException(
'`region` must be one of the following: ' .
implode(', ', $allowedRegions)
);
}

public static function create($appId, $apiKey, $region = null)
{
$config = [
'appId' => $appId,
'apiKey' => $apiKey,
Expand Down
23 changes: 13 additions & 10 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ use {{invokerPackage}}\Support\Helpers;
*/
public static function create($appId = null, $apiKey = null, $region = null)
{
$allowedRegions = self::getAllowedRegions();
$config = {{configClassname}}::create($appId, $apiKey, $region, $allowedRegions);
$allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];

if (
{{^fallbackToAliasHost}}$region === null ||{{/fallbackToAliasHost}}
($region !== null && !in_array($region, $allowedRegions, true))
) {
throw new AlgoliaException(
'`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ' .
implode(', ', $allowedRegions)
);
}

return static::createWithConfig($config);
}
$config = {{configClassname}}::create($appId, $apiKey, $region);

/**
* Returns the allowed regions for the config
*/
public static function getAllowedRegions()
{
return [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];
return static::createWithConfig($config);
}
{{/hasRegionalHost}}

Expand Down
3 changes: 1 addition & 2 deletions templates/php/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
$config = {{clientPrefix}}Config::create(
$appId,
$apiKey,
$region,
{{client}}::getAllowedRegions()
$region
);
$clusterHosts = {{client}}::getClusterHosts($config);
{{/hasRegionalHost}}
Expand Down
19 changes: 18 additions & 1 deletion tests/CTS/client/personalization/parameters.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
},
{
"testName": "throws when incorrect region is given",
"autoCreateClient": false,
Expand All @@ -11,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: eu, us"
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
Expand Down
19 changes: 18 additions & 1 deletion tests/CTS/client/predict/parameters.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is required and must be one of the following: ue, ew"
}
}
]
},
{
"testName": "throws when incorrect region is given",
"autoCreateClient": false,
Expand All @@ -11,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: ue, ew"
"error": "`region` is required and must be one of the following: ue, ew"
}
}
]
Expand Down
19 changes: 18 additions & 1 deletion tests/CTS/client/query-suggestions/parameters.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
},
{
"testName": "throws when incorrect region is given",
"autoCreateClient": false,
Expand All @@ -11,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: eu, us"
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/CTS/client/sources/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"region": ""
},
"expected": {
"error": "`region` is missing."
"error": "`region` is required and must be one of the following: de, us"
}
}
]
Expand All @@ -28,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: de, us"
"error": "`region` is required and must be one of the following: de, us"
}
}
]
Expand Down