From bb796af24ee81d8681ae9ecc3e96c45019d8c752 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 18 Jan 2024 17:17:43 +0100 Subject: [PATCH 1/2] fix(clients): support the rest of the languages --- .../algolia/codegen/AlgoliaPhpGenerator.java | 14 ++++---------- .../com/algolia/codegen/utils/Helpers.java | 10 +++++++--- templates/csharp/Configuration.mustache | 11 +++++------ templates/dart/api.mustache | 7 +++++-- templates/go/client.mustache | 12 +++++++++--- templates/java/api.mustache | 8 ++++---- .../javascript/clients/api-single.mustache | 2 +- .../clients/client/api/hosts.mustache | 8 ++++---- templates/kotlin/api.mustache | 7 +++++-- templates/kotlin/snippets/method.mustache | 4 ++-- templates/php/api.mustache | 19 ++++++++++++++----- templates/python/config.mustache | 8 ++++---- templates/ruby/api.mustache | 8 ++++++-- templates/scala/api.mustache | 11 ++++++++--- 14 files changed, 78 insertions(+), 51 deletions(-) diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java index d3a4559e95..9b0bcc4bc7 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java @@ -48,7 +48,9 @@ public void processOpts() { supportingFiles.add(new SupportingFile("client_config.mustache", "lib/Configuration", getClientName(client) + "Config.php")); supportingFiles.add(new SupportingFile("Algolia.mustache", "lib", "Algolia.php")); - setDefaultGeneratorOptions(client); + additionalProperties.put("isSearchClient", client.equals("search")); + additionalProperties.put("configClassname", getClientName(client) + "Config"); + try { additionalProperties.put("packageVersion", Helpers.getClientConfigField("php", "packageVersion")); } catch (GeneratorException e) { @@ -68,15 +70,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation return Helpers.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers)); } - /** Set default generator options */ - public void setDefaultGeneratorOptions(String client) { - if (client.equals("search") || client.equals("recommend")) { - additionalProperties.put("useCache", true); - } - additionalProperties.put("isSearchClient", client.equals("search")); - additionalProperties.put("configClassname", getClientName(client) + "Config"); - } - + @Override public String getComposerPackageName() { return "algolia/algoliasearch-client-php"; } diff --git a/generators/src/main/java/com/algolia/codegen/utils/Helpers.java b/generators/src/main/java/com/algolia/codegen/utils/Helpers.java index 5b5c1c7596..cd29d41fe9 100644 --- a/generators/src/main/java/com/algolia/codegen/utils/Helpers.java +++ b/generators/src/main/java/com/algolia/codegen/utils/Helpers.java @@ -131,9 +131,13 @@ public static void generateServers(List servers, MapYour API Key public {{packageName}}Config(string appId, string apiKey) : base(appId, apiKey, "{{packageName}}") { - DefaultHosts = GetDefaultHosts({{^uniqueHost}}appId{{/uniqueHost}}); + DefaultHosts = GetDefaultHosts({{#hostWithAppID}}appId{{/hostWithAppID}}); Compression = CompressionType.NONE; } {{/hasRegionalHost}} - {{^hasRegionalHost}}{{^uniqueHost}} + {{#hostWithAppID}} private static List GetDefaultHosts(string appId) { List hosts = new List @@ -86,8 +86,7 @@ namespace Algolia.Search.Clients hosts.AddRange(commonHosts); return hosts; } - {{/uniqueHost}} - {{/hasRegionalHost}} + {{/hostWithAppID}} {{#hasRegionalHost}} private static List GetDefaultHosts(string region) { @@ -124,7 +123,7 @@ namespace Algolia.Search.Clients { new StatefulHost { - Url = "{{{uniqueHost}}}", + Url = "{{{.}}}", Up = true, LastUse = DateTime.UtcNow, Accept = CallType.Read | CallType.Write @@ -133,4 +132,4 @@ namespace Algolia.Search.Clients } {{/uniqueHost}} } -} \ No newline at end of file +} diff --git a/templates/dart/api.mustache b/templates/dart/api.mustache index d35af608a9..585e6d907f 100644 --- a/templates/dart/api.mustache +++ b/templates/dart/api.mustache @@ -40,7 +40,7 @@ final class {{classname}} implements ApiClient { appId: appId, apiKey: apiKey, options: options, - {{^hasRegionalHost}} + {{#hostWithAppID}} defaultHosts: () => [ Host(url: '$appId-dsn.algolia.net', callType: CallType.read), Host(url: '$appId.algolia.net', callType: CallType.write), @@ -52,7 +52,7 @@ final class {{classname}} implements ApiClient { Host(url: '$appId-3.algolianet.com'), ]..shuffle() ), - {{/hasRegionalHost}} + {{/hostWithAppID}} {{#hasRegionalHost}} defaultHosts: () { final allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}]; @@ -66,6 +66,9 @@ final class {{classname}} implements ApiClient { return [Host(url: url)]; } {{/hasRegionalHost}} + {{#uniqueHost}} + defaultHosts: () => [Host(url: '{{{.}}}')], + {{/uniqueHost}} ) { assert(appId.isNotEmpty, '`appId` is missing.'); assert(apiKey.isNotEmpty, '`apiKey` is missing.'); diff --git a/templates/go/client.mustache b/templates/go/client.mustache index 64b6424440..b00fcc0523 100644 --- a/templates/go/client.mustache +++ b/templates/go/client.mustache @@ -66,7 +66,7 @@ func NewClientWithConfig(cfg Configuration) (*APIClient, error) { if {{^fallbackToAliasHost}}cfg.Region == "" || {{/fallbackToAliasHost}}(cfg.Region != "" && !slices.Contains(allowedRegions[:], string(cfg.Region))) { return nil, fmt.Errorf("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: %s", strings.Join(allowedRegions[:], ", ")) }{{/hasRegionalHost}} - hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{^hasRegionalHost}}cfg.AppID{{/hasRegionalHost}}) + hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{#hostWithAppID}}cfg.AppID{{/hostWithAppID}}) } else { for _, h := range cfg.Hosts { hosts = append(hosts, transport.NewStatefulHost(h, call.IsReadWrite)) @@ -103,7 +103,7 @@ func getDefaultHosts(r Region) []*transport.StatefulHost { return []*transport.StatefulHost{transport.NewStatefulHost(strings.ReplaceAll("{{{regionalHost}}}", "{region}", string(r)), call.IsReadWrite)} } {{/hasRegionalHost}} -{{^hasRegionalHost}} +{{#hostWithAppID}} func getDefaultHosts(appID string) []*transport.StatefulHost { hosts := []*transport.StatefulHost{ transport.NewStatefulHost(appID + "-dsn.algolia.net", call.IsRead), @@ -118,7 +118,13 @@ func getDefaultHosts(appID string) []*transport.StatefulHost { )...) return hosts } -{{/hasRegionalHost}} +{{/hostWithAppID}} +{{#uniqueHost}} +func getDefaultHosts() []*transport.StatefulHost { + return []*transport.StatefulHost{transport.NewStatefulHost("{{{.}}}", call.IsReadWrite)} +} +{{/uniqueHost}} + func getUserAgent() string { return fmt.Sprintf("Algolia for Go ({{{packageVersion}}}); Go (%s); {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{{/lambda.camelcase}}{{/lambda.titlecase}} ({{{packageVersion}}})", runtime.Version()) } diff --git a/templates/java/api.mustache b/templates/java/api.mustache index 369aa2a73e..c85a0b884a 100644 --- a/templates/java/api.mustache +++ b/templates/java/api.mustache @@ -59,11 +59,11 @@ public class {{classname}} extends ApiClient { } public {{classname}}(String appId, String apiKey, ClientOptions options) { - super(appId, apiKey, "{{{baseName}}}", options, getDefaultHosts({{^uniqueHost}}appId{{/uniqueHost}})); + super(appId, apiKey, "{{{baseName}}}", options, getDefaultHosts({{#hostWithAppID}}appId{{/hostWithAppID}})); } {{/hasRegionalHost}} - {{^uniqueHost}}{{^hasRegionalHost}} + {{#hostWithAppID}} private static List getDefaultHosts(String appId) { List hosts = new ArrayList<>(); hosts.add(new Host(appId + "-dsn.algolia.net", EnumSet.of(CallType.READ))); @@ -78,7 +78,7 @@ public class {{classname}} extends ApiClient { return Stream.concat(hosts.stream(), commonHosts.stream()).collect(Collectors.toList()); } - {{/hasRegionalHost}} + {{/hostWithAppID}} {{#hasRegionalHost}} private static List getDefaultHosts(String region) throws AlgoliaRuntimeException { @@ -103,7 +103,7 @@ public class {{classname}} extends ApiClient { hosts.add(new Host(url, EnumSet.of(CallType.READ, CallType.WRITE))); return hosts; } - {{/hasRegionalHost}}{{/uniqueHost}} + {{/hasRegionalHost}} {{#uniqueHost}} private static List getDefaultHosts() { diff --git a/templates/javascript/clients/api-single.mustache b/templates/javascript/clients/api-single.mustache index 679ee64a6e..c96f928766 100644 --- a/templates/javascript/clients/api-single.mustache +++ b/templates/javascript/clients/api-single.mustache @@ -22,7 +22,7 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({ }: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) { const auth = createAuth(appIdOption, apiKeyOption, authMode); const transporter = createTransporter({ - hosts: getDefaultHosts({{^hasRegionalHost}}{{^uniqueHost}}appIdOption{{/uniqueHost}}{{/hasRegionalHost}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}), + hosts: getDefaultHosts({{#hostWithAppID}}appIdOption{{/hostWithAppID}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}), ...options, algoliaAgent: getAlgoliaAgent({ algoliaAgents, diff --git a/templates/javascript/clients/client/api/hosts.mustache b/templates/javascript/clients/client/api/hosts.mustache index 38a5f383f5..674da21ce1 100644 --- a/templates/javascript/clients/client/api/hosts.mustache +++ b/templates/javascript/clients/client/api/hosts.mustache @@ -4,11 +4,11 @@ export type Region = (typeof REGIONS)[number]; {{/hasRegionalHost}} {{^hasRegionalHost}} -function getDefaultHosts({{^uniqueHost}}appId: string{{/uniqueHost}}): Host[] { +function getDefaultHosts({{#hostWithAppID}}appId: string{{/hostWithAppID}}): Host[] { {{#uniqueHost}} - return [{url: "{{{uniqueHost}}}", accept: 'readWrite', protocol: 'https' }]; + return [{url: "{{{.}}}", accept: 'readWrite', protocol: 'https' }]; {{/uniqueHost}} - {{^uniqueHost}} + {{#hostWithAppID}} return ( [ { @@ -41,7 +41,7 @@ function getDefaultHosts({{^uniqueHost}}appId: string{{/uniqueHost}}): Host[] { }, ]) ); - {{/uniqueHost}} + {{/hostWithAppID}} } {{/hasRegionalHost}} diff --git a/templates/kotlin/api.mustache b/templates/kotlin/api.mustache index df3a363145..36df1b1b9a 100644 --- a/templates/kotlin/api.mustache +++ b/templates/kotlin/api.mustache @@ -25,7 +25,7 @@ public class {{classname}}( } override val requester: Requester = requesterOf(clientName = "{{{baseName}}}", appId = appId, apiKey = apiKey, options = options) { - {{^hasRegionalHost}} + {{#hostWithAppID}} listOf( Host("$appId-dsn.algolia.net", CallType.Read), Host("$appId.algolia.net", CallType.Write), @@ -34,7 +34,7 @@ public class {{classname}}( Host("$appId-2.algolianet.com"), Host("$appId-3.algolianet.com") ).apply { shuffle() } - {{/hasRegionalHost}} + {{/hostWithAppID}} {{#hasRegionalHost}} val allowedRegions = listOf({{#allowedRegions}}"{{.}}"{{^-last}},{{/-last}}{{/allowedRegions}}) {{^fallbackToAliasHost}} @@ -46,6 +46,9 @@ public class {{classname}}( val url = {{#fallbackToAliasHost}}if (region == null) "{{{hostWithFallback}}}" else {{/fallbackToAliasHost}} "{{{hostForKotlin}}}" listOf(Host(url)) {{/hasRegionalHost}} + {{#uniqueHost}} + listOf(Host("{{.}}")) + {{/uniqueHost}} } {{#operation}} diff --git a/templates/kotlin/snippets/method.mustache b/templates/kotlin/snippets/method.mustache index 5c2011a2f1..5a73a3daf3 100644 --- a/templates/kotlin/snippets/method.mustache +++ b/templates/kotlin/snippets/method.mustache @@ -11,7 +11,7 @@ class Snippet{{client}} { {{#snippet}} suspend fun snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}() { // Initialize the client - val client = {{client}}(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY"{{#hasRegionalHost}}, region = "YOUR_APP_ID_REGION"{{/hasRegionalHost}}) + val client = {{client}}(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY"{{#hasRegionalHost}}, region = "YOUR_REGION"{{/hasRegionalHost}}) // Call the API var response = client.{{method}}( @@ -46,4 +46,4 @@ class Snippet{{client}} { {{/snippet}} {{/blocksRequests}} -} \ No newline at end of file +} diff --git a/templates/php/api.mustache b/templates/php/api.mustache index 9d452fd532..ecfba7b432 100644 --- a/templates/php/api.mustache +++ b/templates/php/api.mustache @@ -113,7 +113,7 @@ use {{invokerPackage}}\Support\Helpers; */ public static function getClusterHosts({{configClassname}} $config) { - {{#useCache}} + {{#hostWithAppID}} $cacheKey = sprintf('%s-clusterHosts-%s', __CLASS__, $config->getAppId()); if ($hosts = $config->getHosts()) { @@ -125,9 +125,9 @@ use {{invokerPackage}}\Support\Helpers; $clusterHosts = ClusterHosts::createFromAppId($config->getAppId()) ->setCacheKey($cacheKey); } - {{/useCache}} + {{/hostWithAppID}} - {{^useCache}} + {{#hasRegionalHost}} if ($hosts = $config->getHosts()) { // If a list of hosts was passed, we ignore the cache $clusterHosts = ClusterHosts::create($hosts); @@ -137,7 +137,16 @@ use {{invokerPackage}}\Support\Helpers; '{{{hostWithFallback}}}'; $clusterHosts = ClusterHosts::create($url); } - {{/useCache}} + {{/hasRegionalHost}} + + {{#uniqueHost}} + if ($hosts = $config->getHosts()) { + // If a list of hosts was passed, we ignore the cache + $clusterHosts = ClusterHosts::create($hosts); + } else { + $clusterHosts = ClusterHosts::create('{{.}}'); + } + {{/uniqueHost}} return $clusterHosts; } @@ -430,4 +439,4 @@ use {{invokerPackage}}\Support\Helpers; ); } } -{{/operations}} \ No newline at end of file +{{/operations}} diff --git a/templates/python/config.mustache b/templates/python/config.mustache index c1092bd48e..34927baed8 100644 --- a/templates/python/config.mustache +++ b/templates/python/config.mustache @@ -60,10 +60,10 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig): self.hosts = HostsCollection( {{#uniqueHost}} [ - Host("{{{uniqueHost}}}"), + Host("{{{.}}}"), ] {{/uniqueHost}} - {{^uniqueHost}} + {{#hostWithAppID}} [ Host("{}-dsn.algolia.net".format(self.app_id), 10, CallType.READ), Host("{}.algolia.net".format(self.app_id), 10, CallType.WRITE), @@ -71,6 +71,6 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig): Host("{}-2.algolianet.com".format(self.app_id)), Host("{}-3.algolianet.com".format(self.app_id)), ] - {{/uniqueHost}} + {{/hostWithAppID}} ) - {{/hasRegionalHost}} \ No newline at end of file + {{/hasRegionalHost}} diff --git a/templates/ruby/api.mustache b/templates/ruby/api.mustache index c74b269588..6375dc0177 100644 --- a/templates/ruby/api.mustache +++ b/templates/ruby/api.mustache @@ -31,14 +31,18 @@ module {{moduleName}} {{/fallbackToAliasHost}} hosts << Transport::StatefulHost.new({{#fallbackToAliasHost}}region.nil? ? '{{{hostWithFallback}}}' : {{/fallbackToAliasHost}} '{{{regionalHost}}}'.sub!('{region}', region), accept: CallType::READ | CallType::WRITE) - {{/hasRegionalHost}}{{^hasRegionalHost}} + {{/hasRegionalHost}} + {{#hostWithAppID}} hosts << Transport::StatefulHost.new("#{app_id}-dsn.algolia.net", accept: CallType::READ) hosts << Transport::StatefulHost.new("#{app_id}.algolia.net", accept: CallType::WRITE) hosts += 1.upto(3).map do |i| Transport::StatefulHost.new("#{app_id}-#{i}.algolianet.com", accept: CallType::READ | CallType::WRITE) end.shuffle - {{/hasRegionalHost}} + {{/hostWithAppID}} + {{#uniqueHost}} + hosts << Transport::StatefulHost.new('{{.}}', accept: CallType::READ | CallType::WRITE) + {{/uniqueHost}} config = Algolia::Configuration.new(app_id, api_key, hosts, '{{{baseName}}}', opts) create_with_config(config) diff --git a/templates/scala/api.mustache b/templates/scala/api.mustache index ccb8b4511a..0606406c78 100644 --- a/templates/scala/api.mustache +++ b/templates/scala/api.mustache @@ -60,7 +60,7 @@ object {{classname}} { Seq(Host(url = url, callTypes = Set(CallType.Read, CallType.Write))) } {{/hasRegionalHost}} - {{^hasRegionalHost}} + {{#hostWithAppID}} private def hosts(appId: String): Seq[Host] = { val commonHosts = Random.shuffle( List( @@ -74,7 +74,12 @@ object {{classname}} { Host(appId + ".algolia.net", Set(CallType.Write)) ) ++ commonHosts } - {{/hasRegionalHost}} + {{/hostWithAppID}} + {{#uniqueHost}} + private def hosts(): Seq[Host] = { + List(Host("{{.}}", Set(CallType.Read, CallType.Write))) + } + {{/uniqueHost}} } class {{classname}}( @@ -86,7 +91,7 @@ class {{classname}}( appId = appId, apiKey = apiKey, clientName = "{{{baseName}}}", - defaultHosts = {{^hasRegionalHost}}hosts(appId){{/hasRegionalHost}}{{#hasRegionalHost}}hosts(region){{/hasRegionalHost}}, + defaultHosts = hosts({{#hostWithAppID}}appId{{/hostWithAppID}}{{#hasRegionalHost}}region{{/hasRegionalHost}}), formats = JsonSupport.format, options = clientOptions ) { From 9c14824cc48cf436464c5335a7cfe3d3a42757f7 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 18 Jan 2024 17:34:44 +0100 Subject: [PATCH 2/2] review --- templates/kotlin/snippets/method.mustache | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/kotlin/snippets/method.mustache b/templates/kotlin/snippets/method.mustache index 5a73a3daf3..5c2011a2f1 100644 --- a/templates/kotlin/snippets/method.mustache +++ b/templates/kotlin/snippets/method.mustache @@ -11,7 +11,7 @@ class Snippet{{client}} { {{#snippet}} suspend fun snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}() { // Initialize the client - val client = {{client}}(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY"{{#hasRegionalHost}}, region = "YOUR_REGION"{{/hasRegionalHost}}) + val client = {{client}}(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY"{{#hasRegionalHost}}, region = "YOUR_APP_ID_REGION"{{/hasRegionalHost}}) // Call the API var response = client.{{method}}( @@ -46,4 +46,4 @@ class Snippet{{client}} { {{/snippet}} {{/blocksRequests}} -} +} \ No newline at end of file