@@ -398,28 +398,28 @@ private enum Method {
398
398
GET , POST , PUT , DELETE
399
399
}
400
400
401
- protected byte [] getRequestRaw (String url , boolean search ) throws AlgoliaException {
402
- return _requestRaw (Method .GET , url , null , getReadHostsThatAreUp (), connectTimeout , search ? searchTimeout : readTimeout );
401
+ protected byte [] getRequestRaw (@ NonNull String url , @ Nullable Map < String , String > urlParameters , boolean search , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
402
+ return _requestRaw (Method .GET , url , urlParameters , /* json: */ null , getReadHostsThatAreUp (), connectTimeout , search ? searchTimeout : readTimeout , requestOptions );
403
403
}
404
404
405
- protected JSONObject getRequest (String url , boolean search ) throws AlgoliaException {
406
- return _request (Method .GET , url , null , getReadHostsThatAreUp (), connectTimeout , search ? searchTimeout : readTimeout );
405
+ protected JSONObject getRequest (@ NonNull String url , @ Nullable Map < String , String > urlParameters , boolean search , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
406
+ return _request (Method .GET , url , urlParameters , /* json: */ null , getReadHostsThatAreUp (), connectTimeout , search ? searchTimeout : readTimeout , requestOptions );
407
407
}
408
408
409
- protected JSONObject deleteRequest (String url ) throws AlgoliaException {
410
- return _request (Method .DELETE , url , null , getWriteHostsThatAreUp (), connectTimeout , readTimeout );
409
+ protected JSONObject deleteRequest (@ NonNull String url , @ Nullable Map < String , String > urlParameters , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
410
+ return _request (Method .DELETE , url , urlParameters , /* json: */ null , getWriteHostsThatAreUp (), connectTimeout , readTimeout , requestOptions );
411
411
}
412
412
413
- protected JSONObject postRequest (String url , String obj , boolean readOperation ) throws AlgoliaException {
414
- return _request (Method .POST , url , obj , (readOperation ? getReadHostsThatAreUp () : getWriteHostsThatAreUp ()), connectTimeout , (readOperation ? searchTimeout : readTimeout ));
413
+ protected JSONObject postRequest (@ NonNull String url , @ Nullable Map < String , String > urlParameters , @ Nullable String obj , boolean readOperation , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
414
+ return _request (Method .POST , url , urlParameters , obj , (readOperation ? getReadHostsThatAreUp () : getWriteHostsThatAreUp ()), connectTimeout , (readOperation ? searchTimeout : readTimeout ), requestOptions );
415
415
}
416
416
417
- protected byte [] postRequestRaw (String url , String obj , boolean readOperation ) throws AlgoliaException {
418
- return _requestRaw (Method .POST , url , obj , (readOperation ? getReadHostsThatAreUp () : getWriteHostsThatAreUp ()), connectTimeout , (readOperation ? searchTimeout : readTimeout ));
417
+ protected byte [] postRequestRaw (@ NonNull String url , @ Nullable Map < String , String > urlParameters , @ Nullable String obj , boolean readOperation , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
418
+ return _requestRaw (Method .POST , url , urlParameters , obj , (readOperation ? getReadHostsThatAreUp () : getWriteHostsThatAreUp ()), connectTimeout , (readOperation ? searchTimeout : readTimeout ), requestOptions );
419
419
}
420
420
421
- protected JSONObject putRequest (String url , String obj ) throws AlgoliaException {
422
- return _request (Method .PUT , url , obj , getWriteHostsThatAreUp (), connectTimeout , readTimeout );
421
+ protected JSONObject putRequest (@ NonNull String url , @ Nullable Map < String , String > urlParameters , @ NonNull String obj , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
422
+ return _request (Method .PUT , url , urlParameters , obj , getWriteHostsThatAreUp (), connectTimeout , readTimeout , requestOptions );
423
423
}
424
424
425
425
/**
@@ -482,17 +482,18 @@ private static JSONObject _getAnswerJSONObject(InputStream istream) throws IOExc
482
482
* Send the query according to parameters and returns its result as a JSONObject
483
483
*
484
484
* @param m HTTP Method to use
485
- * @param url endpoint URL
485
+ * @param url Endpoint URL, *without query string*. The query string is handled by `urlParameters`.
486
+ * @param urlParameters URL parameters
486
487
* @param json optional JSON Object to send
487
488
* @param hostsArray array of hosts to try successively
488
489
* @param connectTimeout maximum wait time to open connection
489
490
* @param readTimeout maximum time to read data on socket
490
491
* @return a JSONObject containing the resulting data or error
491
492
* @throws AlgoliaException if the request data is not valid json
492
493
*/
493
- private JSONObject _request (Method m , String url , String json , List <String > hostsArray , int connectTimeout , int readTimeout ) throws AlgoliaException {
494
+ private JSONObject _request (@ NonNull Method m , @ NonNull String url , @ Nullable Map < String , String > urlParameters , @ Nullable String json , @ NonNull List <String > hostsArray , int connectTimeout , int readTimeout , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
494
495
try {
495
- return _getJSONObject (_requestRaw (m , url , json , hostsArray , connectTimeout , readTimeout ));
496
+ return _getJSONObject (_requestRaw (m , url , urlParameters , json , hostsArray , connectTimeout , readTimeout , requestOptions ));
496
497
} catch (JSONException e ) {
497
498
throw new AlgoliaException ("JSON decode error:" + e .getMessage ());
498
499
} catch (UnsupportedEncodingException e ) {
@@ -504,15 +505,16 @@ private JSONObject _request(Method m, String url, String json, List<String> host
504
505
* Send the query according to parameters and returns its result as a JSONObject
505
506
*
506
507
* @param m HTTP Method to use
507
- * @param url endpoint URL
508
- * @param json optional JSON Object to send
508
+ * @param url Endpoint URL, *without query string*. The query string is handled by `urlParameters`.
509
+ * @param urlParameters URL parameters
510
+ * @param json (optional) JSON body
509
511
* @param hostsArray array of hosts to try successively
510
512
* @param connectTimeout maximum wait time to open connection
511
513
* @param readTimeout maximum time to read data on socket
512
514
* @return a JSONObject containing the resulting data or error
513
515
* @throws AlgoliaException in case of connection or data handling error
514
516
*/
515
- private byte [] _requestRaw (Method m , String url , String json , List <String > hostsArray , int connectTimeout , int readTimeout ) throws AlgoliaException {
517
+ private byte [] _requestRaw (@ NonNull Method m , @ NonNull String url , @ Nullable Map < String , String > urlParameters , @ Nullable String json , @ NonNull List <String > hostsArray , int connectTimeout , int readTimeout , @ Nullable RequestOptions requestOptions ) throws AlgoliaException {
516
518
String requestMethod ;
517
519
List <Exception > errors = new ArrayList <>(hostsArray .size ());
518
520
// for each host
@@ -536,17 +538,32 @@ private byte[] _requestRaw(Method m, String url, String json, List<String> hosts
536
538
537
539
InputStream stream = null ;
538
540
HttpURLConnection hostConnection = null ;
539
- // set URL
540
541
try {
541
- URL hostURL = new URL ("https://" + host + url );
542
+ // Compute final URL parameters.
543
+ final Map <String , String > parameters = new HashMap <>();
544
+ if (urlParameters != null ) {
545
+ parameters .putAll (urlParameters );
546
+ }
547
+ if (requestOptions != null ) {
548
+ parameters .putAll (requestOptions .urlParameters );
549
+ }
550
+
551
+ // Build URL.
552
+ String urlString = "https://" + host + url ;
553
+ if (!parameters .isEmpty ()) {
554
+ urlString += "?" + AbstractQuery .build (parameters );
555
+ }
556
+ URL hostURL = new URL (urlString );
557
+
558
+ // Open connection.
542
559
hostConnection = (HttpURLConnection ) hostURL .openConnection ();
543
560
544
561
//set timeouts
545
562
hostConnection .setRequestMethod (requestMethod );
546
563
hostConnection .setConnectTimeout (connectTimeout );
547
564
hostConnection .setReadTimeout (readTimeout );
548
565
549
- // set auth headers
566
+ // Headers
550
567
hostConnection .setRequestProperty ("X-Algolia-Application-Id" , this .applicationID );
551
568
// If API key is too big, send it in the request's body (if applicable).
552
569
if (this .apiKey != null && this .apiKey .length () > MAX_API_KEY_LENGTH && json != null ) {
@@ -560,9 +577,16 @@ private byte[] _requestRaw(Method m, String url, String json, List<String> hosts
560
577
} else {
561
578
hostConnection .setRequestProperty ("X-Algolia-API-Key" , this .apiKey );
562
579
}
580
+ // Client-level headers
563
581
for (Map .Entry <String , String > entry : this .headers .entrySet ()) {
564
582
hostConnection .setRequestProperty (entry .getKey (), entry .getValue ());
565
583
}
584
+ // Request-level headers
585
+ if (requestOptions != null ) {
586
+ for (Map .Entry <String , String > entry : requestOptions .headers .entrySet ()) {
587
+ hostConnection .setRequestProperty (entry .getKey (), entry .getValue ());
588
+ }
589
+ }
566
590
567
591
// set user agent
568
592
hostConnection .setRequestProperty ("User-Agent" , userAgentRaw );
0 commit comments