Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit e081acd

Browse files
authored
fix(Index): missing request options methods (#458)
* fix(Index): Implement getObjectsAsync(objectIDs) with RequestOptions * fix(Index): Implement deleteBy with RequestOptions * fix(BrowseController): Add missing RequestOptions
1 parent f5cf4f8 commit e081acd

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/Index.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,23 @@ public Request getObjectsAsync(final @NonNull List<String> objectIDs, final List
630630
return getObjectsAsync(objectIDs, attributesToRetrieve, /* requestOptions: */ null, completionHandler);
631631
}
632632

633+
/**
634+
* Get several objects from this index (asynchronously), optionally restricting the retrieved content (asynchronously).
635+
*
636+
* @param objectIDs Identifiers of objects to retrieve.
637+
* @param requestOptions Request-specific options.
638+
* @param completionHandler The listener that will be notified of the request's outcome.
639+
* @return A cancellable request.
640+
*/
641+
public Request getObjectsAsync(final @NonNull List<String> objectIDs, @Nullable final RequestOptions requestOptions, @Nullable CompletionHandler completionHandler) {
642+
return getClient().new AsyncTaskRequest(completionHandler) {
643+
@NonNull
644+
@Override protected JSONObject run() throws AlgoliaException {
645+
return getObjects(objectIDs, null, requestOptions);
646+
}
647+
}.start();
648+
}
649+
633650
/**
634651
* Get several objects from this index (asynchronously), optionally restricting the retrieved content (asynchronously).
635652
*
@@ -777,16 +794,27 @@ public Request deleteByQueryAsync(@NonNull Query query, @Nullable final RequestO
777794
* @param completionHandler The listener that will be notified of the request's outcome.
778795
* @return A cancellable request.
779796
*/
780-
public Request deleteByAsync(@NonNull Query query, CompletionHandler completionHandler) {
797+
public Request deleteByAsync(@NonNull Query query, @Nullable final RequestOptions requestOptions, @Nullable CompletionHandler completionHandler) {
781798
final Query queryCopy = new Query(query);
782799
return getClient().new AsyncTaskRequest(completionHandler) {
783800
@NonNull
784801
@Override protected JSONObject run() throws AlgoliaException {
785-
return deleteBy(queryCopy);
802+
return deleteBy(queryCopy, requestOptions);
786803
}
787804
}.start();
788805
}
789806

807+
/**
808+
* Delete all objects matching a query (helper).
809+
*
810+
* @param query The query that objects to delete must match.
811+
* @param completionHandler The listener that will be notified of the request's outcome.
812+
* @return A cancellable request.
813+
*/
814+
public Request deleteByAsync(@NonNull Query query, @Nullable CompletionHandler completionHandler) {
815+
return deleteByAsync(query, null, completionHandler);
816+
}
817+
790818
/**
791819
* Get this index's settings (asynchronously).
792820
*
@@ -1293,8 +1321,19 @@ protected void deleteByQuery(@NonNull Query query, @Nullable RequestOptions requ
12931321
* @throws AlgoliaException
12941322
*/
12951323
protected JSONObject deleteBy(@NonNull Query query) throws AlgoliaException {
1324+
return deleteBy(query, null);
1325+
}
1326+
1327+
/**
1328+
* Delete all records matching the query.
1329+
*
1330+
* @param query the query string
1331+
* @throws AlgoliaException
1332+
*
1333+
*/
1334+
protected JSONObject deleteBy(@NonNull Query query, RequestOptions requestOptions) throws AlgoliaException {
12961335
try {
1297-
return client.postRequest("/1/indexes/" + encodedIndexName + "/deleteByQuery", query.getParameters(), new JSONObject().put("params", query.build()).toString(), false, /* requestOptions: */ null);
1336+
return client.postRequest("/1/indexes/" + encodedIndexName + "/deleteByQuery", query.getParameters(), new JSONObject().put("params", query.build()).toString(), false, requestOptions);
12981337
} catch (JSONException e) {
12991338
e.printStackTrace();
13001339
return null;

algoliasearch/src/main/java/com/algolia/search/saas/helpers/BrowseIterator.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@
2424
package com.algolia.search.saas.helpers;
2525

2626
import android.support.annotation.NonNull;
27+
import android.support.annotation.Nullable;
2728

2829
import com.algolia.search.saas.AlgoliaException;
2930
import com.algolia.search.saas.CompletionHandler;
3031
import com.algolia.search.saas.Index;
3132
import com.algolia.search.saas.Query;
3233
import com.algolia.search.saas.Request;
34+
import com.algolia.search.saas.RequestOptions;
3335

3436
import org.json.JSONObject;
3537

3638
/**
3739
* Iterator to browse all index content.
38-
*
40+
* <p>
3941
* This helper takes care of chaining API requests and calling back the handler block with the results, until:
4042
* - the end of the index has been reached;
4143
* - an error has been encountered;
@@ -51,8 +53,8 @@ public interface BrowseIteratorHandler {
5153
* Called at each batch of results.
5254
*
5355
* @param iterator The iterator where the results originate from.
54-
* @param result The results (in case of success).
55-
* @param error The error (in case of error).
56+
* @param result The results (in case of success).
57+
* @param error The error (in case of error).
5658
*/
5759
public void handleBatch(@NonNull BrowseIterator iterator, JSONObject result, AlgoliaException error);
5860
}
@@ -66,6 +68,9 @@ public interface BrowseIteratorHandler {
6668
/** Listener. */
6769
private BrowseIteratorHandler handler;
6870

71+
/** Eventual request-specific options */
72+
private @Nullable RequestOptions requestOptions;
73+
6974
/** Cursor to use for the next call, if any. */
7075
private String cursor;
7176

@@ -82,14 +87,28 @@ public interface BrowseIteratorHandler {
8287
* Construct a new browse iterator.
8388
* NOTE: The iteration does not start automatically. You have to call `start()` explicitly.
8489
*
85-
* @param index The index to be browsed.
86-
* @param query The query used to filter the results.
90+
* @param index The index to be browsed.
91+
* @param query The query used to filter the results.
8792
* @param handler Handler called for each batch of results.
8893
*/
8994
public BrowseIterator(@NonNull Index index, @NonNull Query query, @NonNull BrowseIteratorHandler handler) {
95+
this(index, query, null, handler);
96+
}
97+
98+
/**
99+
* Construct a new browse iterator.
100+
* NOTE: The iteration does not start automatically. You have to call `start()` explicitly.
101+
*
102+
* @param index The index to be browsed.
103+
* @param query The query used to filter the results.
104+
* @param requestOptions Request-specific options.
105+
* @param handler Handler called for each batch of results.
106+
*/
107+
public BrowseIterator(@NonNull Index index, @NonNull Query query, @Nullable RequestOptions requestOptions, @NonNull BrowseIteratorHandler handler) {
90108
this.index = index;
91109
this.query = query;
92110
this.handler = handler;
111+
this.requestOptions = requestOptions;
93112
}
94113

95114
/**
@@ -100,7 +119,7 @@ public void start() {
100119
throw new IllegalStateException();
101120
}
102121
started = true;
103-
request = index.browseAsync(query, completionHandler);
122+
request = index.browseAsync(query, requestOptions, completionHandler);
104123
}
105124

106125
/**
@@ -109,8 +128,9 @@ public void start() {
109128
* The listener will not be called after the iteration has been cancelled.
110129
*/
111130
public void cancel() {
112-
if (cancelled)
131+
if (cancelled) {
113132
return;
133+
}
114134
request.cancel();
115135
request = null;
116136
cancelled = true;
@@ -131,7 +151,7 @@ private void next() {
131151
if (!hasNext()) {
132152
throw new IllegalStateException();
133153
}
134-
request = index.browseFromAsync(cursor, completionHandler);
154+
request = index.browseFromAsync(cursor, requestOptions, completionHandler);
135155
}
136156

137157
private CompletionHandler completionHandler = new CompletionHandler() {

0 commit comments

Comments
 (0)