24
24
package com .algolia .search .saas .helpers ;
25
25
26
26
import android .support .annotation .NonNull ;
27
+ import android .support .annotation .Nullable ;
27
28
28
29
import com .algolia .search .saas .AlgoliaException ;
29
30
import com .algolia .search .saas .CompletionHandler ;
30
31
import com .algolia .search .saas .Index ;
31
32
import com .algolia .search .saas .Query ;
32
33
import com .algolia .search .saas .Request ;
34
+ import com .algolia .search .saas .RequestOptions ;
33
35
34
36
import org .json .JSONObject ;
35
37
36
38
/**
37
39
* Iterator to browse all index content.
38
- *
40
+ * <p>
39
41
* This helper takes care of chaining API requests and calling back the handler block with the results, until:
40
42
* - the end of the index has been reached;
41
43
* - an error has been encountered;
@@ -51,8 +53,8 @@ public interface BrowseIteratorHandler {
51
53
* Called at each batch of results.
52
54
*
53
55
* @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).
56
58
*/
57
59
public void handleBatch (@ NonNull BrowseIterator iterator , JSONObject result , AlgoliaException error );
58
60
}
@@ -66,6 +68,9 @@ public interface BrowseIteratorHandler {
66
68
/** Listener. */
67
69
private BrowseIteratorHandler handler ;
68
70
71
+ /** Eventual request-specific options */
72
+ private @ Nullable RequestOptions requestOptions ;
73
+
69
74
/** Cursor to use for the next call, if any. */
70
75
private String cursor ;
71
76
@@ -82,14 +87,28 @@ public interface BrowseIteratorHandler {
82
87
* Construct a new browse iterator.
83
88
* NOTE: The iteration does not start automatically. You have to call `start()` explicitly.
84
89
*
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.
87
92
* @param handler Handler called for each batch of results.
88
93
*/
89
94
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 ) {
90
108
this .index = index ;
91
109
this .query = query ;
92
110
this .handler = handler ;
111
+ this .requestOptions = requestOptions ;
93
112
}
94
113
95
114
/**
@@ -100,7 +119,7 @@ public void start() {
100
119
throw new IllegalStateException ();
101
120
}
102
121
started = true ;
103
- request = index .browseAsync (query , completionHandler );
122
+ request = index .browseAsync (query , requestOptions , completionHandler );
104
123
}
105
124
106
125
/**
@@ -109,8 +128,9 @@ public void start() {
109
128
* The listener will not be called after the iteration has been cancelled.
110
129
*/
111
130
public void cancel () {
112
- if (cancelled )
131
+ if (cancelled ) {
113
132
return ;
133
+ }
114
134
request .cancel ();
115
135
request = null ;
116
136
cancelled = true ;
@@ -131,7 +151,7 @@ private void next() {
131
151
if (!hasNext ()) {
132
152
throw new IllegalStateException ();
133
153
}
134
- request = index .browseFromAsync (cursor , completionHandler );
154
+ request = index .browseFromAsync (cursor , requestOptions , completionHandler );
135
155
}
136
156
137
157
private CompletionHandler completionHandler = new CompletionHandler () {
0 commit comments