2
2
3
3
import com .algolia .exceptions .*;
4
4
import com .algolia .utils .JSON ;
5
+ import com .algolia .utils .RequestOptions ;
5
6
import com .algolia .utils .Requester ;
6
7
import com .algolia .utils .UserAgent ;
7
8
import java .io .IOException ;
@@ -22,7 +23,7 @@ public class ApiClient {
22
23
private boolean debugging = false ;
23
24
private Map <String , String > defaultHeaderMap = new HashMap <String , String >();
24
25
25
- private String appId , apiKey ;
26
+ private String contentType ;
26
27
27
28
private DateFormat dateFormat ;
28
29
@@ -38,6 +39,8 @@ public ApiClient(
38
39
String clientName ,
39
40
UserAgent .Segment [] segments
40
41
) {
42
+ this .contentType = "application/json" ;
43
+
41
44
UserAgent ua = new UserAgent ("0.0.1" );
42
45
ua .addSegment (new UserAgent .Segment (clientName , "0.0.1" ));
43
46
if (segments != null ) {
@@ -47,8 +50,11 @@ public ApiClient(
47
50
}
48
51
setUserAgent (ua .toString ());
49
52
50
- this .appId = appId ;
51
- this .apiKey = apiKey ;
53
+ defaultHeaderMap .put ("X-Algolia-Application-Id" , appId );
54
+ defaultHeaderMap .put ("X-Algolia-API-Key" , apiKey );
55
+ defaultHeaderMap .put ("Accept" , this .contentType );
56
+ defaultHeaderMap .put ("Content-Type" , this .contentType );
57
+
52
58
this .requester = requester ;
53
59
}
54
60
@@ -190,19 +196,6 @@ public String parameterToString(Object param) {
190
196
}
191
197
}
192
198
193
- /**
194
- * Check if the given MIME is a JSON MIME. JSON MIME examples: application/json application/json;
195
- * charset=UTF8 APPLICATION/JSON application/vnd.company+json "* / *" is also default to JSON
196
- *
197
- * @param mime MIME (Multipurpose Internet Mail Extensions)
198
- * @return True if the given MIME is JSON, false otherwise.
199
- */
200
- public static boolean isJsonMime (String mime ) {
201
- String jsonMime =
202
- "(?i)^(application/json|[^;/ \t ]+/[^;/ \t ]+[+]json)[ \t ]*(;.*)?$" ;
203
- return mime != null && (mime .matches (jsonMime ) || mime .equals ("*/*" ));
204
- }
205
-
206
199
/**
207
200
* Escape the given string to be used as URL query value.
208
201
*
@@ -222,28 +215,19 @@ public String escapeString(String str) {
222
215
* request Content-Type.
223
216
*
224
217
* @param obj The Java object
225
- * @param contentType The request Content-Type
226
218
* @return The serialized request body
227
219
* @throws AlgoliaRuntimeException If fail to serialize the given object
228
220
*/
229
- public RequestBody serialize (Object obj , String contentType )
230
- throws AlgoliaRuntimeException {
231
- if (obj instanceof byte []) {
232
- // Binary (byte array) body parameter support.
233
- return RequestBody .create ((byte []) obj , MediaType .parse (contentType ));
234
- } else if (isJsonMime (contentType )) {
235
- String content ;
236
- if (obj != null ) {
237
- content = JSON .serialize (obj );
238
- } else {
239
- content = null ;
240
- }
241
- return RequestBody .create (content , MediaType .parse (contentType ));
221
+ public RequestBody serialize (Object obj ) throws AlgoliaRuntimeException {
222
+ String content ;
223
+
224
+ if (obj != null ) {
225
+ content = JSON .serialize (obj );
242
226
} else {
243
- throw new AlgoliaRuntimeException (
244
- "Content type \" " + contentType + "\" is not supported"
245
- );
227
+ content = null ;
246
228
}
229
+
230
+ return RequestBody .create (content , MediaType .parse (this .contentType ));
247
231
}
248
232
249
233
/**
@@ -291,6 +275,8 @@ public void onResponse(Call call, Response response)
291
275
* @param queryParams The query parameters
292
276
* @param body The request body object
293
277
* @param headerParams The header parameters
278
+ * @param requestOptions The requestOptions to send along with the query, they will be merged with
279
+ * the transporter requestOptions.
294
280
* @return The HTTP call
295
281
* @throws AlgoliaRuntimeException If fail to serialize the request body object
296
282
*/
@@ -299,14 +285,16 @@ public Call buildCall(
299
285
String method ,
300
286
Map <String , String > queryParams ,
301
287
Object body ,
302
- Map <String , String > headerParams
288
+ Map <String , String > headerParams ,
289
+ RequestOptions requestOptions
303
290
) throws AlgoliaRuntimeException {
304
291
Request request = buildRequest (
305
292
path ,
306
293
method ,
307
294
queryParams ,
308
295
body ,
309
- headerParams
296
+ headerParams ,
297
+ requestOptions
310
298
);
311
299
312
300
return requester .newCall (request );
@@ -321,6 +309,8 @@ public Call buildCall(
321
309
* @param queryParams The query parameters
322
310
* @param body The request body object
323
311
* @param headerParams The header parameters
312
+ * @param requestOptions The requestOptions to send along with the query, they will be merged with
313
+ * the transporter requestOptions.
324
314
* @return The HTTP request
325
315
* @throws AlgoliaRuntimeException If fail to serialize the request body object
326
316
*/
@@ -329,20 +319,21 @@ public Request buildRequest(
329
319
String method ,
330
320
Map <String , String > queryParams ,
331
321
Object body ,
332
- Map <String , String > headerParams
322
+ Map <String , String > headerParams ,
323
+ RequestOptions requestOptions
333
324
) throws AlgoliaRuntimeException {
334
- headerParams .put ("X-Algolia-Application-Id" , this .appId );
335
- headerParams .put ("X-Algolia-API-Key" , this .apiKey );
336
- headerParams .put ("Accept" , "application/json" );
337
- headerParams .put ("Content-Type" , "application/json" );
338
-
339
- String contentType = "application/json" ;
340
- headerParams .put ("Accept" , contentType );
341
- headerParams .put ("Content-Type" , contentType );
342
-
343
- final String url = buildUrl (path , queryParams );
325
+ boolean hasRequestOptions = requestOptions != null ;
326
+ final String url = buildUrl (
327
+ path ,
328
+ queryParams ,
329
+ hasRequestOptions ? requestOptions .getExtraQueryParams () : null
330
+ );
344
331
final Request .Builder reqBuilder = new Request .Builder ().url (url );
345
- processHeaderParams (headerParams , reqBuilder );
332
+ processHeaderParams (
333
+ headerParams ,
334
+ hasRequestOptions ? requestOptions .getExtraHeaders () : null ,
335
+ reqBuilder
336
+ );
346
337
347
338
RequestBody reqBody ;
348
339
if (!HttpMethod .permitsRequestBody (method )) {
@@ -353,10 +344,10 @@ public Request buildRequest(
353
344
reqBody = null ;
354
345
} else {
355
346
// use an empty request body (for POST, PUT and PATCH)
356
- reqBody = RequestBody .create ("" , MediaType .parse (contentType ));
347
+ reqBody = RequestBody .create ("" , MediaType .parse (this . contentType ));
357
348
}
358
349
} else {
359
- reqBody = serialize (body , contentType );
350
+ reqBody = serialize (body );
360
351
}
361
352
362
353
return reqBuilder .method (method , reqBody ).build ();
@@ -367,14 +358,38 @@ public Request buildRequest(
367
358
*
368
359
* @param path The sub path
369
360
* @param queryParams The query parameters
361
+ * @param extraQueryParams The query parameters, coming from the requestOptions
370
362
* @return The full URL
371
363
*/
372
- public String buildUrl (String path , Map <String , String > queryParams ) {
373
- final StringBuilder url = new StringBuilder ();
364
+ public String buildUrl (
365
+ String path ,
366
+ Map <String , String > queryParams ,
367
+ Map <String , String > extraQueryParams
368
+ ) {
369
+ StringBuilder url = new StringBuilder ();
374
370
375
371
// The real host will be assigned by the retry strategy
376
372
url .append ("http://temp.path" ).append (path );
377
373
374
+ url = parseQueryParameters (path , url , queryParams );
375
+ url = parseQueryParameters (path , url , extraQueryParams );
376
+
377
+ return url .toString ();
378
+ }
379
+
380
+ /**
381
+ * Parses the given map of Query Parameters to a given URL.
382
+ *
383
+ * @param path The sub path
384
+ * @param url The url to add queryParams to
385
+ * @param queryParams The query parameters
386
+ * @return The URL
387
+ */
388
+ public StringBuilder parseQueryParameters (
389
+ String path ,
390
+ StringBuilder url ,
391
+ Map <String , String > queryParams
392
+ ) {
378
393
if (queryParams != null && !queryParams .isEmpty ()) {
379
394
// support (constant) query string in `path`, e.g. "/posts?draft=1"
380
395
String prefix = path .contains ("?" ) ? "&" : "?" ;
@@ -395,17 +410,19 @@ public String buildUrl(String path, Map<String, String> queryParams) {
395
410
}
396
411
}
397
412
398
- return url . toString () ;
413
+ return url ;
399
414
}
400
415
401
416
/**
402
417
* Set header parameters to the request builder, including default headers.
403
418
*
404
419
* @param headerParams Header parameters in the form of Map
420
+ * @param extraHeaderParams Header parameters in the form of Map, coming from RequestOptions
405
421
* @param reqBuilder Request.Builder
406
422
*/
407
423
public void processHeaderParams (
408
424
Map <String , String > headerParams ,
425
+ Map <String , String > extraHeaderParams ,
409
426
Request .Builder reqBuilder
410
427
) {
411
428
for (Entry <String , String > param : headerParams .entrySet ()) {
@@ -419,6 +436,14 @@ public void processHeaderParams(
419
436
);
420
437
}
421
438
}
439
+ if (extraHeaderParams != null ) {
440
+ for (Entry <String , String > header : extraHeaderParams .entrySet ()) {
441
+ reqBuilder .header (
442
+ header .getKey (),
443
+ parameterToString (header .getValue ())
444
+ );
445
+ }
446
+ }
422
447
}
423
448
424
449
/**
0 commit comments