1
1
package com .algolia ;
2
2
3
+ import com .algolia .exceptions .*;
3
4
import com .algolia .utils .Requester ;
4
5
import java .io .IOException ;
5
6
import java .io .UnsupportedEncodingException ;
@@ -18,7 +19,6 @@ public class ApiClient {
18
19
private boolean debugging = false ;
19
20
private Map <String , String > defaultHeaderMap = new HashMap <String , String >();
20
21
21
- private String basePath ;
22
22
private String appId , apiKey ;
23
23
24
24
private DateFormat dateFormat ;
@@ -31,7 +31,6 @@ public class ApiClient {
31
31
public ApiClient (String appId , String apiKey , Requester requester ) {
32
32
setUserAgent ("OpenAPI-Generator/0.1.0/java" );
33
33
34
- this .basePath = "https://" + appId + "-1.algolianet.com" ;
35
34
this .appId = appId ;
36
35
this .apiKey = apiKey ;
37
36
this .requester = requester ;
@@ -330,11 +329,11 @@ public String escapeString(String str) {
330
329
* @param response HTTP response
331
330
* @param returnType The type of the Java object
332
331
* @return The deserialized Java object
333
- * @throws ApiException If fail to deserialize response body, i.e. cannot read response body or
334
- * the Content-Type of the response is not supported.
332
+ * @throws AlgoliaRuntimeException If fail to deserialize response body, i.e. cannot read response
333
+ * body or the Content-Type of the response is not supported.
335
334
*/
336
335
public <T > T deserialize (Response response , Type returnType )
337
- throws ApiException {
336
+ throws AlgoliaRuntimeException {
338
337
if (response == null || returnType == null ) {
339
338
return null ;
340
339
}
@@ -344,7 +343,7 @@ public <T> T deserialize(Response response, Type returnType)
344
343
try {
345
344
return (T ) response .body ().bytes ();
346
345
} catch (IOException e ) {
347
- throw new ApiException (e );
346
+ throw new AlgoliaRuntimeException (e );
348
347
}
349
348
}
350
349
@@ -353,7 +352,7 @@ public <T> T deserialize(Response response, Type returnType)
353
352
if (response .body () != null ) respBody =
354
353
response .body ().string (); else respBody = null ;
355
354
} catch (IOException e ) {
356
- throw new ApiException (e );
355
+ throw new AlgoliaRuntimeException (e );
357
356
}
358
357
359
358
if (respBody == null || "" .equals (respBody )) {
@@ -371,14 +370,12 @@ public <T> T deserialize(Response response, Type returnType)
371
370
// Expecting string, return the raw response body.
372
371
return (T ) respBody ;
373
372
} else {
374
- throw new ApiException (
373
+ throw new AlgoliaApiException (
375
374
"Content type \" " +
376
375
contentType +
377
376
"\" is not supported for type: " +
378
377
returnType ,
379
- response .code (),
380
- response .headers ().toMultimap (),
381
- respBody
378
+ response .code ()
382
379
);
383
380
}
384
381
}
@@ -390,10 +387,10 @@ public <T> T deserialize(Response response, Type returnType)
390
387
* @param obj The Java object
391
388
* @param contentType The request Content-Type
392
389
* @return The serialized request body
393
- * @throws ApiException If fail to serialize the given object
390
+ * @throws AlgoliaRuntimeException If fail to serialize the given object
394
391
*/
395
392
public RequestBody serialize (Object obj , String contentType )
396
- throws ApiException {
393
+ throws AlgoliaRuntimeException {
397
394
if (obj instanceof byte []) {
398
395
// Binary (byte array) body parameter support.
399
396
return RequestBody .create ((byte []) obj , MediaType .parse (contentType ));
@@ -406,7 +403,7 @@ public RequestBody serialize(Object obj, String contentType)
406
403
}
407
404
return RequestBody .create (content , MediaType .parse (contentType ));
408
405
} else {
409
- throw new ApiException (
406
+ throw new AlgoliaRuntimeException (
410
407
"Content type \" " + contentType + "\" is not supported"
411
408
);
412
409
}
@@ -418,9 +415,9 @@ public RequestBody serialize(Object obj, String contentType)
418
415
* @param <T> Type
419
416
* @param call An instance of the Call object
420
417
* @return ApiResponse<T>
421
- * @throws ApiException If fail to execute the call
418
+ * @throws AlgoliaRuntimeException If fail to execute the call
422
419
*/
423
- public <T > ApiResponse <T > execute (Call call ) throws ApiException {
420
+ public <T > ApiResponse <T > execute (Call call ) throws AlgoliaRuntimeException {
424
421
return execute (call , null );
425
422
}
426
423
@@ -432,10 +429,10 @@ public <T> ApiResponse<T> execute(Call call) throws ApiException {
432
429
* @param call Call
433
430
* @return ApiResponse object containing response status, headers and data, which is a Java object
434
431
* deserialized from response body and would be null when returnType is null.
435
- * @throws ApiException If fail to execute the call
432
+ * @throws AlgoliaRuntimeException If fail to execute the call
436
433
*/
437
434
public <T > ApiResponse <T > execute (Call call , Type returnType )
438
- throws ApiException {
435
+ throws AlgoliaRuntimeException {
439
436
try {
440
437
Response response = call .execute ();
441
438
T data = handleResponse (response , returnType );
@@ -445,7 +442,7 @@ public <T> ApiResponse<T> execute(Call call, Type returnType)
445
442
data
446
443
);
447
444
} catch (IOException e ) {
448
- throw new ApiException (e );
445
+ throw new AlgoliaRuntimeException (e );
449
446
}
450
447
}
451
448
@@ -478,7 +475,7 @@ public <T> void executeAsync(
478
475
new Callback () {
479
476
@ Override
480
477
public void onFailure (Call call , IOException e ) {
481
- callback .onFailure (new ApiException (e ), 0 , null );
478
+ callback .onFailure (new AlgoliaRuntimeException (e ), 0 , null );
482
479
}
483
480
484
481
@ Override
@@ -487,7 +484,7 @@ public void onResponse(Call call, Response response)
487
484
T result ;
488
485
try {
489
486
result = (T ) handleResponse (response , returnType );
490
- } catch (ApiException e ) {
487
+ } catch (AlgoliaRuntimeException e ) {
491
488
callback .onFailure (
492
489
e ,
493
490
response .code (),
@@ -496,7 +493,7 @@ public void onResponse(Call call, Response response)
496
493
return ;
497
494
} catch (Exception e ) {
498
495
callback .onFailure (
499
- new ApiException (e ),
496
+ new AlgoliaRuntimeException (e ),
500
497
response .code (),
501
498
response .headers ().toMultimap ()
502
499
);
@@ -519,11 +516,11 @@ public void onResponse(Call call, Response response)
519
516
* @param response Response
520
517
* @param returnType Return type
521
518
* @return Type
522
- * @throws ApiException If the response has an unsuccessful status code or fail to deserialize the
523
- * response body
519
+ * @throws AlgoliaRuntimeException If the response has an unsuccessful status code or fail to
520
+ * deserialize the response body
524
521
*/
525
522
public <T > T handleResponse (Response response , Type returnType )
526
- throws ApiException {
523
+ throws AlgoliaRuntimeException {
527
524
if (response .isSuccessful ()) {
528
525
if (returnType == null || response .code () == 204 ) {
529
526
// returning null if the returnType is not defined,
@@ -532,11 +529,10 @@ public <T> T handleResponse(Response response, Type returnType)
532
529
try {
533
530
response .body ().close ();
534
531
} catch (Exception e ) {
535
- throw new ApiException (
532
+ throw new AlgoliaApiException (
536
533
response .message (),
537
534
e ,
538
- response .code (),
539
- response .headers ().toMultimap ()
535
+ response .code ()
540
536
);
541
537
}
542
538
}
@@ -545,25 +541,14 @@ public <T> T handleResponse(Response response, Type returnType)
545
541
return deserialize (response , returnType );
546
542
}
547
543
} else {
548
- String respBody = null ;
549
544
if (response .body () != null ) {
550
545
try {
551
- respBody = response .body ().string ();
546
+ response .body ().string ();
552
547
} catch (IOException e ) {
553
- throw new ApiException (
554
- response .message (),
555
- e ,
556
- response .code (),
557
- response .headers ().toMultimap ()
558
- );
548
+ throw new AlgoliaApiException (response .message (), e , response .code ());
559
549
}
560
550
}
561
- throw new ApiException (
562
- response .message (),
563
- response .code (),
564
- response .headers ().toMultimap (),
565
- respBody
566
- );
551
+ throw new AlgoliaApiException (response .message (), response .code ());
567
552
}
568
553
}
569
554
@@ -578,7 +563,7 @@ public <T> T handleResponse(Response response, Type returnType)
578
563
* @param headerParams The header parameters
579
564
* @param callback Callback for upload/download progress
580
565
* @return The HTTP call
581
- * @throws ApiException If fail to serialize the request body object
566
+ * @throws AlgoliaRuntimeException If fail to serialize the request body object
582
567
*/
583
568
public Call buildCall (
584
569
String path ,
@@ -587,7 +572,7 @@ public Call buildCall(
587
572
Object body ,
588
573
Map <String , String > headerParams ,
589
574
ApiCallback callback
590
- ) throws ApiException {
575
+ ) throws AlgoliaRuntimeException {
591
576
Request request = buildRequest (
592
577
path ,
593
578
method ,
@@ -611,7 +596,7 @@ public Call buildCall(
611
596
* @param headerParams The header parameters
612
597
* @param callback Callback for upload/download progress
613
598
* @return The HTTP request
614
- * @throws ApiException If fail to serialize the request body object
599
+ * @throws AlgoliaRuntimeException If fail to serialize the request body object
615
600
*/
616
601
public Request buildRequest (
617
602
String path ,
@@ -620,7 +605,7 @@ public Request buildRequest(
620
605
Object body ,
621
606
Map <String , String > headerParams ,
622
607
ApiCallback callback
623
- ) throws ApiException {
608
+ ) throws AlgoliaRuntimeException {
624
609
headerParams .put ("X-Algolia-Application-Id" , this .appId );
625
610
headerParams .put ("X-Algolia-API-Key" , this .apiKey );
626
611
@@ -677,7 +662,9 @@ public Request buildRequest(
677
662
*/
678
663
public String buildUrl (String path , List <Pair > queryParams ) {
679
664
final StringBuilder url = new StringBuilder ();
680
- url .append (basePath ).append (path );
665
+
666
+ // The real host will be assigned by the retry strategy
667
+ url .append ("http://temp.path" ).append (path );
681
668
682
669
if (queryParams != null && !queryParams .isEmpty ()) {
683
670
// support (constant) query string in `path`, e.g. "/posts?draft=1"
0 commit comments