From 68ad7314804c40a87921964d223c791267e9801a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 6 May 2022 15:20:41 +0200 Subject: [PATCH] fix(generator): add `hostWithFallback` (#479) --- .../main/java/com/algolia/codegen/Utils.java | 18 +++++----------- scripts/generate.ts | 2 +- specs/predict/spec.yml | 7 ++++++- .../java/libraries/okhttp-gson/api.mustache | 21 +++++++------------ templates/javascript/api-single.mustache | 16 +++++--------- templates/php/api.mustache | 3 ++- tests/CTS/client/abtesting/parameters.json | 3 +-- tests/CTS/client/analytics/parameters.json | 3 +-- tests/CTS/client/insights/parameters.json | 3 +-- 9 files changed, 30 insertions(+), 46 deletions(-) diff --git a/generators/src/main/java/com/algolia/codegen/Utils.java b/generators/src/main/java/com/algolia/codegen/Utils.java index e53afcf85e..3e9c1477b8 100644 --- a/generators/src/main/java/com/algolia/codegen/Utils.java +++ b/generators/src/main/java/com/algolia/codegen/Utils.java @@ -85,7 +85,7 @@ public static void generateServer( boolean hasRegionalHost = false; boolean fallbackToAliasHost = false; String host = ""; - String topLevelDomain = ""; + String hostWithFallback = ""; Set allowedRegions = new HashSet<>(); for (Map server : servers) { if (!server.containsKey("url")) { @@ -102,7 +102,9 @@ public static void generateServer( } String otherUrl = (String) otherServer.getOrDefault("url", ""); if (otherUrl.replace(".{region}", "").equals(server.get("url"))) { + URL fallbackURL = new URL(otherUrl.replace(".{region}", "")); fallbackToAliasHost = true; + hostWithFallback = fallbackURL.getHost(); break; } } @@ -132,26 +134,16 @@ public static void generateServer( // This is used for hosts like `insights` that uses `.io` URL url = new URL((String) server.get("url")); - String[] hostParts = url.getHost().split("\\."); - host = hostParts[0]; - topLevelDomain = hostParts[hostParts.length - 1]; + host = url.getHost(); } + additionalProperties.put("hostWithFallback", hostWithFallback); additionalProperties.put("hasRegionalHost", hasRegionalHost); additionalProperties.put("fallbackToAliasHost", fallbackToAliasHost); additionalProperties.put("host", host); - additionalProperties.put("topLevelDomain", topLevelDomain); additionalProperties.put( "allowedRegions", allowedRegions.toArray(new String[0]) ); - - if (clientKebab.equals("predict")) { - additionalProperties.put("isExperimentalHost", true); - additionalProperties.put( - "host", - new URL((String) servers.get(0).get("url")).getHost() - ); - } } catch (Exception e) { e.printStackTrace(); System.exit(1); diff --git a/scripts/generate.ts b/scripts/generate.ts index 7eb5e024e1..7fa32de477 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -38,7 +38,7 @@ export async function generate( await generateOpenapitools(generators); - const availableWorkspaces = await run('yarn workspaces list', { verbose }); + const availableWorkspaces = await run('yarn workspaces list'); const langs = [...new Set(generators.map((gen) => gen.language))]; const useCustomGenerator = langs .map((lang) => getCustomGenerator(lang)) diff --git a/specs/predict/spec.yml b/specs/predict/spec.yml index 4788088497..7828757fb0 100644 --- a/specs/predict/spec.yml +++ b/specs/predict/spec.yml @@ -10,7 +10,12 @@ components: apiKey: $ref: '../common/securitySchemes.yml#/apiKey' servers: - - url: https://predict-api-432xa6wemq-ew.a.run.app + - url: https://predict-api-432xa6wemq-{region}.a.run.app + variables: + region: + enum: + - ew + default: ew security: - appId: [] apiKey: [] diff --git a/templates/java/libraries/okhttp-gson/api.mustache b/templates/java/libraries/okhttp-gson/api.mustache index de7a0b9a56..8c27a5464d 100644 --- a/templates/java/libraries/okhttp-gson/api.mustache +++ b/templates/java/libraries/okhttp-gson/api.mustache @@ -50,11 +50,11 @@ public class {{classname}} extends ApiClient { {{^hasRegionalHost}} public {{classname}}(String appId, String apiKey) { - this(appId, apiKey, new HttpRequester(getDefaultHosts({{^isExperimentalHost}}appId{{/isExperimentalHost}})), null); + this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), null); } public {{classname}}(String appId, String apiKey, UserAgent.Segment[] userAgentSegments) { - this(appId, apiKey, new HttpRequester(getDefaultHosts({{^isExperimentalHost}}appId{{/isExperimentalHost}})), userAgentSegments); + this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), userAgentSegments); } {{/hasRegionalHost}} @@ -66,7 +66,7 @@ public class {{classname}} extends ApiClient { super(appId, apiKey, requester, "{{{baseName}}}", userAgentSegments); } - {{^hasRegionalHost}}{{^isExperimentalHost}} + {{^hasRegionalHost}} private static List getDefaultHosts(String appId) { List hosts = new ArrayList(); hosts.add(new StatefulHost(appId + "-dsn.algolia.net", "https", EnumSet.of(CallType.READ))); @@ -81,20 +81,15 @@ public class {{classname}} extends ApiClient { return Stream.concat(hosts.stream(), commonHosts.stream()).collect(Collectors.toList()); } - {{/isExperimentalHost}}{{/hasRegionalHost}} - - {{#isExperimentalHost}} - private static List getDefaultHosts() { - List hosts = new ArrayList(); - hosts.add(new StatefulHost("{{{host}}}", "https", EnumSet.of(CallType.READ, CallType.WRITE))); - return hosts; - } - {{/isExperimentalHost}} + {{/hasRegionalHost}} {{#hasRegionalHost}} private static List getDefaultHosts(String region) { List hosts = new ArrayList(); - 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))); + + String url = {{#fallbackToAliasHost}}region == null ? "{{{hostWithFallback}}}" : {{/fallbackToAliasHost}} "{{{host}}}".replace("{region}", region); + + hosts.add(new StatefulHost(url, "https", EnumSet.of(CallType.READ, CallType.WRITE))); return hosts; } {{/hasRegionalHost}} diff --git a/templates/javascript/api-single.mustache b/templates/javascript/api-single.mustache index a5d2c4f6dc..8460439969 100644 --- a/templates/javascript/api-single.mustache +++ b/templates/javascript/api-single.mustache @@ -31,7 +31,7 @@ export const apiClientVersion = '{{packageVersion}}'; export type Region = {{#allowedRegions}}'{{.}}'{{^-last}}|{{/-last}}{{/allowedRegions}}; {{/hasRegionalHost}} -{{^hasRegionalHost}}{{^isExperimentalHost}} +{{^hasRegionalHost}} function getDefaultHosts(appId: string): Host[] { return ( [ @@ -66,19 +66,13 @@ function getDefaultHosts(appId: string): Host[] { ]) ); } -{{/isExperimentalHost}}{{/hasRegionalHost}} - -{{#isExperimentalHost}} -function getDefaultHosts(): Host[] { - return [{ url: '{{host}}', accept: 'readWrite', protocol: 'https' }]; -} -{{/isExperimentalHost}} +{{/hasRegionalHost}} {{#hasRegionalHost}} function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region): Host[] { - {{#fallbackToAliasHost}}const regionHost = region ? `.${region}.` : '.';{{/fallbackToAliasHost}} + const url = {{#fallbackToAliasHost}}!region ? '{{{hostWithFallback}}}' : {{/fallbackToAliasHost}} '{{{host}}}'.replace('{region}', region); - return [{ url: `{{{host}}}{{#fallbackToAliasHost}}${regionHost}{{/fallbackToAliasHost}}{{^fallbackToAliasHost}}.${region}.{{/fallbackToAliasHost}}algolia.{{#topLevelDomain}}{{.}}{{/topLevelDomain}}{{^topLevelDomain}}com{{/topLevelDomain}}`, accept: 'readWrite', protocol: 'https' }]; + return [{ url, accept: 'readWrite', protocol: 'https' }]; } {{/hasRegionalHost}} @@ -86,7 +80,7 @@ function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}} export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) { const auth = createAuth(options.appId, options.apiKey, options.authMode); const transporter = createTransporter({ - hosts: options?.hosts ?? getDefaultHosts({{^hasRegionalHost}}{{^isExperimentalHost}}options.appId{{/isExperimentalHost}}{{/hasRegionalHost}}{{#hasRegionalHost}}options.region{{/hasRegionalHost}}), + hosts: options?.hosts ?? getDefaultHosts({{^hasRegionalHost}}options.appId{{/hasRegionalHost}}{{#hasRegionalHost}}options.region{{/hasRegionalHost}}), hostsCache: options.hostsCache, requestsCache: options.requestsCache, responsesCache: options.responsesCache, diff --git a/templates/php/api.mustache b/templates/php/api.mustache index 4cdb99e280..72a5f7c968 100644 --- a/templates/php/api.mustache +++ b/templates/php/api.mustache @@ -102,7 +102,8 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts; // If a list of hosts was passed, we ignore the cache $clusterHosts = ClusterHosts::create($hosts); } else { - $clusterHosts = ClusterHosts::create('{{host}}.'.$config->getRegion().'.algolia.{{topLevelDomain}}'); + $url = str_replace('{region}', $config->getRegion(), '{{{host}}}'); + $clusterHosts = ClusterHosts::create($url); } {{/useCache}} diff --git a/tests/CTS/client/abtesting/parameters.json b/tests/CTS/client/abtesting/parameters.json index ae44b7123b..545d419972 100644 --- a/tests/CTS/client/abtesting/parameters.json +++ b/tests/CTS/client/abtesting/parameters.json @@ -7,8 +7,7 @@ "type": "createClient", "parameters": { "appId": "my-app-id", - "apiKey": "my-api-key", - "region": "" + "apiKey": "my-api-key" }, "expected": { "error": false diff --git a/tests/CTS/client/analytics/parameters.json b/tests/CTS/client/analytics/parameters.json index ff8ebbaba1..7a1793fd4d 100644 --- a/tests/CTS/client/analytics/parameters.json +++ b/tests/CTS/client/analytics/parameters.json @@ -7,8 +7,7 @@ "type": "createClient", "parameters": { "appId": "my-app-id", - "apiKey": "my-api-key", - "region": "" + "apiKey": "my-api-key" }, "expected": { "error": false diff --git a/tests/CTS/client/insights/parameters.json b/tests/CTS/client/insights/parameters.json index eb0430b4ce..24e08c48d4 100644 --- a/tests/CTS/client/insights/parameters.json +++ b/tests/CTS/client/insights/parameters.json @@ -7,8 +7,7 @@ "type": "createClient", "parameters": { "appId": "my-app-id", - "apiKey": "my-api-key", - "region": "" + "apiKey": "my-api-key" }, "expected": { "error": false