Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clients): support the rest of the languages #2571

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ public static void generateServers(List<CodegenServer> servers, Map<String, Obje
regionalHost = url.getHost();
}

if (servers.size() == 1 && hostWithFallback.isEmpty() && !hasRegionalHost) {
URL url = new URL(servers.get(0).url);
bundle.put("uniqueHost", url.getHost());
if (!hasRegionalHost) {
if (servers.size() == 1 && hostWithFallback.isEmpty()) {
URL url = new URL(servers.get(0).url);
bundle.put("uniqueHost", url.getHost());
} else {
bundle.put("hostWithAppID", true);
}
}

bundle.put("hostWithFallback", hostWithFallback);
Expand Down
11 changes: 5 additions & 6 deletions templates/csharp/Configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ namespace Algolia.Search.Clients
/// <param name="apiKey">Your API Key</param>
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<StatefulHost> GetDefaultHosts(string appId)
{
List<StatefulHost> hosts = new List<StatefulHost>
Expand Down Expand Up @@ -86,8 +86,7 @@ namespace Algolia.Search.Clients
hosts.AddRange(commonHosts);
return hosts;
}
{{/uniqueHost}}
{{/hasRegionalHost}}
{{/hostWithAppID}}
{{#hasRegionalHost}}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down Expand Up @@ -124,7 +123,7 @@ namespace Algolia.Search.Clients
{
new StatefulHost
{
Url = "{{{uniqueHost}}}",
Url = "{{{.}}}",
Up = true,
LastUse = DateTime.UtcNow,
Accept = CallType.Read | CallType.Write
Expand All @@ -133,4 +132,4 @@ namespace Algolia.Search.Clients
}
{{/uniqueHost}}
}
}
}
7 changes: 5 additions & 2 deletions templates/dart/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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}}];
Expand All @@ -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.');
Expand Down
12 changes: 9 additions & 3 deletions templates/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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),
Expand All @@ -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())
}
Expand Down
8 changes: 4 additions & 4 deletions templates/java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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<Host> getDefaultHosts(String appId) {
List<Host> hosts = new ArrayList<>();
hosts.add(new Host(appId + "-dsn.algolia.net", EnumSet.of(CallType.READ)));
Expand All @@ -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<Host> getDefaultHosts(String region) throws AlgoliaRuntimeException {
Expand All @@ -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<Host> getDefaultHosts() {
Expand Down
2 changes: 1 addition & 1 deletion templates/javascript/clients/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions templates/javascript/clients/client/api/hosts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
[
{
Expand Down Expand Up @@ -41,7 +41,7 @@ function getDefaultHosts({{^uniqueHost}}appId: string{{/uniqueHost}}): Host[] {
},
])
);
{{/uniqueHost}}
{{/hostWithAppID}}
}
{{/hasRegionalHost}}

Expand Down
7 changes: 5 additions & 2 deletions templates/kotlin/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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}}
Expand All @@ -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}}
Expand Down
19 changes: 14 additions & 5 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -430,4 +439,4 @@ use {{invokerPackage}}\Support\Helpers;
);
}
}
{{/operations}}
{{/operations}}
8 changes: 4 additions & 4 deletions templates/python/config.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ 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),
Host("{}-1.algolianet.com".format(self.app_id)),
Host("{}-2.algolianet.com".format(self.app_id)),
Host("{}-3.algolianet.com".format(self.app_id)),
]
{{/uniqueHost}}
{{/hostWithAppID}}
)
{{/hasRegionalHost}}
{{/hasRegionalHost}}
8 changes: 6 additions & 2 deletions templates/ruby/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions templates/scala/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}}(
Expand All @@ -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
) {
Expand Down