Skip to content

Commit 2481e3a

Browse files
authored
fix(clients): support the rest of the languages (#2571)
1 parent b27441b commit 2481e3a

File tree

13 files changed

+76
-49
lines changed

13 files changed

+76
-49
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public void processOpts() {
4848
supportingFiles.add(new SupportingFile("client_config.mustache", "lib/Configuration", getClientName(client) + "Config.php"));
4949
supportingFiles.add(new SupportingFile("Algolia.mustache", "lib", "Algolia.php"));
5050

51-
setDefaultGeneratorOptions(client);
51+
additionalProperties.put("isSearchClient", client.equals("search"));
52+
additionalProperties.put("configClassname", getClientName(client) + "Config");
53+
5254
try {
5355
additionalProperties.put("packageVersion", Helpers.getClientConfigField("php", "packageVersion"));
5456
} catch (GeneratorException e) {
@@ -68,15 +70,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
6870
return Helpers.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
6971
}
7072

71-
/** Set default generator options */
72-
public void setDefaultGeneratorOptions(String client) {
73-
if (client.equals("search") || client.equals("recommend")) {
74-
additionalProperties.put("useCache", true);
75-
}
76-
additionalProperties.put("isSearchClient", client.equals("search"));
77-
additionalProperties.put("configClassname", getClientName(client) + "Config");
78-
}
79-
73+
@Override
8074
public String getComposerPackageName() {
8175
return "algolia/algoliasearch-client-php";
8276
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,13 @@ public static void generateServers(List<CodegenServer> servers, Map<String, Obje
131131
regionalHost = url.getHost();
132132
}
133133

134-
if (servers.size() == 1 && hostWithFallback.isEmpty() && !hasRegionalHost) {
135-
URL url = new URL(servers.get(0).url);
136-
bundle.put("uniqueHost", url.getHost());
134+
if (!hasRegionalHost) {
135+
if (servers.size() == 1 && hostWithFallback.isEmpty()) {
136+
URL url = new URL(servers.get(0).url);
137+
bundle.put("uniqueHost", url.getHost());
138+
} else {
139+
bundle.put("hostWithAppID", true);
140+
}
137141
}
138142

139143
bundle.put("hostWithFallback", hostWithFallback);

templates/csharp/Configuration.mustache

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ namespace Algolia.Search.Clients
3636
/// <param name="apiKey">Your API Key</param>
3737
public {{packageName}}Config(string appId, string apiKey) : base(appId, apiKey, "{{packageName}}")
3838
{
39-
DefaultHosts = GetDefaultHosts({{^uniqueHost}}appId{{/uniqueHost}});
39+
DefaultHosts = GetDefaultHosts({{#hostWithAppID}}appId{{/hostWithAppID}});
4040
Compression = CompressionType.NONE;
4141
}
4242
{{/hasRegionalHost}}
43-
{{^hasRegionalHost}}{{^uniqueHost}}
43+
{{#hostWithAppID}}
4444
private static List<StatefulHost> GetDefaultHosts(string appId)
4545
{
4646
List<StatefulHost> hosts = new List<StatefulHost>
@@ -86,8 +86,7 @@ namespace Algolia.Search.Clients
8686
hosts.AddRange(commonHosts);
8787
return hosts;
8888
}
89-
{{/uniqueHost}}
90-
{{/hasRegionalHost}}
89+
{{/hostWithAppID}}
9190
{{#hasRegionalHost}}
9291
private static List<StatefulHost> GetDefaultHosts(string region)
9392
{
@@ -124,7 +123,7 @@ namespace Algolia.Search.Clients
124123
{
125124
new StatefulHost
126125
{
127-
Url = "{{{uniqueHost}}}",
126+
Url = "{{{.}}}",
128127
Up = true,
129128
LastUse = DateTime.UtcNow,
130129
Accept = CallType.Read | CallType.Write
@@ -133,4 +132,4 @@ namespace Algolia.Search.Clients
133132
}
134133
{{/uniqueHost}}
135134
}
136-
}
135+
}

templates/dart/api.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class {{classname}} implements ApiClient {
4040
appId: appId,
4141
apiKey: apiKey,
4242
options: options,
43-
{{^hasRegionalHost}}
43+
{{#hostWithAppID}}
4444
defaultHosts: () => [
4545
Host(url: '$appId-dsn.algolia.net', callType: CallType.read),
4646
Host(url: '$appId.algolia.net', callType: CallType.write),
@@ -52,7 +52,7 @@ final class {{classname}} implements ApiClient {
5252
Host(url: '$appId-3.algolianet.com'),
5353
]..shuffle()
5454
),
55-
{{/hasRegionalHost}}
55+
{{/hostWithAppID}}
5656
{{#hasRegionalHost}}
5757
defaultHosts: () {
5858
final allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];
@@ -66,6 +66,9 @@ final class {{classname}} implements ApiClient {
6666
return [Host(url: url)];
6767
}
6868
{{/hasRegionalHost}}
69+
{{#uniqueHost}}
70+
defaultHosts: () => [Host(url: '{{{.}}}')],
71+
{{/uniqueHost}}
6972
) {
7073
assert(appId.isNotEmpty, '`appId` is missing.');
7174
assert(apiKey.isNotEmpty, '`apiKey` is missing.');

templates/go/client.mustache

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewClientWithConfig(cfg Configuration) (*APIClient, error) {
6666
if {{^fallbackToAliasHost}}cfg.Region == "" || {{/fallbackToAliasHost}}(cfg.Region != "" && !slices.Contains(allowedRegions[:], string(cfg.Region))) {
6767
return nil, fmt.Errorf("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: %s", strings.Join(allowedRegions[:], ", "))
6868
}{{/hasRegionalHost}}
69-
hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{^hasRegionalHost}}cfg.AppID{{/hasRegionalHost}})
69+
hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{#hostWithAppID}}cfg.AppID{{/hostWithAppID}})
7070
} else {
7171
for _, h := range cfg.Hosts {
7272
hosts = append(hosts, transport.NewStatefulHost(h, call.IsReadWrite))
@@ -103,7 +103,7 @@ func getDefaultHosts(r Region) []*transport.StatefulHost {
103103
return []*transport.StatefulHost{transport.NewStatefulHost(strings.ReplaceAll("{{{regionalHost}}}", "{region}", string(r)), call.IsReadWrite)}
104104
}
105105
{{/hasRegionalHost}}
106-
{{^hasRegionalHost}}
106+
{{#hostWithAppID}}
107107
func getDefaultHosts(appID string) []*transport.StatefulHost {
108108
hosts := []*transport.StatefulHost{
109109
transport.NewStatefulHost(appID + "-dsn.algolia.net", call.IsRead),
@@ -118,7 +118,13 @@ func getDefaultHosts(appID string) []*transport.StatefulHost {
118118
)...)
119119
return hosts
120120
}
121-
{{/hasRegionalHost}}
121+
{{/hostWithAppID}}
122+
{{#uniqueHost}}
123+
func getDefaultHosts() []*transport.StatefulHost {
124+
return []*transport.StatefulHost{transport.NewStatefulHost("{{{.}}}", call.IsReadWrite)}
125+
}
126+
{{/uniqueHost}}
127+
122128
func getUserAgent() string {
123129
return fmt.Sprintf("Algolia for Go ({{{packageVersion}}}); Go (%s); {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{{/lambda.camelcase}}{{/lambda.titlecase}} ({{{packageVersion}}})", runtime.Version())
124130
}

templates/java/api.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public class {{classname}} extends ApiClient {
5959
}
6060

6161
public {{classname}}(String appId, String apiKey, ClientOptions options) {
62-
super(appId, apiKey, "{{{baseName}}}", options, getDefaultHosts({{^uniqueHost}}appId{{/uniqueHost}}));
62+
super(appId, apiKey, "{{{baseName}}}", options, getDefaultHosts({{#hostWithAppID}}appId{{/hostWithAppID}}));
6363
}
6464
{{/hasRegionalHost}}
6565

66-
{{^uniqueHost}}{{^hasRegionalHost}}
66+
{{#hostWithAppID}}
6767
private static List<Host> getDefaultHosts(String appId) {
6868
List<Host> hosts = new ArrayList<>();
6969
hosts.add(new Host(appId + "-dsn.algolia.net", EnumSet.of(CallType.READ)));
@@ -78,7 +78,7 @@ public class {{classname}} extends ApiClient {
7878
7979
return Stream.concat(hosts.stream(), commonHosts.stream()).collect(Collectors.toList());
8080
}
81-
{{/hasRegionalHost}}
81+
{{/hostWithAppID}}
8282

8383
{{#hasRegionalHost}}
8484
private static List<Host> getDefaultHosts(String region) throws AlgoliaRuntimeException {
@@ -103,7 +103,7 @@ public class {{classname}} extends ApiClient {
103103
hosts.add(new Host(url, EnumSet.of(CallType.READ, CallType.WRITE)));
104104
return hosts;
105105
}
106-
{{/hasRegionalHost}}{{/uniqueHost}}
106+
{{/hasRegionalHost}}
107107

108108
{{#uniqueHost}}
109109
private static List<Host> getDefaultHosts() {

templates/javascript/clients/api-single.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
2222
}: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
2323
const auth = createAuth(appIdOption, apiKeyOption, authMode);
2424
const transporter = createTransporter({
25-
hosts: getDefaultHosts({{^hasRegionalHost}}{{^uniqueHost}}appIdOption{{/uniqueHost}}{{/hasRegionalHost}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}),
25+
hosts: getDefaultHosts({{#hostWithAppID}}appIdOption{{/hostWithAppID}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}),
2626
...options,
2727
algoliaAgent: getAlgoliaAgent({
2828
algoliaAgents,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export type Region = (typeof REGIONS)[number];
44
{{/hasRegionalHost}}
55

66
{{^hasRegionalHost}}
7-
function getDefaultHosts({{^uniqueHost}}appId: string{{/uniqueHost}}): Host[] {
7+
function getDefaultHosts({{#hostWithAppID}}appId: string{{/hostWithAppID}}): Host[] {
88
{{#uniqueHost}}
9-
return [{url: "{{{uniqueHost}}}", accept: 'readWrite', protocol: 'https' }];
9+
return [{url: "{{{.}}}", accept: 'readWrite', protocol: 'https' }];
1010
{{/uniqueHost}}
11-
{{^uniqueHost}}
11+
{{#hostWithAppID}}
1212
return (
1313
[
1414
{
@@ -41,7 +41,7 @@ function getDefaultHosts({{^uniqueHost}}appId: string{{/uniqueHost}}): Host[] {
4141
},
4242
])
4343
);
44-
{{/uniqueHost}}
44+
{{/hostWithAppID}}
4545
}
4646
{{/hasRegionalHost}}
4747

templates/kotlin/api.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class {{classname}}(
2525
}
2626

2727
override val requester: Requester = requesterOf(clientName = "{{{baseName}}}", appId = appId, apiKey = apiKey, options = options) {
28-
{{^hasRegionalHost}}
28+
{{#hostWithAppID}}
2929
listOf(
3030
Host("$appId-dsn.algolia.net", CallType.Read),
3131
Host("$appId.algolia.net", CallType.Write),
@@ -34,7 +34,7 @@ public class {{classname}}(
3434
Host("$appId-2.algolianet.com"),
3535
Host("$appId-3.algolianet.com")
3636
).apply { shuffle() }
37-
{{/hasRegionalHost}}
37+
{{/hostWithAppID}}
3838
{{#hasRegionalHost}}
3939
val allowedRegions = listOf({{#allowedRegions}}"{{.}}"{{^-last}},{{/-last}}{{/allowedRegions}})
4040
{{^fallbackToAliasHost}}
@@ -46,6 +46,9 @@ 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}}
4952
}
5053

5154
{{#operation}}

templates/php/api.mustache

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ use {{invokerPackage}}\Support\Helpers;
113113
*/
114114
public static function getClusterHosts({{configClassname}} $config)
115115
{
116-
{{#useCache}}
116+
{{#hostWithAppID}}
117117
$cacheKey = sprintf('%s-clusterHosts-%s', __CLASS__, $config->getAppId());
118118

119119
if ($hosts = $config->getHosts()) {
@@ -125,9 +125,9 @@ use {{invokerPackage}}\Support\Helpers;
125125
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId())
126126
->setCacheKey($cacheKey);
127127
}
128-
{{/useCache}}
128+
{{/hostWithAppID}}
129129
130-
{{^useCache}}
130+
{{#hasRegionalHost}}
131131
if ($hosts = $config->getHosts()) {
132132
// If a list of hosts was passed, we ignore the cache
133133
$clusterHosts = ClusterHosts::create($hosts);
@@ -137,7 +137,16 @@ use {{invokerPackage}}\Support\Helpers;
137137
'{{{hostWithFallback}}}';
138138
$clusterHosts = ClusterHosts::create($url);
139139
}
140-
{{/useCache}}
140+
{{/hasRegionalHost}}
141+
142+
{{#uniqueHost}}
143+
if ($hosts = $config->getHosts()) {
144+
// If a list of hosts was passed, we ignore the cache
145+
$clusterHosts = ClusterHosts::create($hosts);
146+
} else {
147+
$clusterHosts = ClusterHosts::create('{{.}}');
148+
}
149+
{{/uniqueHost}}
141150

142151
return $clusterHosts;
143152
}
@@ -430,4 +439,4 @@ use {{invokerPackage}}\Support\Helpers;
430439
);
431440
}
432441
}
433-
{{/operations}}
442+
{{/operations}}

0 commit comments

Comments
 (0)