Skip to content

Commit c0675ff

Browse files
authoredAug 30, 2024··
fix(clients): correly parse usage hosts (#3622)
1 parent a5888bc commit c0675ff

File tree

13 files changed

+85
-43
lines changed

13 files changed

+85
-43
lines changed
 

‎generators/src/main/java/com/algolia/codegen/utils/Helpers.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public static void generateServers(List<CodegenServer> servers, Map<String, Obje
9292
try {
9393
boolean hasRegionalHost = false;
9494
boolean fallbackToAliasHost = false;
95+
boolean hasVariables = false;
9596
String regionalHost = "";
9697
String hostWithFallback = "";
9798
Set<String> allowedRegions = new HashSet<>();
@@ -117,6 +118,7 @@ public static void generateServers(List<CodegenServer> servers, Map<String, Obje
117118
if (server.variables == null || server.variables.isEmpty()) {
118119
continue;
119120
}
121+
hasVariables = true;
120122
CodegenServerVariable regionVar = server.variables.stream().filter(v -> v.name.equals("region")).findFirst().orElse(null);
121123
if (regionVar == null || regionVar.enumValues == null || regionVar.enumValues.isEmpty()) {
122124
continue;
@@ -133,9 +135,13 @@ public static void generateServers(List<CodegenServer> servers, Map<String, Obje
133135
}
134136

135137
if (!hasRegionalHost) {
136-
if (servers.size() == 1 && hostWithFallback.isEmpty()) {
137-
URL url = new URL(servers.get(0).url);
138-
bundle.put("uniqueHost", url.getHost());
138+
if (!hasVariables && hostWithFallback.isEmpty()) {
139+
List<String> hostsWithoutVariables = new ArrayList<>();
140+
for (CodegenServer otherServer : servers) {
141+
URL url = new URL(otherServer.url);
142+
hostsWithoutVariables.add(url.getHost());
143+
}
144+
bundle.put("hostsWithoutVariables", hostsWithoutVariables);
139145
} else {
140146
bundle.put("hostWithAppID", true);
141147
}

‎templates/csharp/Configuration.mustache

+6-5
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,21 @@ private static List<StatefulHost> GetDefaultHosts(string region)
116116
return hosts;
117117
}
118118
{{/hasRegionalHost}}
119-
{{#uniqueHost}}
119+
{{#hostsWithoutVariables.size}}
120120
private static List<StatefulHost> GetDefaultHosts()
121121
{
122122
return new List<StatefulHost>
123123
{
124+
{{#hostsWithoutVariables}}
124125
new()
125126
{
126127
Url = "{{{.}}}",
127128
Up = true,
128129
LastUse = DateTime.UtcNow,
129130
Accept = CallType.Read | CallType.Write
130-
}
131+
},
132+
{{/hostsWithoutVariables}}
131133
};
132134
}
133-
{{/uniqueHost}}
134-
}
135-
135+
{{/hostsWithoutVariables.size}}
136+
}

‎templates/dart/api.mustache

+8-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,14 @@ final class {{classname}} implements ApiClient {
6767
}
6868
{{/hasRegionalHost}}
6969
{{#uniqueHost}}
70-
defaultHosts: () => [Host(url: '{{{.}}}')],
7170
{{/uniqueHost}}
71+
{{#hostsWithoutVariables.size}}
72+
defaultHosts: () => [
73+
{{#hostsWithoutVariables}}
74+
Host(url: '{{{.}}}'),
75+
{{/hostsWithoutVariables}}
76+
],
77+
{{/hostsWithoutVariables.size}}
7278
) {
7379
assert(appId.isNotEmpty, '`appId` is missing.');
7480
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
@@ -208,4 +214,4 @@ final class {{classname}} implements ApiClient {
208214
@override
209215
void dispose() => _retryStrategy.dispose();
210216
}
211-
{{/operations}}
217+
{{/operations}}

‎templates/go/client.mustache

+7-3
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ func getDefaultHosts(appID string) []transport.StatefulHost {
100100
return hosts
101101
}
102102
{{/hostWithAppID}}
103-
{{#uniqueHost}}
103+
{{#hostsWithoutVariables.size}}
104104
func getDefaultHosts() []transport.StatefulHost {
105-
return []transport.StatefulHost{transport.NewStatefulHost("https", "{{{.}}}", call.IsReadWrite)}
105+
return []transport.StatefulHost{
106+
{{#hostsWithoutVariables}}
107+
transport.NewStatefulHost("https", "{{{.}}}", call.IsReadWrite),
108+
{{/hostsWithoutVariables}}
109+
}
106110
}
107-
{{/uniqueHost}}
111+
{{/hostsWithoutVariables.size}}
108112

109113
func getUserAgent() string {
110114
return fmt.Sprintf("Algolia for Go ({{{packageVersion}}}); Go (%s); {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{{/lambda.camelcase}}{{/lambda.titlecase}} ({{{packageVersion}}})", runtime.Version())

‎templates/java/api.mustache

+4-2
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ public class {{classname}} extends ApiClient {
120120
}
121121
{{/hasRegionalHost}}
122122

123-
{{#uniqueHost}}
123+
{{#hostsWithoutVariables.size}}
124124
private static List<Host> getDefaultHosts() {
125125
List<Host> hosts = new ArrayList<>();
126+
{{#hostsWithoutVariables}}
126127
hosts.add(new Host("{{{.}}}", EnumSet.of(CallType.READ, CallType.WRITE)));
128+
{{/hostsWithoutVariables}}
127129
return hosts;
128130
}
129-
{{/uniqueHost}}
131+
{{/hostsWithoutVariables.size}}
130132

131133
{{#operation}}
132134
/**

‎templates/javascript/clients/client/api/hosts.mustache

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ export type Region = (typeof REGIONS)[number];
55

66
{{^hasRegionalHost}}
77
function getDefaultHosts({{#hostWithAppID}}appId: string{{/hostWithAppID}}): Host[] {
8+
{{#hostsWithoutVariables.size}}
9+
return [
10+
{{#hostsWithoutVariables}}
11+
{url: "{{{.}}}", accept: 'readWrite', protocol: 'https' },
12+
{{/hostsWithoutVariables}}
13+
];
14+
{{/hostsWithoutVariables.size}}
815
{{#uniqueHost}}
9-
return [{url: "{{{.}}}", accept: 'readWrite', protocol: 'https' }];
1016
{{/uniqueHost}}
1117
{{#hostWithAppID}}
1218
return (

‎templates/kotlin/api.mustache

+8-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ public class {{classname}}(
4646
val url = {{#fallbackToAliasHost}}if (region == null) "{{{hostWithFallback}}}" else {{/fallbackToAliasHost}} "{{{hostForKotlin}}}"
4747
listOf(Host(url))
4848
{{/hasRegionalHost}}
49-
{{#uniqueHost}}
50-
listOf(Host("{{.}}"))
51-
{{/uniqueHost}}
49+
{{#hostsWithoutVariables.size}}
50+
listOf(
51+
{{#hostsWithoutVariables}}
52+
Host("{{.}}"),
53+
{{/hostsWithoutVariables}}
54+
)
55+
{{/hostsWithoutVariables.size}}
5256
}
5357

5458
{{#operation}}
@@ -109,4 +113,4 @@ public class {{classname}}(
109113
}
110114
{{/operation}}
111115
}
112-
{{/operations}}
116+
{{/operations}}

‎templates/php/api.mustache

+7-3
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,18 @@ use {{invokerPackage}}\Support\Helpers;
128128
}
129129
{{/hasRegionalHost}}
130130

131-
{{#uniqueHost}}
131+
{{#hostsWithoutVariables.size}}
132132
if ($hosts = $config->getHosts()) {
133133
// If a list of hosts was passed, we ignore the cache
134134
$clusterHosts = ClusterHosts::create($hosts);
135135
} else {
136-
$clusterHosts = ClusterHosts::create('{{.}}');
136+
$clusterHosts = ClusterHosts::create([
137+
{{#hostsWithoutVariables}}
138+
'{{.}}',
139+
{{/hostsWithoutVariables}}
140+
]);
137141
}
138-
{{/uniqueHost}}
142+
{{/hostsWithoutVariables.size}}
139143

140144
return $clusterHosts;
141145
}

‎templates/python/config.mustache

+5-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig):
4444

4545
{{^hasRegionalHost}}
4646
self.hosts = HostsCollection(
47-
{{#uniqueHost}}
47+
{{#hostsWithoutVariables.size}}
4848
[
49+
{{#hostsWithoutVariables}}
4950
Host("{{{.}}}"),
51+
{{/hostsWithoutVariables}}
5052
]
51-
{{/uniqueHost}}
53+
{{/hostsWithoutVariables.size}}
5254
{{#hostWithAppID}}
5355
[
5456
Host(url="{}-dsn.algolia.net".format(self.app_id), priority=10, accept=CallType.READ),
@@ -60,4 +62,4 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig):
6062
reorder_hosts=True,
6163
{{/hostWithAppID}}
6264
)
63-
{{/hasRegionalHost}}
65+
{{/hasRegionalHost}}

‎templates/ruby/api.mustache

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ module {{moduleName}}
4545
Transport::StatefulHost.new("#{app_id}-#{i}.algolianet.com", accept: CallType::READ | CallType::WRITE)
4646
end.shuffle
4747
{{/hostWithAppID}}
48-
{{#uniqueHost}}
48+
{{#hostsWithoutVariables.size}}
49+
{{#hostsWithoutVariables}}
4950
hosts << Transport::StatefulHost.new('{{.}}', accept: CallType::READ | CallType::WRITE)
50-
{{/uniqueHost}}
51+
{{/hostsWithoutVariables}}
52+
{{/hostsWithoutVariables.size}}
5153

5254
config = Algolia::Configuration.new(app_id, api_key, hosts, '{{{baseName}}}', opts)
5355
create_with_config(config)
@@ -227,4 +229,4 @@ module {{moduleName}}
227229
{{/isSearchClient}}
228230
end
229231
{{/operations}}
230-
end
232+
end

‎templates/scala/api.mustache

+8-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@ object {{classname}} {
7575
) ++ commonHosts
7676
}
7777
{{/hostWithAppID}}
78-
{{#uniqueHost}}
78+
{{#hostsWithoutVariables.size}}
7979
private def hosts(): Seq[Host] = {
80-
List(Host("{{.}}", Set(CallType.Read, CallType.Write)))
80+
List(
81+
{{#hostsWithoutVariables}}
82+
Host("{{.}}", Set(CallType.Read, CallType.Write)),
83+
{{/hostsWithoutVariables}}
84+
)
8185
}
82-
{{/uniqueHost}}
86+
{{/hostsWithoutVariables.size}}
8387
}
8488

8589
class {{classname}}(
@@ -132,4 +136,4 @@ class {{classname}}(
132136

133137
{{/operation}}
134138
}
135-
{{/operations}}
139+
{{/operations}}

‎templates/swift/client_configuration.mustache

+9-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct {{#lambda.client-to-name}}{{{client}}}{{/lambda.client-to-name}}Cl
5555
UserAgentController.append(UserAgent(title: "{{#lambda.client-to-name}}{{client}}{{/lambda.client-to-name}}", version: Version.current.description))
5656

5757
guard let hosts = hosts else {
58-
{{^uniqueHost}}{{^hasRegionalHost}}
58+
{{^hostsWithoutVariables}}{{^hasRegionalHost}}
5959
func buildHost(_ components: (suffix: String, callType: RetryableHost.CallTypeSupport)) throws
6060
-> RetryableHost
6161
{
@@ -78,15 +78,16 @@ public struct {{#lambda.client-to-name}}{{{client}}}{{/lambda.client-to-name}}Cl
7878
].map(buildHost).shuffled()
7979

8080
self.hosts = hosts + commonHosts
81-
{{/hasRegionalHost}}{{/uniqueHost}}
82-
{{#uniqueHost}}
83-
guard let url = URL(string: "https://{{{.}}}") else {
84-
throw AlgoliaError.runtimeError("Malformed URL")
85-
}
86-
81+
{{/hasRegionalHost}}{{/hostsWithoutVariables}}
82+
{{#hostsWithoutVariables.size}}
8783
self.hosts = [
88-
.init(url: url)
84+
{{#hostsWithoutVariables}}
85+
.init(url: URL(string: "https://{{{.}}}")!),
86+
{{/hostsWithoutVariables}}
8987
]
88+
{{/hostsWithoutVariables.size}}
89+
{{#uniqueHost}}
90+
9091
{{/uniqueHost}}
9192
{{#hasRegionalHost}}
9293
guard {{#fallbackToAliasHost}}region == nil || {{/fallbackToAliasHost}}authorizedRegions.contains(region{{#fallbackToAliasHost}}!{{/fallbackToAliasHost}}) else {

‎tests/CTS/client/usage/api.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"expected": {
2020
"type": "host",
21-
"match": "test-app-id-dsn.algolia.net"
21+
"match": "usage.algolia.com"
2222
}
2323
}
2424
]
@@ -42,7 +42,7 @@
4242
},
4343
"expected": {
4444
"type": "host",
45-
"match": "test-app-id.algolia.net"
45+
"match": "usage.algolia.com"
4646
}
4747
}
4848
]

0 commit comments

Comments
 (0)
Please sign in to comment.