Skip to content

Commit

Permalink
fix(clients): correly parse usage hosts (#3622) (generated) [skip ci]
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
  • Loading branch information
2 people authored and millotp committed Aug 30, 2024
1 parent 9159f05 commit 38a5341
Show file tree
Hide file tree
Showing 34 changed files with 82 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ private static List<StatefulHost> GetDefaultHosts(string region)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ private static List<StatefulHost> GetDefaultHosts(string region)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ private static List<StatefulHost> GetDefaultHosts(string region)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ private static List<StatefulHost> GetDefaultHosts(string region)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ private static List<StatefulHost> GetDefaultHosts()
Up = true,
LastUse = DateTime.UtcNow,
Accept = CallType.Read | CallType.Write
}
},
};
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ private static List<StatefulHost> GetDefaultHosts(string region)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ private static List<StatefulHost> GetDefaultHosts(string region)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,3 @@ private static List<StatefulHost> GetDefaultHosts(string appId)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,3 @@ private static List<StatefulHost> GetDefaultHosts(string appId)
return hosts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,27 @@ public sealed class UsageConfig : AlgoliaConfig
/// <param name="apiKey">Your API Key</param>
public UsageConfig(string appId, string apiKey) : base(appId, apiKey, "Usage")
{
DefaultHosts = GetDefaultHosts(appId);
DefaultHosts = GetDefaultHosts();
Compression = CompressionType.None;
}
private static List<StatefulHost> GetDefaultHosts(string appId)
private static List<StatefulHost> GetDefaultHosts()
{
var hosts = new List<StatefulHost>
return new List<StatefulHost>
{
new()
{
Url = $"{appId}-dsn.algolia.net",
Url = "usage.algolia.com",
Up = true,
LastUse = DateTime.UtcNow,
Accept = CallType.Read
Accept = CallType.Read | CallType.Write
},
new()
{
Url = $"{appId}.algolia.net", Up = true, LastUse = DateTime.UtcNow, Accept = CallType.Write,
}
};

var commonHosts = new List<StatefulHost>
{
new()
{
Url = $"{appId}-1.algolianet.com",
Url = "usage-dev.algolia.com",
Up = true,
LastUse = DateTime.UtcNow,
Accept = CallType.Read | CallType.Write,
Accept = CallType.Read | CallType.Write
},
new()
{
Url = $"{appId}-2.algolianet.com",
Up = true,
LastUse = DateTime.UtcNow,
Accept = CallType.Read | CallType.Write,
},
new()
{
Url = $"{appId}-3.algolianet.com",
Up = true,
LastUse = DateTime.UtcNow,
Accept = CallType.Read | CallType.Write,
}
}.Shuffle();

hosts.AddRange(commonHosts);
return hosts;
};
}
}

4 changes: 3 additions & 1 deletion clients/algoliasearch-client-go/algolia/monitoring/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 5 additions & 13 deletions clients/algoliasearch-client-go/algolia/usage/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
import com.algolia.utils.*;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

public class UsageClient extends ApiClient {
Expand All @@ -28,22 +24,14 @@ public UsageClient(String appId, String apiKey) {
}

public UsageClient(String appId, String apiKey, ClientOptions options) {
super(appId, apiKey, "Usage", options, getDefaultHosts(appId));
super(appId, apiKey, "Usage", options, getDefaultHosts());
}

private static List<Host> getDefaultHosts(String appId) {
private static List<Host> getDefaultHosts() {
List<Host> hosts = new ArrayList<>();
hosts.add(new Host(appId + "-dsn.algolia.net", EnumSet.of(CallType.READ)));
hosts.add(new Host(appId + ".algolia.net", EnumSet.of(CallType.WRITE)));

List<Host> commonHosts = new ArrayList<>();
hosts.add(new Host(appId + "-1.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new Host(appId + "-2.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new Host(appId + "-3.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));

Collections.shuffle(commonHosts, new Random());

return Stream.concat(hosts.stream(), commonHosts.stream()).collect(Collectors.toList());
hosts.add(new Host("usage.algolia.com", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new Host("usage-dev.algolia.com", EnumSet.of(CallType.READ, CallType.WRITE)));
return hosts;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.

import { createAuth, createTransporter, getAlgoliaAgent, shuffle } from '@algolia/client-common';
import { createAuth, createTransporter, getAlgoliaAgent } from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Expand All @@ -22,39 +22,11 @@ import type { IndexUsage } from '../model/indexUsage';

export const apiClientVersion = '1.2.3';

function getDefaultHosts(appId: string): Host[] {
return (
[
{
url: `${appId}-dsn.algolia.net`,
accept: 'read',
protocol: 'https',
},
{
url: `${appId}.algolia.net`,
accept: 'write',
protocol: 'https',
},
] as Host[]
).concat(
shuffle([
{
url: `${appId}-1.algolianet.com`,
accept: 'readWrite',
protocol: 'https',
},
{
url: `${appId}-2.algolianet.com`,
accept: 'readWrite',
protocol: 'https',
},
{
url: `${appId}-3.algolianet.com`,
accept: 'readWrite',
protocol: 'https',
},
]),
);
function getDefaultHosts(): Host[] {
return [
{ url: 'usage.algolia.com', accept: 'readWrite', protocol: 'https' },
{ url: 'usage-dev.algolia.com', accept: 'readWrite', protocol: 'https' },
];
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
Expand All @@ -67,7 +39,7 @@ export function createUsageClient({
}: CreateClientOptions) {
const auth = createAuth(appIdOption, apiKeyOption, authMode);
const transporter = createTransporter({
hosts: getDefaultHosts(appIdOption),
hosts: getDefaultHosts(),
...options,
algoliaAgent: getAlgoliaAgent({
algoliaAgents,
Expand Down Expand Up @@ -274,7 +246,6 @@ export function createUsageClient({
.replace('{indexName}', encodeURIComponent(indexName));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class MonitoringClient(
}

override val requester: Requester = requesterOf(clientName = "Monitoring", appId = appId, apiKey = apiKey, options = options) {
listOf(Host("status.algolia.com"))
listOf(
Host("status.algolia.com"),
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ public class UsageClient(

override val requester: Requester = requesterOf(clientName = "Usage", appId = appId, apiKey = apiKey, options = options) {
listOf(
Host("$appId-dsn.algolia.net", CallType.Read),
Host("$appId.algolia.net", CallType.Write),
) + mutableListOf(
Host("$appId-1.algolianet.com"),
Host("$appId-2.algolianet.com"),
Host("$appId-3.algolianet.com"),
).apply { shuffle() }
Host("usage.algolia.com"),
Host("usage-dev.algolia.com"),
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public static function getClusterHosts(MonitoringConfig $config)
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} else {
$clusterHosts = ClusterHosts::create('status.algolia.com');
$clusterHosts = ClusterHosts::create([
'status.algolia.com',
]);
}

return $clusterHosts;
Expand Down
13 changes: 5 additions & 8 deletions clients/algoliasearch-client-php/lib/Api/UsageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,14 @@ public static function createWithConfig(UsageConfig $config)
*/
public static function getClusterHosts(UsageConfig $config)
{
$cacheKey = sprintf('%s-clusterHosts-%s', __CLASS__, $config->getAppId());

if ($hosts = $config->getHosts()) {
// If a list of hosts was passed, we ignore the cache
$clusterHosts = ClusterHosts::create($hosts);
} elseif (false === ($clusterHosts = ClusterHosts::createFromCache($cacheKey))) {
// We'll try to restore the ClusterHost from cache, if we cannot
// we create a new instance and set the cache key
$clusterHosts = ClusterHosts::createFromAppId($config->getAppId())
->setCacheKey($cacheKey)
;
} else {
$clusterHosts = ClusterHosts::create([
'usage.algolia.com',
'usage-dev.algolia.com',
]);
}

return $clusterHosts;
Expand Down
20 changes: 4 additions & 16 deletions clients/algoliasearch-client-python/algoliasearch/usage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

from algoliasearch.http.base_config import BaseConfig
from algoliasearch.http.hosts import CallType, Host, HostsCollection
from algoliasearch.http.hosts import Host, HostsCollection
from algoliasearch.http.user_agent import UserAgent


Expand Down Expand Up @@ -32,19 +32,7 @@ def __init__(

self.hosts = HostsCollection(
[
Host(
url="{}-dsn.algolia.net".format(self.app_id),
priority=10,
accept=CallType.READ,
),
Host(
url="{}.algolia.net".format(self.app_id),
priority=10,
accept=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)),
],
reorder_hosts=True,
Host("usage.algolia.com"),
Host("usage-dev.algolia.com"),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@ def initialize(config = nil)

def self.create(app_id, api_key, opts = {})
hosts = []
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
hosts << Transport::StatefulHost.new("usage.algolia.com", accept: CallType::READ | CallType::WRITE)
hosts << Transport::StatefulHost.new("usage-dev.algolia.com", accept: CallType::READ | CallType::WRITE)

config = Algolia::Configuration.new(app_id, api_key, hosts, "Usage", opts)
create_with_config(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ object MonitoringClient {
)

private def hosts(): Seq[Host] = {
List(Host("status.algolia.com", Set(CallType.Read, CallType.Write)))
List(
Host("status.algolia.com", Set(CallType.Read, CallType.Write))
)
}
}

Expand Down
Loading

0 comments on commit 38a5341

Please sign in to comment.