Skip to content

Commit

Permalink
fix(generator): add hostWithFallback (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 6, 2022
1 parent 22ee27e commit 68ad731
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 46 deletions.
18 changes: 5 additions & 13 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void generateServer(
boolean hasRegionalHost = false;
boolean fallbackToAliasHost = false;
String host = "";
String topLevelDomain = "";
String hostWithFallback = "";
Set<String> allowedRegions = new HashSet<>();
for (Map<String, Object> server : servers) {
if (!server.containsKey("url")) {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion specs/predict/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
Expand Down
21 changes: 8 additions & 13 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

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

{{^hasRegionalHost}}{{^isExperimentalHost}}
{{^hasRegionalHost}}
private static List<StatefulHost> getDefaultHosts(String appId) {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
hosts.add(new StatefulHost(appId + "-dsn.algolia.net", "https", EnumSet.of(CallType.READ)));
Expand All @@ -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<StatefulHost> getDefaultHosts() {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
hosts.add(new StatefulHost("{{{host}}}", "https", EnumSet.of(CallType.READ, CallType.WRITE)));
return hosts;
}
{{/isExperimentalHost}}
{{/hasRegionalHost}}

{{#hasRegionalHost}}
private static List<StatefulHost> getDefaultHosts(String region) {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
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}}
Expand Down
16 changes: 5 additions & 11 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
[
Expand Down Expand Up @@ -66,27 +66,21 @@ 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}}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
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,
Expand Down
3 changes: 2 additions & 1 deletion templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
3 changes: 1 addition & 2 deletions tests/CTS/client/abtesting/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
"apiKey": "my-api-key"
},
"expected": {
"error": false
Expand Down
3 changes: 1 addition & 2 deletions tests/CTS/client/analytics/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
"apiKey": "my-api-key"
},
"expected": {
"error": false
Expand Down
3 changes: 1 addition & 2 deletions tests/CTS/client/insights/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
"apiKey": "my-api-key"
},
"expected": {
"error": false
Expand Down

0 comments on commit 68ad731

Please sign in to comment.