Skip to content

Commit 4c1596d

Browse files
authored
fix(clients): list available regions when region is missing (#916)
1 parent be45fc4 commit 4c1596d

File tree

10 files changed

+81
-65
lines changed

10 files changed

+81
-65
lines changed

templates/java/api.mustache

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,7 @@ public class {{classname}} extends ApiClient {
9999
private static List<StatefulHost> getDefaultHosts(String region) throws AlgoliaRuntimeException {
100100
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
101101
102-
{{^fallbackToAliasHost}}
103-
boolean found = false;
104-
if (region == null) {
105-
throw new AlgoliaRuntimeException("`region` is missing");
106-
}
107-
for (String allowed : allowedRegions) {
108-
if (allowed.equals(region)) {
109-
found = true;
110-
break;
111-
}
112-
}
113-
{{/fallbackToAliasHost}}
114-
{{#fallbackToAliasHost}}
115-
boolean found = region == null;
102+
boolean found = {{^fallbackToAliasHost}}false{{/fallbackToAliasHost}}{{#fallbackToAliasHost}}region == null{{/fallbackToAliasHost}};
116103
if (region != null) {
117104
for (String allowed : allowedRegions) {
118105
if (allowed.equals(region)) {
@@ -121,9 +108,9 @@ public class {{classname}} extends ApiClient {
121108
}
122109
}
123110
}
124-
{{/fallbackToAliasHost}}
125-
if (!found) {
126-
throw new AlgoliaRuntimeException("`region` must be one of the following: {{#allowedRegions}}{{.}}{{^-last}}, {{/-last}}{{/allowedRegions}}");
111+
112+
if ({{^fallbackToAliasHost}}region == null || {{/fallbackToAliasHost}}!found){
113+
throw new AlgoliaRuntimeException("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: {{#allowedRegions}}{{.}}{{^-last}}, {{/-last}}{{/allowedRegions}}");
127114
}
128115

129116
String url = {{#fallbackToAliasHost}}region == null ? "{{{hostWithFallback}}}" : {{/fallbackToAliasHost}} "{{{host}}}".replace("{region}", region);

templates/javascript/clients/algoliasearch/builds/initClients.mustache

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ function initAbtesting(initOptions: InitClientOptions & InitClientRegion<Abtesti
3535
}
3636

3737
function initPersonalization(initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>): PersonalizationClient {
38-
if (!initOptions.region) {
39-
throw new Error('`region` is missing.');
40-
}
41-
4238
if (
43-
initOptions.region &&
39+
!initOptions.region || (initOptions.region &&
4440
(typeof initOptions.region !== 'string' ||
45-
!personalizationRegions.includes(initOptions.region))
41+
!personalizationRegions.includes(initOptions.region)))
4642
) {
4743
throw new Error(
48-
`\`region\` must be one of the following: ${personalizationRegions.join(', ')}`
44+
`\`region\` is required and must be one of the following: ${personalizationRegions.join(', ')}`
4945
);
5046
}
5147

templates/javascript/clients/client/builds/checkParameters.mustache

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ if (!apiKey || typeof apiKey !== 'string') {
77
}
88

99
{{#hasRegionalHost}}
10-
{{^fallbackToAliasHost}}
11-
if (!region) {
12-
throw new Error("`region` is missing.");
13-
}
14-
{{/fallbackToAliasHost}}
1510

16-
if (region && (typeof region !== 'string' || !REGIONS.includes(region))) {
17-
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
11+
if ({{^fallbackToAliasHost}}!region || {{/fallbackToAliasHost}}(region && (typeof region !== 'string' || !REGIONS.includes(region)))) {
12+
throw new Error(`\`region\` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ${REGIONS.join(', ')}`);
1813
}
1914
{{/hasRegionalHost}}

templates/php/ConfigWithRegion.mustache

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,8 @@ use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
77

88
abstract class ConfigWithRegion extends Configuration
99
{
10-
public static function create(
11-
$appId,
12-
$apiKey,
13-
$region = null,
14-
$allowedRegions = null
15-
) {
16-
if (
17-
$region !== null &&
18-
$allowedRegions !== null &&
19-
!in_array($region, $allowedRegions, true)
20-
) {
21-
throw new AlgoliaException(
22-
'`region` must be one of the following: ' .
23-
implode(', ', $allowedRegions)
24-
);
25-
}
26-
10+
public static function create($appId, $apiKey, $region = null)
11+
{
2712
$config = [
2813
'appId' => $appId,
2914
'apiKey' => $apiKey,

templates/php/api.mustache

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,21 @@ use {{invokerPackage}}\Support\Helpers;
5151
*/
5252
public static function create($appId = null, $apiKey = null, $region = null)
5353
{
54-
$allowedRegions = self::getAllowedRegions();
55-
$config = {{configClassname}}::create($appId, $apiKey, $region, $allowedRegions);
54+
$allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];
55+
56+
if (
57+
{{^fallbackToAliasHost}}$region === null ||{{/fallbackToAliasHost}}
58+
($region !== null && !in_array($region, $allowedRegions, true))
59+
) {
60+
throw new AlgoliaException(
61+
'`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ' .
62+
implode(', ', $allowedRegions)
63+
);
64+
}
5665

57-
return static::createWithConfig($config);
58-
}
66+
$config = {{configClassname}}::create($appId, $apiKey, $region);
5967

60-
/**
61-
* Returns the allowed regions for the config
62-
*/
63-
public static function getAllowedRegions()
64-
{
65-
return [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];
68+
return static::createWithConfig($config);
6669
}
6770
{{/hasRegionalHost}}
6871

templates/php/tests/client/suite.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
3737
$config = {{clientPrefix}}Config::create(
3838
$appId,
3939
$apiKey,
40-
$region,
41-
{{client}}::getAllowedRegions()
40+
$region
4241
);
4342
$clusterHosts = {{client}}::getClusterHosts($config);
4443
{{/hasRegionalHost}}

tests/CTS/client/personalization/parameters.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
[
2+
{
3+
"testName": "throws when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": "`region` is required and must be one of the following: eu, us"
15+
}
16+
}
17+
]
18+
},
219
{
320
"testName": "throws when incorrect region is given",
421
"autoCreateClient": false,
@@ -11,7 +28,7 @@
1128
"region": "not_a_region"
1229
},
1330
"expected": {
14-
"error": "`region` must be one of the following: eu, us"
31+
"error": "`region` is required and must be one of the following: eu, us"
1532
}
1633
}
1734
]

tests/CTS/client/predict/parameters.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
[
2+
{
3+
"testName": "throws when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": "`region` is required and must be one of the following: ue, ew"
15+
}
16+
}
17+
]
18+
},
219
{
320
"testName": "throws when incorrect region is given",
421
"autoCreateClient": false,
@@ -11,7 +28,7 @@
1128
"region": "not_a_region"
1229
},
1330
"expected": {
14-
"error": "`region` must be one of the following: ue, ew"
31+
"error": "`region` is required and must be one of the following: ue, ew"
1532
}
1633
}
1734
]

tests/CTS/client/query-suggestions/parameters.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
[
2+
{
3+
"testName": "throws when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": "`region` is required and must be one of the following: eu, us"
15+
}
16+
}
17+
]
18+
},
219
{
320
"testName": "throws when incorrect region is given",
421
"autoCreateClient": false,
@@ -11,7 +28,7 @@
1128
"region": "not_a_region"
1229
},
1330
"expected": {
14-
"error": "`region` must be one of the following: eu, us"
31+
"error": "`region` is required and must be one of the following: eu, us"
1532
}
1633
}
1734
]

tests/CTS/client/sources/parameters.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"region": ""
1212
},
1313
"expected": {
14-
"error": "`region` is missing."
14+
"error": "`region` is required and must be one of the following: de, us"
1515
}
1616
}
1717
]
@@ -28,7 +28,7 @@
2828
"region": "not_a_region"
2929
},
3030
"expected": {
31-
"error": "`region` must be one of the following: de, us"
31+
"error": "`region` is required and must be one of the following: de, us"
3232
}
3333
}
3434
]

0 commit comments

Comments
 (0)