Skip to content

Commit

Permalink
chore: generated code for commit 7665aef.
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Millot <[email protected]>
  • Loading branch information
algolia-bot and millotp committed Jul 18, 2024
1 parent 7665aef commit 0c2874d
Show file tree
Hide file tree
Showing 25 changed files with 905 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5801,8 +5801,8 @@ public void waitForAppTask(Long taskID) {
* the transporter requestOptions. (optional)
*/
public GetApiKeyResponse waitForApiKey(
ApiKeyOperation operation,
String key,
ApiKeyOperation operation,
ApiKey apiKey,
int maxRetries,
IntUnaryOperator timeout,
Expand Down Expand Up @@ -5835,28 +5835,26 @@ public GetApiKeyResponse waitForApiKey(
);
}

// bypass lambda restriction to modify final object
final GetApiKeyResponse[] addedKey = new GetApiKeyResponse[] { null };

// check the status of the getApiKey method
TaskUtils.retryUntil(
return TaskUtils.retryUntil(
() -> {
try {
addedKey[0] = this.getApiKey(key, requestOptions);
// magic number to signify we found the key
return -2;
return this.getApiKey(key, requestOptions);
} catch (AlgoliaApiException e) {
return e.getStatusCode();
if (e.getStatusCode() == 404) {
return null;
}

throw e;
}
},
(Integer status) -> {
(GetApiKeyResponse response) -> {
switch (operation) {
case ADD:
// stop either when the key is created or when we don't receive 404
return status == -2 || status != 404;
// stop when we don't receive 404 meaning the key is created
return response != null;
case DELETE:
// stop when the key is not found
return status == 404;
return response == null;
default:
// continue
return false;
Expand All @@ -5865,105 +5863,103 @@ public GetApiKeyResponse waitForApiKey(
maxRetries,
timeout
);

return addedKey[0];
}

/**
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added or deleted.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200,
* 5000) by default. (optional)
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public GetApiKeyResponse waitForApiKey(
ApiKeyOperation operation,
String key,
ApiKeyOperation operation,
int maxRetries,
IntUnaryOperator timeout,
RequestOptions requestOptions
) {
return this.waitForApiKey(operation, key, null, maxRetries, timeout, requestOptions);
return this.waitForApiKey(key, operation, null, maxRetries, timeout, requestOptions);
}

/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param operation The `operation` that was done on a `key`.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of
* the response with it.
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, RequestOptions requestOptions) {
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, ApiKey apiKey, RequestOptions requestOptions) {
return this.waitForApiKey(key, operation, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}

/**
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added or deleted.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, RequestOptions requestOptions) {
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, RequestOptions requestOptions) {
return this.waitForApiKey(key, operation, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}

/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param operation The `operation` that was done on a `key`.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of
* the response with it.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200,
* 5000) by default. (optional)
*/
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, int maxRetries, IntUnaryOperator timeout) {
return this.waitForApiKey(operation, key, apiKey, maxRetries, timeout, null);
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, ApiKey apiKey, int maxRetries, IntUnaryOperator timeout) {
return this.waitForApiKey(key, operation, apiKey, maxRetries, timeout, null);
}

/**
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added or deleted.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200,
* 5000) by default. (optional)
*/
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout) {
return this.waitForApiKey(operation, key, null, maxRetries, timeout, null);
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, int maxRetries, IntUnaryOperator timeout) {
return this.waitForApiKey(key, operation, null, maxRetries, timeout, null);
}

/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param operation The `operation` that was done on a `key`.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of
* the response with it.
*/
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey) {
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, ApiKey apiKey) {
return this.waitForApiKey(key, operation, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}

/**
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
*
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added or deleted.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
*/
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key) {
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation) {
return this.waitForApiKey(key, operation, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,19 @@ export function createSearchClient({
Math.min(retryCount * 200, 5000),
}: WaitForApiKeyOptions,
requestOptions?: RequestOptions
): Promise<ApiError | GetApiKeyResponse> {
): Promise<GetApiKeyResponse | undefined> {
let retryCount = 0;
const baseIteratorOptions: IterableOptions<ApiError | GetApiKeyResponse> =
{
aggregator: () => (retryCount += 1),
error: {
validate: () => retryCount >= maxRetries,
message: () =>
`The maximum number of retries exceeded. (${retryCount}/${maxRetries})`,
},
timeout: () => timeout(retryCount),
};
const baseIteratorOptions: IterableOptions<
GetApiKeyResponse | undefined
> = {
aggregator: () => (retryCount += 1),
error: {
validate: () => retryCount >= maxRetries,
message: () =>
`The maximum number of retries exceeded. (${retryCount}/${maxRetries})`,
},
timeout: () => timeout(retryCount),
};

if (operation === 'update') {
if (!apiKey) {
Expand Down Expand Up @@ -366,9 +367,15 @@ export function createSearchClient({
return createIterablePromise({
...baseIteratorOptions,
func: () =>
this.getApiKey({ key }, requestOptions).catch((error) => error),
validate: (error: ApiError) =>
operation === 'add' ? error.status !== 404 : error.status === 404,
this.getApiKey({ key }, requestOptions).catch((error: ApiError) => {
if (error.status === 404) {
return undefined;
}

throw error;
}),
validate: (response) =>
operation === 'add' ? response !== undefined : response === undefined,
});
},

Expand Down
8 changes: 4 additions & 4 deletions clients/algoliasearch-client-php/lib/Api/SearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2817,8 +2817,8 @@ function ($res) {return 'published' === $res['status']; },
/**
* Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @param string $operation the `operation` that was done on a `key`
* @param string $key the `key` that has been added, deleted or updated
* @param string $operation the `operation` that was done on a `key`
* @param array $apiKey necessary to know if an `update` operation has been processed, compare fields of the response with it
* @param null|int $maxRetries Maximum number of retries
* @param null|int $timeout Timeout
Expand All @@ -2827,8 +2827,8 @@ function ($res) {return 'published' === $res['status']; },
* @throws ExceededRetriesException
*/
public function waitForApiKey(
$operation,
$key,
$operation,
$apiKey = null,
$maxRetries = null,
$timeout = null,
Expand All @@ -2842,14 +2842,14 @@ public function waitForApiKey(
$maxRetries = $this->config->getDefaultMaxRetries();
}

Helpers::retryForApiKeyUntil(
return Helpers::retryForApiKeyUntil(
$operation,
$this,
$key,
$apiKey,
$maxRetries,
$timeout,
null,
'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout',
$requestOptions
);
}
Expand Down
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-php/lib/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static function retryForApiKeyUntil(
// In case of a deletion, if there was an error, the $key has been deleted as it should be
if (
'delete' === $operation
&& $e->getCode() === 404
&& 404 === $e->getCode()
) {
return null;
}
Expand Down
27 changes: 16 additions & 11 deletions clients/algoliasearch-client-python/algoliasearch/search/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ def _aggregator(_: GetTaskResponse) -> None:

async def wait_for_api_key(
self,
operation: str,
key: str,
operation: str,
api_key: Optional[ApiKey] = None,
max_retries: int = 50,
timeout: RetryTimeout = RetryTimeout(),
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> GetApiKeyResponse:
) -> GetApiKeyResponse | None:
"""
Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
"""
Expand All @@ -271,7 +271,7 @@ async def wait_for_api_key(
"`apiKey` is required when waiting for an `update` operation."
)

async def _func(_prev: GetApiKeyResponse) -> GetApiKeyResponse:
async def _func(_prev: GetApiKeyResponse | None) -> GetApiKeyResponse | None:
try:
return await self.get_api_key(key=key, request_options=request_options)
except RequestException as e:
Expand All @@ -281,20 +281,25 @@ async def _func(_prev: GetApiKeyResponse) -> GetApiKeyResponse:
return None
raise e

def _aggregator(_: GetApiKeyResponse) -> None:
def _aggregator(_: GetApiKeyResponse | None) -> None:
self._retry_count += 1

def _validate(_resp: GetApiKeyResponse) -> bool:
def _validate(_resp: GetApiKeyResponse | None) -> bool:
if operation == "update":
for field in api_key:
if isinstance(api_key[field], list) and isinstance(
_resp[field], list
resp_dict = _resp.to_dict()
api_key_dict = (
api_key.to_dict() if isinstance(api_key, ApiKey) else api_key
)
for field in api_key_dict:
if isinstance(api_key_dict[field], list) and isinstance(
resp_dict[field], list
):
if len(api_key[field]) != len(_resp[field]) or any(
v != _resp[field][i] for i, v in enumerate(api_key[field])
if len(api_key_dict[field]) != len(resp_dict[field]) or any(
v != resp_dict[field][i]
for i, v in enumerate(api_key_dict[field])
):
return False
elif api_key[field] != _resp[field]:
elif api_key_dict[field] != resp_dict[field]:
return False
return True
elif operation == "add":
Expand Down
30 changes: 15 additions & 15 deletions clients/algoliasearch-client-ruby/lib/algolia/api/search_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3179,16 +3179,16 @@ def wait_for_app_task(

# Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
#
# @param operation [String] the `operation` that was done on a `key`.
# @param key [String] the `key` that has been added, deleted or updated.
# @param operation [String] the `operation` that was done on a `key`.
# @param api_key [Hash] necessary to know if an `update` operation has been processed, compare fields of the response with it.
# @param max_retries [Integer] the maximum number of retries.
# @param timeout [Proc] the function to decide how long to wait between retries.
# @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `getApikey` method and merged with the transporter requestOptions.
# @return [Http::Response] the last get_api_key response
def wait_for_api_key(
operation,
key,
operation,
api_key = {},
max_retries = 50,
timeout = -> (retry_count) { [retry_count * 200, 5000].min },
Expand All @@ -3198,19 +3198,15 @@ def wait_for_api_key(
if operation == "update"
raise ArgumentError, "`api_key` is required when waiting for an `update` operation." if api_key.nil?
while retries < max_retries
begin
updatad_key = get_api_key(key, request_options)
updated_key_hash = updatad_key.to_hash
equals = true
api_key.to_hash.each do |k, v|
equals &&= updated_key_hash[k] == v
end

return updatad_key if equals
rescue AlgoliaError => e
raise e unless e.code == 404
updated_key = get_api_key(key, request_options)
updated_key_hash = updated_key.to_hash
equals = true
api_key.to_hash.each do |k, v|
equals &&= updated_key_hash[k] == v
end

return updated_key if equals

retries += 1
sleep(timeout.call(retries) / 1000.0)
end
Expand All @@ -3222,8 +3218,12 @@ def wait_for_api_key(
begin
res = get_api_key(key, request_options)
return res if operation == "add"
rescue AlgoliaError => e
return res if operation == "delete" && e.code == 404
rescue AlgoliaHttpError => e
if e.code == 404
return nil if operation == "delete"
else
raise e
end
end

retries += 1
Expand Down
Loading

0 comments on commit 0c2874d

Please sign in to comment.