Skip to content

Commit 408f44a

Browse files
committed
feat(java): browse objects/synonyms/rules
1 parent c576b29 commit 408f44a

File tree

7 files changed

+332
-239
lines changed

7 files changed

+332
-239
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.algolia.utils;
2+
3+
import java.util.Iterator;
4+
import java.util.function.BooleanSupplier;
5+
import java.util.function.Supplier;
6+
7+
public class AlgoliaIterableHelper {
8+
9+
public static <T> Iterable<T> createIterable(Supplier<Iterator<T>> executeQuery, BooleanSupplier _hasNext) {
10+
return new Iterable<T>() {
11+
@Override
12+
public Iterator<T> iterator() {
13+
return new Iterator<T>() {
14+
private boolean isFirstRequest = true;
15+
private Iterator<T> currentIterator = null;
16+
17+
@Override
18+
public boolean hasNext() {
19+
if(isFirstRequest || (_hasNext.getAsBoolean() && !currentIterator.hasNext())) {
20+
currentIterator = executeQuery.get();
21+
isFirstRequest = false;
22+
}
23+
return currentIterator != null && currentIterator.hasNext();
24+
}
25+
26+
@Override
27+
public T next() {
28+
if(currentIterator == null || !currentIterator.hasNext()) {
29+
currentIterator = executeQuery.get();
30+
isFirstRequest = false;
31+
}
32+
return currentIterator.next();
33+
}
34+
};
35+
}
36+
};
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.algolia.utils;
2+
3+
public class Holder<T> {
4+
5+
public T value;
6+
7+
public Holder() {
8+
this.value = null;
9+
}
10+
11+
public Holder(T value) {
12+
this.value = value;
13+
}
14+
}

playground/java/src/main/java/com/algolia/playground/Search.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static void main(String[] args) {
6464
SearchResponses<Actor> sr = result.get();
6565
Actor a = sr.getResults().get(0).getHits().get(0);
6666
System.out.println(a.name);
67+
6768
} catch (InterruptedException e) {
6869
System.err.println("InterrupedException" + e.getMessage());
6970
e.printStackTrace();

specs/search/paths/synonyms/common/schemas.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ synonymHit:
3939
items:
4040
type: string
4141
description: List of query words that will match the token.
42-
_highlightResult:
43-
type: object
44-
description: Highlighted results.
45-
additionalProperties: false
46-
properties:
47-
type:
48-
$ref: '../../../common/schemas/Hit.yml#/highlightResultMap'
49-
synonyms:
50-
type: array
51-
items:
52-
$ref: '../../../common/schemas/Hit.yml#/highlightResultMap'
5342
required:
5443
- objectID
5544
- type

specs/search/paths/synonyms/searchSynonyms.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ post:
88
description: Search or browse all synonyms, optionally filtering them by type.
99
parameters:
1010
- $ref: '../../../common/parameters.yml#/IndexName'
11-
- $ref: 'common/parameters.yml#/Type'
11+
- $ref: './common/parameters.yml#/Type'
1212
- $ref: '../../../common/parameters.yml#/PageDefault0'
1313
- $ref: '../../../common/parameters.yml#/HitsPerPage'
1414
requestBody:
@@ -28,7 +28,7 @@ post:
2828
content:
2929
application/json:
3030
schema:
31-
$ref: 'common/schemas.yml#/searchSynonymsResponse'
31+
$ref: './common/schemas.yml#/searchSynonymsResponse'
3232
'400':
3333
$ref: '../../../common/responses/BadRequest.yml'
3434
'402':

templates/java/api.mustache

Lines changed: 1 addition & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -261,231 +261,6 @@ public class {{classname}} extends ApiClient {
261261
{{/optionalParams.0}}
262262
{{/operation}}
263263

264-
{{#isSearchClient}}
265-
/**
266-
* Helper: Wait for a task to complete with `indexName` and `taskID`.
267-
*
268-
* @summary Wait for a task to complete.
269-
* @param indexName The `indexName` where the operation was performed.
270-
* @param taskID The `taskID` returned in the method response.
271-
* @param maxRetries The maximum number of retry. 50 by default. (optional)
272-
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
273-
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
274-
*/
275-
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
276-
TaskUtils.retryUntil(() -> {
277-
return this.getTask(indexName, taskID, requestOptions);
278-
}, (GetTaskResponse task) -> {
279-
return task.getStatus() == TaskStatus.PUBLISHED;
280-
}, maxRetries, timeout);
281-
}
282-
283-
/**
284-
* Helper: Wait for a task to complete with `indexName` and `taskID`.
285-
*
286-
* @summary Wait for a task to complete.
287-
* @param indexName The `indexName` where the operation was performed.
288-
* @param taskID The `taskID` returned in the method response.
289-
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
290-
*/
291-
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
292-
this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
293-
}
294-
295-
/**
296-
* Helper: Wait for a task to complete with `indexName` and `taskID`.
297-
*
298-
* @summary Wait for a task to complete.
299-
* @param indexName The `indexName` where the operation was performed.
300-
* @param taskID The `taskID` returned in the method response.
301-
* @param maxRetries The maximum number of retry. 50 by default. (optional)
302-
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
303-
*/
304-
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) {
305-
this.waitForTask(indexName, taskID, maxRetries, timeout, null);
306-
}
307-
308-
/**
309-
* Helper: Wait for a task to complete with `indexName` and `taskID`.
310-
*
311-
* @summary Wait for a task to complete.
312-
* @param indexName The `indexName` where the operation was performed.
313-
* @param taskID The `taskID` returned in the method response.
314-
*/
315-
public void waitForTask(String indexName, Long taskID) {
316-
this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
317-
}
318-
319-
/**
320-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
321-
*
322-
* @summary Wait for an API key task to be processed.
323-
* @param operation The `operation` that was done on a `key`.
324-
* @param key The `key` that has been added, deleted or updated.
325-
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
326-
* @param maxRetries The maximum number of retry. 50 by default. (optional)
327-
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
328-
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
329-
*/
330-
public GetApiKeyResponse waitForApiKey(
331-
ApiKeyOperation operation,
332-
String key,
333-
ApiKey apiKey,
334-
int maxRetries,
335-
IntUnaryOperator timeout,
336-
RequestOptions requestOptions
337-
) {
338-
if (operation == ApiKeyOperation.UPDATE) {
339-
if (apiKey == null) {
340-
throw new AlgoliaRetryException("`apiKey` is required when waiting for an `update` operation.");
341-
}
342-
343-
// when updating an api key, we poll the api until we receive a different key
344-
return TaskUtils.retryUntil(
345-
() -> {
346-
return this.getApiKey(key, requestOptions);
347-
},
348-
(GetApiKeyResponse respKey) -> {
349-
// we need to convert to an ApiKey object to use the `equals` method
350-
ApiKey sameType = new ApiKey()
351-
.setAcl(respKey.getAcl())
352-
.setDescription(respKey.getDescription())
353-
.setIndexes(respKey.getIndexes())
354-
.setMaxHitsPerQuery(respKey.getMaxHitsPerQuery())
355-
.setMaxQueriesPerIPPerHour(respKey.getMaxQueriesPerIPPerHour())
356-
.setQueryParameters(respKey.getQueryParameters())
357-
.setReferers(respKey.getReferers())
358-
.setValidity(respKey.getValidity());
359-
360-
return apiKey.equals(sameType);
361-
},
362-
maxRetries,
363-
timeout
364-
);
365-
}
366-
367-
// bypass lambda restriction to modify final object
368-
final GetApiKeyResponse[] addedKey = new GetApiKeyResponse[] { null };
369-
370-
// check the status of the getApiKey method
371-
TaskUtils.retryUntil(
372-
() -> {
373-
try {
374-
addedKey[0] = this.getApiKey(key, requestOptions);
375-
// magic number to signify we found the key
376-
return -2;
377-
} catch (AlgoliaApiException e) {
378-
return e.getHttpErrorCode();
379-
}
380-
},
381-
(Integer status) -> {
382-
switch (operation) {
383-
case ADD:
384-
// stop either when the key is created or when we don't receive 404
385-
return status == -2 || status != 404;
386-
case DELETE:
387-
// stop when the key is not found
388-
return status == 404;
389-
default:
390-
// continue
391-
return false;
392-
}
393-
},
394-
maxRetries,
395-
timeout
396-
);
397-
398-
return addedKey[0];
399-
}
400-
401-
/**
402-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
403-
*
404-
* @summary Wait for an API key task to be processed.
405-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
406-
* @param key The `key` that has been added, deleted or updated.
407-
* @param maxRetries The maximum number of retry. 50 by default. (optional)
408-
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
409-
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
410-
*/
411-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
412-
return this.waitForApiKey(operation, key, null, maxRetries, timeout, requestOptions);
413-
}
414-
415-
/**
416-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
417-
*
418-
* @summary Wait for an API key task to be processed.
419-
* @param operation The `operation` that was done on a `key`.
420-
* @param key The `key` that has been added, deleted or updated.
421-
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
422-
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
423-
*/
424-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, RequestOptions requestOptions) {
425-
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
426-
}
427-
428-
/**
429-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
430-
*
431-
* @summary Wait for an API key task to be processed.
432-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
433-
* @param key The `key` that has been added, deleted or updated.
434-
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
435-
*/
436-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, RequestOptions requestOptions) {
437-
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
438-
}
439-
440-
/**
441-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
442-
*
443-
* @summary Wait for an API key task to be processed.
444-
* @param operation The `operation` that was done on a `key`.
445-
* @param key The `key` that has been added, deleted or updated.
446-
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
447-
* @param maxRetries The maximum number of retry. 50 by default. (optional)
448-
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
449-
*/
450-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, int maxRetries, IntUnaryOperator timeout) {
451-
return this.waitForApiKey(operation, key, apiKey, maxRetries, timeout, null);
452-
}
453-
454-
/**
455-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
456-
*
457-
* @summary Wait for an API key task to be processed.
458-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
459-
* @param key The `key` that has been added, deleted or updated.
460-
* @param maxRetries The maximum number of retry. 50 by default. (optional)
461-
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
462-
*/
463-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout) {
464-
return this.waitForApiKey(operation, key, null, maxRetries, timeout, null);
465-
}
466-
467-
/**
468-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
469-
*
470-
* @summary Wait for an API key task to be processed.
471-
* @param operation The `operation` that was done on a `key`.
472-
* @param key The `key` that has been added, deleted or updated.
473-
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
474-
*/
475-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey) {
476-
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
477-
}
478-
479-
/**
480-
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
481-
*
482-
* @summary Wait for an API key task to be processed.
483-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
484-
* @param key The `key` that has been added, deleted or updated.
485-
*/
486-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key) {
487-
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
488-
}
489-
{{/isSearchClient}}
264+
{{> api_helpers}}
490265
}
491266
{{/operations}}

0 commit comments

Comments
 (0)