40
40
41
41
import java .util .ArrayList ;
42
42
import java .util .Collections ;
43
+ import java .util .HashMap ;
43
44
import java .util .List ;
45
+ import java .util .Map ;
44
46
import java .util .Objects ;
47
+ import java .util .stream .Collectors ;
45
48
46
49
/**
47
50
* The portion of an HTTP request to OpenSearch that can be
@@ -53,18 +56,21 @@ public final class RequestOptions {
53
56
*/
54
57
public static final RequestOptions DEFAULT = new Builder (
55
58
Collections .emptyList (),
59
+ Collections .emptyMap (),
56
60
HeapBufferedResponseConsumerFactory .DEFAULT ,
57
61
null ,
58
62
null
59
63
).build ();
60
64
61
65
private final List <Header > headers ;
66
+ private final Map <String , String > queryParams ;
62
67
private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory ;
63
68
private final WarningsHandler warningsHandler ;
64
69
private final RequestConfig requestConfig ;
65
70
66
71
private RequestOptions (Builder builder ) {
67
72
this .headers = Collections .unmodifiableList (new ArrayList <>(builder .headers ));
73
+ this .queryParams = Collections .unmodifiableMap (new HashMap <>(builder .queryParams ));
68
74
this .httpAsyncResponseConsumerFactory = builder .httpAsyncResponseConsumerFactory ;
69
75
this .warningsHandler = builder .warningsHandler ;
70
76
this .requestConfig = builder .requestConfig ;
@@ -74,7 +80,7 @@ private RequestOptions(Builder builder) {
74
80
* Create a builder that contains these options but can be modified.
75
81
*/
76
82
public Builder toBuilder () {
77
- return new Builder (headers , httpAsyncResponseConsumerFactory , warningsHandler , requestConfig );
83
+ return new Builder (headers , queryParams , httpAsyncResponseConsumerFactory , warningsHandler , requestConfig );
78
84
}
79
85
80
86
/**
@@ -84,6 +90,13 @@ public List<Header> getHeaders() {
84
90
return headers ;
85
91
}
86
92
93
+ /**
94
+ * Query parameters to attach to the request.
95
+ */
96
+ public Map <String , String > getQueryParams () {
97
+ return queryParams ;
98
+ }
99
+
87
100
/**
88
101
* The {@link HttpAsyncResponseConsumerFactory} used to create one
89
102
* {@link AsyncResponseConsumer} callback per retry. Controls how the
@@ -142,6 +155,12 @@ public String toString() {
142
155
b .append (headers .get (h ).toString ());
143
156
}
144
157
}
158
+ if (queryParams .size () > 0 ) {
159
+ if (comma ) b .append (", " );
160
+ comma = true ;
161
+ b .append ("queryParams=" );
162
+ b .append (queryParams .entrySet ().stream ().map (e -> e .getKey () + "=" + e .getValue ()).collect (Collectors .joining ("," )));
163
+ }
145
164
if (httpAsyncResponseConsumerFactory != HttpAsyncResponseConsumerFactory .DEFAULT ) {
146
165
if (comma ) b .append (", " );
147
166
comma = true ;
@@ -170,6 +189,7 @@ public boolean equals(Object obj) {
170
189
171
190
RequestOptions other = (RequestOptions ) obj ;
172
191
return headers .equals (other .headers )
192
+ && queryParams .equals (other .queryParams )
173
193
&& httpAsyncResponseConsumerFactory .equals (other .httpAsyncResponseConsumerFactory )
174
194
&& Objects .equals (warningsHandler , other .warningsHandler );
175
195
}
@@ -179,7 +199,7 @@ public boolean equals(Object obj) {
179
199
*/
180
200
@ Override
181
201
public int hashCode () {
182
- return Objects .hash (headers , httpAsyncResponseConsumerFactory , warningsHandler );
202
+ return Objects .hash (headers , queryParams , httpAsyncResponseConsumerFactory , warningsHandler );
183
203
}
184
204
185
205
/**
@@ -189,17 +209,20 @@ public int hashCode() {
189
209
*/
190
210
public static class Builder {
191
211
private final List <Header > headers ;
212
+ private final Map <String , String > queryParams ;
192
213
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory ;
193
214
private WarningsHandler warningsHandler ;
194
215
private RequestConfig requestConfig ;
195
216
196
217
private Builder (
197
218
List <Header > headers ,
219
+ Map <String , String > queryParams ,
198
220
HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory ,
199
221
WarningsHandler warningsHandler ,
200
222
RequestConfig requestConfig
201
223
) {
202
224
this .headers = new ArrayList <>(headers );
225
+ this .queryParams = new HashMap <>();
203
226
this .httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory ;
204
227
this .warningsHandler = warningsHandler ;
205
228
this .requestConfig = requestConfig ;
@@ -226,6 +249,20 @@ public Builder addHeader(String name, String value) {
226
249
return this ;
227
250
}
228
251
252
+ /**
253
+ * Add the provided query parameter to the request.
254
+ *
255
+ * @param name the query parameter name
256
+ * @param value the query parameter value
257
+ * @throws NullPointerException if {@code name} or {@code value} is null.
258
+ */
259
+ public Builder addQueryParam (String name , String value ) {
260
+ Objects .requireNonNull (name , "query parameter name cannot be null" );
261
+ Objects .requireNonNull (value , "query parameter value cannot be null" );
262
+ this .queryParams .put (name , value );
263
+ return this ;
264
+ }
265
+
229
266
/**
230
267
* Set the {@link HttpAsyncResponseConsumerFactory} used to create one
231
268
* {@link AsyncResponseConsumer} callback per retry. Controls how the
0 commit comments