Skip to content

Commit 68ad731

Browse files
authored
fix(generator): add hostWithFallback (#479)
1 parent 22ee27e commit 68ad731

File tree

9 files changed

+30
-46
lines changed

9 files changed

+30
-46
lines changed

generators/src/main/java/com/algolia/codegen/Utils.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static void generateServer(
8585
boolean hasRegionalHost = false;
8686
boolean fallbackToAliasHost = false;
8787
String host = "";
88-
String topLevelDomain = "";
88+
String hostWithFallback = "";
8989
Set<String> allowedRegions = new HashSet<>();
9090
for (Map<String, Object> server : servers) {
9191
if (!server.containsKey("url")) {
@@ -102,7 +102,9 @@ public static void generateServer(
102102
}
103103
String otherUrl = (String) otherServer.getOrDefault("url", "");
104104
if (otherUrl.replace(".{region}", "").equals(server.get("url"))) {
105+
URL fallbackURL = new URL(otherUrl.replace(".{region}", ""));
105106
fallbackToAliasHost = true;
107+
hostWithFallback = fallbackURL.getHost();
106108
break;
107109
}
108110
}
@@ -132,26 +134,16 @@ public static void generateServer(
132134

133135
// This is used for hosts like `insights` that uses `.io`
134136
URL url = new URL((String) server.get("url"));
135-
String[] hostParts = url.getHost().split("\\.");
136-
host = hostParts[0];
137-
topLevelDomain = hostParts[hostParts.length - 1];
137+
host = url.getHost();
138138
}
139+
additionalProperties.put("hostWithFallback", hostWithFallback);
139140
additionalProperties.put("hasRegionalHost", hasRegionalHost);
140141
additionalProperties.put("fallbackToAliasHost", fallbackToAliasHost);
141142
additionalProperties.put("host", host);
142-
additionalProperties.put("topLevelDomain", topLevelDomain);
143143
additionalProperties.put(
144144
"allowedRegions",
145145
allowedRegions.toArray(new String[0])
146146
);
147-
148-
if (clientKebab.equals("predict")) {
149-
additionalProperties.put("isExperimentalHost", true);
150-
additionalProperties.put(
151-
"host",
152-
new URL((String) servers.get(0).get("url")).getHost()
153-
);
154-
}
155147
} catch (Exception e) {
156148
e.printStackTrace();
157149
System.exit(1);

scripts/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function generate(
3838

3939
await generateOpenapitools(generators);
4040

41-
const availableWorkspaces = await run('yarn workspaces list', { verbose });
41+
const availableWorkspaces = await run('yarn workspaces list');
4242
const langs = [...new Set(generators.map((gen) => gen.language))];
4343
const useCustomGenerator = langs
4444
.map((lang) => getCustomGenerator(lang))

specs/predict/spec.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ components:
1010
apiKey:
1111
$ref: '../common/securitySchemes.yml#/apiKey'
1212
servers:
13-
- url: https://predict-api-432xa6wemq-ew.a.run.app
13+
- url: https://predict-api-432xa6wemq-{region}.a.run.app
14+
variables:
15+
region:
16+
enum:
17+
- ew
18+
default: ew
1419
security:
1520
- appId: []
1621
apiKey: []

templates/java/libraries/okhttp-gson/api.mustache

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public class {{classname}} extends ApiClient {
5050

5151
{{^hasRegionalHost}}
5252
public {{classname}}(String appId, String apiKey) {
53-
this(appId, apiKey, new HttpRequester(getDefaultHosts({{^isExperimentalHost}}appId{{/isExperimentalHost}})), null);
53+
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), null);
5454
}
5555

5656
public {{classname}}(String appId, String apiKey, UserAgent.Segment[] userAgentSegments) {
57-
this(appId, apiKey, new HttpRequester(getDefaultHosts({{^isExperimentalHost}}appId{{/isExperimentalHost}})), userAgentSegments);
57+
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), userAgentSegments);
5858
}
5959
{{/hasRegionalHost}}
6060

@@ -66,7 +66,7 @@ public class {{classname}} extends ApiClient {
6666
super(appId, apiKey, requester, "{{{baseName}}}", userAgentSegments);
6767
}
6868

69-
{{^hasRegionalHost}}{{^isExperimentalHost}}
69+
{{^hasRegionalHost}}
7070
private static List<StatefulHost> getDefaultHosts(String appId) {
7171
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
7272
hosts.add(new StatefulHost(appId + "-dsn.algolia.net", "https", EnumSet.of(CallType.READ)));
@@ -81,20 +81,15 @@ public class {{classname}} extends ApiClient {
8181
8282
return Stream.concat(hosts.stream(), commonHosts.stream()).collect(Collectors.toList());
8383
}
84-
{{/isExperimentalHost}}{{/hasRegionalHost}}
85-
86-
{{#isExperimentalHost}}
87-
private static List<StatefulHost> getDefaultHosts() {
88-
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
89-
hosts.add(new StatefulHost("{{{host}}}", "https", EnumSet.of(CallType.READ, CallType.WRITE)));
90-
return hosts;
91-
}
92-
{{/isExperimentalHost}}
84+
{{/hasRegionalHost}}
9385

9486
{{#hasRegionalHost}}
9587
private static List<StatefulHost> getDefaultHosts(String region) {
9688
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
97-
hosts.add(new StatefulHost("{{{host}}}." + {{#fallbackToAliasHost}}(region == null ? "" : region + "."){{/fallbackToAliasHost}}{{^fallbackToAliasHost}}region{{/fallbackToAliasHost}} + "algolia.{{#topLevelDomain}}{{.}}{{/topLevelDomain}}{{^topLevelDomain}}com{{/topLevelDomain}}", "https", EnumSet.of(CallType.READ, CallType.WRITE)));
89+
90+
String url = {{#fallbackToAliasHost}}region == null ? "{{{hostWithFallback}}}" : {{/fallbackToAliasHost}} "{{{host}}}".replace("{region}", region);
91+
92+
hosts.add(new StatefulHost(url, "https", EnumSet.of(CallType.READ, CallType.WRITE)));
9893
return hosts;
9994
}
10095
{{/hasRegionalHost}}

templates/javascript/api-single.mustache

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const apiClientVersion = '{{packageVersion}}';
3131
export type Region = {{#allowedRegions}}'{{.}}'{{^-last}}|{{/-last}}{{/allowedRegions}};
3232
{{/hasRegionalHost}}
3333

34-
{{^hasRegionalHost}}{{^isExperimentalHost}}
34+
{{^hasRegionalHost}}
3535
function getDefaultHosts(appId: string): Host[] {
3636
return (
3737
[
@@ -66,27 +66,21 @@ function getDefaultHosts(appId: string): Host[] {
6666
])
6767
);
6868
}
69-
{{/isExperimentalHost}}{{/hasRegionalHost}}
70-
71-
{{#isExperimentalHost}}
72-
function getDefaultHosts(): Host[] {
73-
return [{ url: '{{host}}', accept: 'readWrite', protocol: 'https' }];
74-
}
75-
{{/isExperimentalHost}}
69+
{{/hasRegionalHost}}
7670

7771
{{#hasRegionalHost}}
7872
function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region): Host[] {
79-
{{#fallbackToAliasHost}}const regionHost = region ? `.${region}.` : '.';{{/fallbackToAliasHost}}
73+
const url = {{#fallbackToAliasHost}}!region ? '{{{hostWithFallback}}}' : {{/fallbackToAliasHost}} '{{{host}}}'.replace('{region}', region);
8074

81-
return [{ url: `{{{host}}}{{#fallbackToAliasHost}}${regionHost}{{/fallbackToAliasHost}}{{^fallbackToAliasHost}}.${region}.{{/fallbackToAliasHost}}algolia.{{#topLevelDomain}}{{.}}{{/topLevelDomain}}{{^topLevelDomain}}com{{/topLevelDomain}}`, accept: 'readWrite', protocol: 'https' }];
75+
return [{ url, accept: 'readWrite', protocol: 'https' }];
8276
}
8377
{{/hasRegionalHost}}
8478

8579
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
8680
export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
8781
const auth = createAuth(options.appId, options.apiKey, options.authMode);
8882
const transporter = createTransporter({
89-
hosts: options?.hosts ?? getDefaultHosts({{^hasRegionalHost}}{{^isExperimentalHost}}options.appId{{/isExperimentalHost}}{{/hasRegionalHost}}{{#hasRegionalHost}}options.region{{/hasRegionalHost}}),
83+
hosts: options?.hosts ?? getDefaultHosts({{^hasRegionalHost}}options.appId{{/hasRegionalHost}}{{#hasRegionalHost}}options.region{{/hasRegionalHost}}),
9084
hostsCache: options.hostsCache,
9185
requestsCache: options.requestsCache,
9286
responsesCache: options.responsesCache,

templates/php/api.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ 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::create('{{host}}.'.$config->getRegion().'.algolia.{{topLevelDomain}}');
105+
$url = str_replace('{region}', $config->getRegion(), '{{{host}}}');
106+
$clusterHosts = ClusterHosts::create($url);
106107
}
107108
{{/useCache}}
108109

tests/CTS/client/abtesting/parameters.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"type": "createClient",
88
"parameters": {
99
"appId": "my-app-id",
10-
"apiKey": "my-api-key",
11-
"region": ""
10+
"apiKey": "my-api-key"
1211
},
1312
"expected": {
1413
"error": false

tests/CTS/client/analytics/parameters.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"type": "createClient",
88
"parameters": {
99
"appId": "my-app-id",
10-
"apiKey": "my-api-key",
11-
"region": ""
10+
"apiKey": "my-api-key"
1211
},
1312
"expected": {
1413
"error": false

tests/CTS/client/insights/parameters.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"type": "createClient",
88
"parameters": {
99
"appId": "my-app-id",
10-
"apiKey": "my-api-key",
11-
"region": ""
10+
"apiKey": "my-api-key"
1211
},
1312
"expected": {
1413
"error": false

0 commit comments

Comments
 (0)