77import org .slf4j .Logger ;
88import org .slf4j .LoggerFactory ;
99import org .typesense .api .exceptions .*;
10- import org .typesense .model .ErrorResponse ;
1110import org .typesense .resources .Node ;
1211
1312import javax .net .ssl .SSLException ;
1413import java .io .IOException ;
15- import java .net .SocketTimeoutException ;
1614import java .time .Duration ;
1715import java .time .LocalDateTime ;
1816import java .util .ArrayList ;
2119import java .util .Map ;
2220import java .util .concurrent .TimeUnit ;
2321
24-
2522public class ApiCall {
2623
2724 private final Configuration configuration ;
@@ -61,14 +58,15 @@ public ApiCall(Configuration configuration) {
6158 mapper .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
6259
6360 client = new OkHttpClient ()
64- .newBuilder ()
65- .connectTimeout (configuration .connectionTimeout .getSeconds (), TimeUnit .SECONDS )
66- .readTimeout (configuration .readTimeout .getSeconds (), TimeUnit .SECONDS )
67- .build ();
61+ .newBuilder ()
62+ .connectTimeout (configuration .connectionTimeout .getSeconds (), TimeUnit .SECONDS )
63+ .readTimeout (configuration .readTimeout .getSeconds (), TimeUnit .SECONDS )
64+ .build ();
6865 }
6966
7067 boolean isDueForHealthCheck (Node node ) {
71- return Duration .between (node .lastAccessTimestamp , LocalDateTime .now ()).getSeconds () > configuration .healthCheckInterval .getSeconds ();
68+ return Duration .between (node .lastAccessTimestamp , LocalDateTime .now ())
69+ .getSeconds () > configuration .healthCheckInterval .getSeconds ();
7270 }
7371
7472 // Loops in a round-robin fashion to check for a healthy node and returns it
@@ -161,7 +159,7 @@ <Q, R> R delete(String endpoint, Q queryParameters, Class<R> responseClass) thro
161159 }
162160
163161 <Q , T > T makeRequest (String endpoint , Q queryParameters , Request .Builder requestBuilder ,
164- Class <T > responseClass ) throws Exception {
162+ Class <T > responseClass ) throws Exception {
165163 int num_tries = 0 ;
166164 Exception lastException = new TypesenseError ("Unknown client error" , 400 );
167165
@@ -174,9 +172,9 @@ <Q, T> T makeRequest(String endpoint, Q queryParameters, Request.Builder request
174172 String url = URI + endpoint ;
175173 String fullUrl = populateQueryParameters (url , queryParameters ).toString ();
176174 Request request = requestBuilder
177- .url (fullUrl )
178- .header (API_KEY_HEADER , apiKey )
179- .build ();
175+ .url (fullUrl )
176+ .header (API_KEY_HEADER , apiKey )
177+ .build ();
180178
181179 Response response = client .newCall (request ).execute ();
182180
@@ -193,11 +191,11 @@ <Q, T> T makeRequest(String endpoint, Q queryParameters, Request.Builder request
193191
194192 } catch (Exception e ) {
195193 boolean handleError = (e instanceof ServerError ) ||
196- (e instanceof ServiceUnavailable ) ||
197- (e .getClass ().getPackage ().getName ().startsWith ("java.net" )) ||
198- (e instanceof SSLException );
194+ (e instanceof ServiceUnavailable ) ||
195+ (e .getClass ().getPackage ().getName ().startsWith ("java.net" )) ||
196+ (e instanceof SSLException );
199197
200- if (!handleError ) {
198+ if (!handleError ) {
201199 // we just throw and move on
202200 throw e ;
203201 }
@@ -233,7 +231,7 @@ private <T> HttpUrl.Builder populateQueryParameters(String url, T queryParameter
233231 value .append ("," );
234232 }
235233 httpBuilder .addQueryParameter (entry .getKey (), value .toString ());
236- } else if (entry .getValue () != null ){
234+ } else if (entry .getValue () != null ) {
237235 httpBuilder .addQueryParameter (entry .getKey (), entry .getValue ().toString ());
238236 }
239237 }
@@ -272,3 +270,10 @@ <T> T handleResponse(Response response, Class<T> responseClass) throws IOExcepti
272270
273271}
274272
273+ class ErrorResponse {
274+ private String message = null ;
275+
276+ public String getMessage () {
277+ return message ;
278+ }
279+ }
0 commit comments