From 68ff7ee4d1910f57667f7a3fa3c65280c995e9cb Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 25 Aug 2022 17:27:42 +0200 Subject: [PATCH] feat(java): browse objects/synonyms/rules (#952) --- .../algolia/utils/AlgoliaIterableHelper.java | 38 ++ .../main/java/com/algolia/utils/Holder.java | 14 + .../java/com/algolia/playground/Search.java | 1 + .../search/paths/synonyms/common/schemas.yml | 11 - .../search/paths/synonyms/searchSynonyms.yml | 4 +- templates/java/api.mustache | 244 +----------- templates/java/api_helpers.mustache | 357 ++++++++++++++++++ 7 files changed, 421 insertions(+), 248 deletions(-) create mode 100644 clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/AlgoliaIterableHelper.java create mode 100644 clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/Holder.java create mode 100644 templates/java/api_helpers.mustache diff --git a/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/AlgoliaIterableHelper.java b/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/AlgoliaIterableHelper.java new file mode 100644 index 0000000000..c1c72176f9 --- /dev/null +++ b/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/AlgoliaIterableHelper.java @@ -0,0 +1,38 @@ +package com.algolia.utils; + +import java.util.Iterator; +import java.util.function.BooleanSupplier; +import java.util.function.Supplier; + +public class AlgoliaIterableHelper { + + public static Iterable createIterable(Supplier> executeQuery, BooleanSupplier _hasNext) { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + private boolean isFirstRequest = true; + private Iterator currentIterator = null; + + @Override + public boolean hasNext() { + if (isFirstRequest || (_hasNext.getAsBoolean() && !currentIterator.hasNext())) { + currentIterator = executeQuery.get(); + isFirstRequest = false; + } + return currentIterator != null && currentIterator.hasNext(); + } + + @Override + public T next() { + if (currentIterator == null || !currentIterator.hasNext()) { + currentIterator = executeQuery.get(); + isFirstRequest = false; + } + return currentIterator.next(); + } + }; + } + }; + } +} diff --git a/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/Holder.java b/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/Holder.java new file mode 100644 index 0000000000..833b5e5e1e --- /dev/null +++ b/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/Holder.java @@ -0,0 +1,14 @@ +package com.algolia.utils; + +public class Holder { + + public T value; + + public Holder() { + this.value = null; + } + + public Holder(T value) { + this.value = value; + } +} diff --git a/playground/java/src/main/java/com/algolia/playground/Search.java b/playground/java/src/main/java/com/algolia/playground/Search.java index 65620c9107..12389d0b90 100644 --- a/playground/java/src/main/java/com/algolia/playground/Search.java +++ b/playground/java/src/main/java/com/algolia/playground/Search.java @@ -64,6 +64,7 @@ public static void main(String[] args) { SearchResponses sr = result.get(); Actor a = sr.getResults().get(0).getHits().get(0); System.out.println(a.name); + } catch (InterruptedException e) { System.err.println("InterrupedException" + e.getMessage()); e.printStackTrace(); diff --git a/specs/search/paths/synonyms/common/schemas.yml b/specs/search/paths/synonyms/common/schemas.yml index 511f786b7e..f82714d8c0 100644 --- a/specs/search/paths/synonyms/common/schemas.yml +++ b/specs/search/paths/synonyms/common/schemas.yml @@ -39,17 +39,6 @@ synonymHit: items: type: string description: List of query words that will match the token. - _highlightResult: - type: object - description: Highlighted results. - additionalProperties: false - properties: - type: - $ref: '../../../common/schemas/Hit.yml#/highlightResultMap' - synonyms: - type: array - items: - $ref: '../../../common/schemas/Hit.yml#/highlightResultMap' required: - objectID - type diff --git a/specs/search/paths/synonyms/searchSynonyms.yml b/specs/search/paths/synonyms/searchSynonyms.yml index 73be6ace7c..6e3ea3a730 100644 --- a/specs/search/paths/synonyms/searchSynonyms.yml +++ b/specs/search/paths/synonyms/searchSynonyms.yml @@ -8,7 +8,7 @@ post: description: Search or browse all synonyms, optionally filtering them by type. parameters: - $ref: '../../../common/parameters.yml#/IndexName' - - $ref: 'common/parameters.yml#/Type' + - $ref: './common/parameters.yml#/Type' - $ref: '../../../common/parameters.yml#/PageDefault0' - $ref: '../../../common/parameters.yml#/HitsPerPage' requestBody: @@ -28,7 +28,7 @@ post: content: application/json: schema: - $ref: 'common/schemas.yml#/searchSynonymsResponse' + $ref: './common/schemas.yml#/searchSynonymsResponse' '400': $ref: '../../../common/responses/BadRequest.yml' '402': diff --git a/templates/java/api.mustache b/templates/java/api.mustache index 558d5e87cf..627050618a 100644 --- a/templates/java/api.mustache +++ b/templates/java/api.mustache @@ -28,7 +28,6 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.concurrent.CompletableFuture; - {{#operations}} public class {{classname}} extends ApiClient { {{#hasRegionalHost}} @@ -124,7 +123,7 @@ public class {{classname}} extends ApiClient { /** * {{{notes}}}{{#allParams}} * @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. * @return {{> return_type}} {{> api_javadoc}} @@ -136,7 +135,7 @@ public class {{classname}} extends ApiClient { /** * {{{notes}}}{{#allParams}} * @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @return {{> return_type}} {{> api_javadoc}} public {{> return_type}} {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#vendorExtensions.x-is-generic}}, Class innerType{{/vendorExtensions.x-is-generic}}) throws AlgoliaRuntimeException { @@ -148,7 +147,7 @@ public class {{classname}} extends ApiClient { /** * {{{notes}}}{{#requiredParams}} * @param {{paramName}} {{{description}}} (required){{/requiredParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. * @return {{> return_type}} {{> api_javadoc}} @@ -162,7 +161,7 @@ public class {{classname}} extends ApiClient { /** * {{{notes}}}{{#requiredParams}} * @param {{paramName}} {{{description}}} (required){{/requiredParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @return {{> return_type}} {{> api_javadoc}} public {{> return_type}} {{operationId}}({{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#vendorExtensions.x-is-generic}}, Class innerType{{/vendorExtensions.x-is-generic}}) throws AlgoliaRuntimeException { @@ -174,7 +173,7 @@ public class {{classname}} extends ApiClient { * (asynchronously) * {{notes}}{{#allParams}} * @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. * @return {{> return_type_async}} The awaitable future {{> api_javadoc}} @@ -224,7 +223,7 @@ public class {{classname}} extends ApiClient { * (asynchronously) * {{notes}}{{#allParams}} * @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @return {{> return_type_async}} The awaitable future {{> api_javadoc}} public {{> return_type_async}} {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#vendorExtensions.x-is-generic}}, Class innerType{{/vendorExtensions.x-is-generic}}) throws AlgoliaRuntimeException { @@ -237,7 +236,7 @@ public class {{classname}} extends ApiClient { * (asynchronously) * {{notes}}{{#requiredParams}} * @param {{paramName}} {{{description}}} (required){{/requiredParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. * @return {{> return_type_async}} The awaitable future {{> api_javadoc}} @@ -252,7 +251,7 @@ public class {{classname}} extends ApiClient { * (asynchronously) * {{notes}}{{#requiredParams}} * @param {{paramName}} {{{description}}} (required){{/requiredParams}}{{#vendorExtensions.x-is-generic}} - * @param innerType The class held by the index, could be your custom class or {@link Object}{{/vendorExtensions.x-is-generic}} + * @param innerType The class held by the index, could be your custom class or {@link Object}.{{/vendorExtensions.x-is-generic}} * @return {{> return_type_async}} The awaitable future {{> api_javadoc}} public {{> return_type_async}} {{operationId}}Async({{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#vendorExtensions.x-is-generic}}, Class innerType{{/vendorExtensions.x-is-generic}}) throws AlgoliaRuntimeException { @@ -261,231 +260,6 @@ public class {{classname}} extends ApiClient { {{/optionalParams.0}} {{/operation}} - {{#isSearchClient}} - /** - * Helper: Wait for a task to complete with `indexName` and `taskID`. - * - * @summary Wait for a task to complete. - * @param indexName The `indexName` where the operation was performed. - * @param taskID The `taskID` returned in the method response. - * @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 void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) { - TaskUtils.retryUntil(() -> { - return this.getTask(indexName, taskID, requestOptions); - }, (GetTaskResponse task) -> { - return task.getStatus() == TaskStatus.PUBLISHED; - }, maxRetries, timeout); - } - - /** - * Helper: Wait for a task to complete with `indexName` and `taskID`. - * - * @summary Wait for a task to complete. - * @param indexName The `indexName` where the operation was performed. - * @param taskID The `taskID` returned in the method response. - * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional) - */ - public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) { - this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions); - } - - /** - * Helper: Wait for a task to complete with `indexName` and `taskID`. - * - * @summary Wait for a task to complete. - * @param indexName The `indexName` where the operation was performed. - * @param taskID The `taskID` returned in the method response. - * @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 void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) { - this.waitForTask(indexName, taskID, maxRetries, timeout, null); - } - - /** - * Helper: Wait for a task to complete with `indexName` and `taskID`. - * - * @summary Wait for a task to complete. - * @param indexName The `indexName` where the operation was performed. - * @param taskID The `taskID` returned in the method response. - */ - public void waitForTask(String indexName, Long taskID) { - this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. - * @param key The `key` that has been added, deleted or updated. - * @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) - * @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, - int maxRetries, - IntUnaryOperator timeout, - RequestOptions requestOptions - ) { - if (operation == ApiKeyOperation.UPDATE) { - if (apiKey == null) { - throw new AlgoliaRetryException("`apiKey` is required when waiting for an `update` operation."); - } - - // when updating an api key, we poll the api until we receive a different key - return TaskUtils.retryUntil( - () -> { - return this.getApiKey(key, requestOptions); - }, - (GetApiKeyResponse respKey) -> { - // we need to convert to an ApiKey object to use the `equals` method - ApiKey sameType = new ApiKey() - .setAcl(respKey.getAcl()) - .setDescription(respKey.getDescription()) - .setIndexes(respKey.getIndexes()) - .setMaxHitsPerQuery(respKey.getMaxHitsPerQuery()) - .setMaxQueriesPerIPPerHour(respKey.getMaxQueriesPerIPPerHour()) - .setQueryParameters(respKey.getQueryParameters()) - .setReferers(respKey.getReferers()) - .setValidity(respKey.getValidity()); - - return apiKey.equals(sameType); - }, - maxRetries, - timeout - ); - } - - // bypass lambda restriction to modify final object - final GetApiKeyResponse[] addedKey = new GetApiKeyResponse[] { null }; - - // check the status of the getApiKey method - TaskUtils.retryUntil( - () -> { - try { - addedKey[0] = this.getApiKey(key, requestOptions); - // magic number to signify we found the key - return -2; - } catch (AlgoliaApiException e) { - return e.getHttpErrorCode(); - } - }, - (Integer status) -> { - switch (operation) { - case ADD: - // stop either when the key is created or when we don't receive 404 - return status == -2 || status != 404; - case DELETE: - // stop when the key is not found - return status == 404; - default: - // continue - return false; - } - }, - maxRetries, - timeout - ); - - return addedKey[0]; - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) - * @param key The `key` that has been added, deleted or updated. - * @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, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) { - return this.waitForApiKey(operation, key, null, maxRetries, timeout, requestOptions); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. - * @param key The `key` that has been added, deleted or updated. - * @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); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) - * @param key The `key` that has been added, deleted or updated. - * @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); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. - * @param key The `key` that has been added, deleted or updated. - * @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); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) - * @param key The `key` that has been added, deleted or updated. - * @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); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. - * @param key The `key` that has been added, deleted or updated. - * @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); - } - - /** - * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. - * - * @summary Wait for an API key task to be processed. - * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) - * @param key The `key` that has been added, deleted or updated. - */ - public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key) { - return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null); - } - {{/isSearchClient}} + {{> api_helpers}} } {{/operations}} diff --git a/templates/java/api_helpers.mustache b/templates/java/api_helpers.mustache new file mode 100644 index 0000000000..a330456e22 --- /dev/null +++ b/templates/java/api_helpers.mustache @@ -0,0 +1,357 @@ +{{#isSearchClient}} +/** + * Helper: Wait for a task to complete with `indexName` and `taskID`. + * + * @summary Wait for a task to complete. + * @param indexName The `indexName` where the operation was performed. + * @param taskID The `taskID` returned in the method response. + * @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 void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) { + TaskUtils.retryUntil(() -> { + return this.getTask(indexName, taskID, requestOptions); + }, (GetTaskResponse task) -> { + return task.getStatus() == TaskStatus.PUBLISHED; + }, maxRetries, timeout); +} + +/** + * Helper: Wait for a task to complete with `indexName` and `taskID`. + * + * @summary Wait for a task to complete. + * @param indexName The `indexName` where the operation was performed. + * @param taskID The `taskID` returned in the method response. + * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional) + */ +public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) { + this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions); +} + +/** + * Helper: Wait for a task to complete with `indexName` and `taskID`. + * + * @summary Wait for a task to complete. + * @param indexName The `indexName` where the operation was performed. + * @param taskID The `taskID` returned in the method response. + * @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 void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) { + this.waitForTask(indexName, taskID, maxRetries, timeout, null); +} + +/** + * Helper: Wait for a task to complete with `indexName` and `taskID`. + * + * @summary Wait for a task to complete. + * @param indexName The `indexName` where the operation was performed. + * @param taskID The `taskID` returned in the method response. + */ +public void waitForTask(String indexName, Long taskID) { + this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. + * @param key The `key` that has been added, deleted or updated. + * @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) + * @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, + int maxRetries, + IntUnaryOperator timeout, + RequestOptions requestOptions +) { + if (operation == ApiKeyOperation.UPDATE) { + if (apiKey == null) { + throw new AlgoliaRetryException("`apiKey` is required when waiting for an `update` operation."); + } + + // when updating an api key, we poll the api until we receive a different key + return TaskUtils.retryUntil( + () -> { + return this.getApiKey(key, requestOptions); + }, + (GetApiKeyResponse respKey) -> { + // we need to convert to an ApiKey object to use the `equals` method + ApiKey sameType = new ApiKey() + .setAcl(respKey.getAcl()) + .setDescription(respKey.getDescription()) + .setIndexes(respKey.getIndexes()) + .setMaxHitsPerQuery(respKey.getMaxHitsPerQuery()) + .setMaxQueriesPerIPPerHour(respKey.getMaxQueriesPerIPPerHour()) + .setQueryParameters(respKey.getQueryParameters()) + .setReferers(respKey.getReferers()) + .setValidity(respKey.getValidity()); + + return apiKey.equals(sameType); + }, + maxRetries, + timeout + ); + } + + // bypass lambda restriction to modify final object + final GetApiKeyResponse[] addedKey = new GetApiKeyResponse[] { null }; + + // check the status of the getApiKey method + TaskUtils.retryUntil( + () -> { + try { + addedKey[0] = this.getApiKey(key, requestOptions); + // magic number to signify we found the key + return -2; + } catch (AlgoliaApiException e) { + return e.getHttpErrorCode(); + } + }, + (Integer status) -> { + switch (operation) { + case ADD: + // stop either when the key is created or when we don't receive 404 + return status == -2 || status != 404; + case DELETE: + // stop when the key is not found + return status == 404; + default: + // continue + return false; + } + }, + maxRetries, + timeout + ); + + return addedKey[0]; +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) + * @param key The `key` that has been added, deleted or updated. + * @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, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) { + return this.waitForApiKey(operation, key, null, maxRetries, timeout, requestOptions); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. + * @param key The `key` that has been added, deleted or updated. + * @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); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) + * @param key The `key` that has been added, deleted or updated. + * @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); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. + * @param key The `key` that has been added, deleted or updated. + * @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); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) + * @param key The `key` that has been added, deleted or updated. + * @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); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. + * @param key The `key` that has been added, deleted or updated. + * @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); +} + +/** + * Helper: Wait for an API key to be added, updated or deleted based on a given `operation`. + * + * @summary Wait for an API key task to be processed. + * @param operation The `operation` that was done on a `key`. (ADD or DELETE only) + * @param key The `key` that has been added, deleted or updated. + */ +public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key) { + return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null); +} + +/** + * Helper: Returns an iterator on top of the `browse` method. + * + * @summary Returns an iterator on `browse`. + * @param indexName The index in which to perform the request. + * @param params The `browse` parameters. + * @param innerType The class held by the index, could be your custom class or {@link Object}. + * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional) + */ +public Iterable browseObjects(String indexName, BrowseParamsObject params, Class innerType, RequestOptions requestOptions) { + final Holder currentCursor = new Holder<>(); + + return AlgoliaIterableHelper.createIterable( + () -> { + BrowseResponse response = this.browse(indexName, BrowseParams.of(params), innerType, requestOptions); + params.setCursor(response.getCursor()); + currentCursor.value = response.getCursor(); + return response.getHits().iterator(); + }, + () -> { + return currentCursor.value != null; + } + ); +} + +/** + * Helper: Returns an iterator on top of the `browse` method. + * + * @summary Returns an iterator on `browse`. + * @param indexName The index in which to perform the request. + * @param params The `browse` parameters. + * @param innerType The class held by the index, could be your custom class or {@link Object}. + */ +public Iterable browseObjects(String indexName, BrowseParamsObject params, Class innerType) { + return browseObjects(indexName, params, innerType, null); +} + +/** + * Helper: Returns an iterator on top of the `searchSynonyms` method. + * + * @summary Returns an iterator on `searchSynonyms`. + * @param indexName The index in which to perform the request. + * @param type The synonym type. (optional) + * @param params The `searchSynonyms` parameters. (optional) + * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional) + */ +public Iterable browseSynonyms(String indexName, SynonymType type, SearchSynonymsParams params, RequestOptions requestOptions) { + final Holder currentPage = new Holder<>(0); + final int hitsPerPage = 1000; + + return AlgoliaIterableHelper.createIterable( + () -> { + SearchSynonymsResponse response = this.searchSynonyms(indexName, type, currentPage.value, hitsPerPage, params, requestOptions); + currentPage.value = response.getNbHits() < hitsPerPage ? null : currentPage.value + 1; + return response.getHits().iterator(); + }, + () -> { + return currentPage.value != null; + } + ); +} + +/** + * Helper: Returns an iterator on top of the `searchSynonyms` method. + * + * @summary Returns an iterator on `searchSynonyms`. + * @param indexName The index in which to perform the request. + * @param type The synonym type. (optional) + * @param params The `searchSynonyms` parameters .(optional) + */ +public Iterable browseSynonyms(String indexName, SynonymType type, SearchSynonymsParams params) { + return browseSynonyms(indexName, type, params, null); +} + + +/** + * Helper: Returns an iterator on top of the `searchSynonyms` method. + * + * @summary Returns an iterator on `searchSynonyms`. + * @param indexName The index in which to perform the request. + */ +public Iterable browseSynonyms(String indexName) { + return browseSynonyms(indexName, null, null, null); +} + +/** + * Helper: Returns an iterator on top of the `searchRules` method. + * + * @summary Returns an iterator on `searchRules`. + * @param indexName The index in which to perform the request. + * @param params The `searchRules` parameters. (optional) + * @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional) + */ +public Iterable browseRules(String indexName, SearchRulesParams params, RequestOptions requestOptions) { + final Holder currentPage = new Holder<>(0); + final int hitsPerPage = 1000; + params.setHitsPerPage(hitsPerPage); + + return AlgoliaIterableHelper.createIterable( + () -> { + SearchRulesResponse response = this.searchRules(indexName, params.setPage(currentPage.value), requestOptions); + currentPage.value = response.getNbHits() < hitsPerPage ? null : currentPage.value + 1; + return response.getHits().iterator(); + }, + () -> { + return currentPage.value != null; + } + ); +} + +/** + * Helper: Returns an iterator on top of the `searchRules` method. + * + * @summary Returns an iterator on `searchRules`. + * @param indexName The index in which to perform the request. + * @param params The `searchRules` parameters. (optional) + */ +public Iterable browseRules(String indexName, SearchRulesParams params) { + return browseRules(indexName, params, null); +} + +/** + * Helper: Returns an iterator on top of the `searchRules` method. + * + * @summary Returns an iterator on `searchRules`. + * @param indexName The index in which to perform the request. + */ +public Iterable browseRules(String indexName) { + return browseRules(indexName, null, null); +} +{{/isSearchClient}}