Skip to content

Commit 1219ef5

Browse files
algolia-botmillotp
andcommitted
chore: generated code for commit 961a4b5. [skip ci]
Co-authored-by: Pierre Millot <[email protected]>
1 parent 961a4b5 commit 1219ef5

File tree

120 files changed

+2893
-8760
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+2893
-8760
lines changed

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/ApiClient.java

Lines changed: 20 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.algolia;
22

33
import com.algolia.exceptions.*;
4+
import com.algolia.utils.JSON;
45
import com.algolia.utils.Requester;
56
import com.algolia.utils.UserAgent;
67
import java.io.IOException;
@@ -12,6 +13,7 @@
1213
import java.time.OffsetDateTime;
1314
import java.util.*;
1415
import java.util.Map.Entry;
16+
import java.util.concurrent.CompletableFuture;
1517
import okhttp3.*;
1618
import okhttp3.internal.http.HttpMethod;
1719

@@ -195,7 +197,7 @@ public String parameterToString(Object param) {
195197
* @param mime MIME (Multipurpose Internet Mail Extensions)
196198
* @return True if the given MIME is JSON, false otherwise.
197199
*/
198-
public boolean isJsonMime(String mime) {
200+
public static boolean isJsonMime(String mime) {
199201
String jsonMime =
200202
"(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$";
201203
return mime != null && (mime.matches(jsonMime) || mime.equals("*/*"));
@@ -215,65 +217,6 @@ public String escapeString(String str) {
215217
}
216218
}
217219

218-
/**
219-
* Deserialize response body to Java object, according to the return type and the Content-Type
220-
* response header.
221-
*
222-
* @param <T> Type
223-
* @param response HTTP response
224-
* @param returnType The type of the Java object
225-
* @return The deserialized Java object
226-
* @throws AlgoliaRuntimeException If fail to deserialize response body, i.e. cannot read response
227-
* body or the Content-Type of the response is not supported.
228-
*/
229-
public <T> T deserialize(Response response, Type returnType)
230-
throws AlgoliaRuntimeException {
231-
if (response == null || returnType == null) {
232-
return null;
233-
}
234-
235-
if ("byte[]".equals(returnType.toString())) {
236-
// Handle binary response (byte array).
237-
try {
238-
return (T) response.body().bytes();
239-
} catch (IOException e) {
240-
throw new AlgoliaRuntimeException(e);
241-
}
242-
}
243-
244-
String respBody;
245-
try {
246-
if (response.body() != null) respBody =
247-
response.body().string(); else respBody = null;
248-
} catch (IOException e) {
249-
throw new AlgoliaRuntimeException(e);
250-
}
251-
252-
if (respBody == null || "".equals(respBody)) {
253-
return null;
254-
}
255-
256-
String contentType = response.headers().get("Content-Type");
257-
if (contentType == null) {
258-
// ensuring a default content type
259-
contentType = "application/json";
260-
}
261-
if (isJsonMime(contentType)) {
262-
return JSON.deserialize(respBody, returnType);
263-
} else if (returnType.equals(String.class)) {
264-
// Expecting string, return the raw response body.
265-
return (T) respBody;
266-
} else {
267-
throw new AlgoliaApiException(
268-
"Content type \"" +
269-
contentType +
270-
"\" is not supported for type: " +
271-
returnType,
272-
response.code()
273-
);
274-
}
275-
}
276-
277220
/**
278221
* Serialize the given Java object into request body according to the object's class and the
279222
* request Content-Type.
@@ -303,147 +246,40 @@ public RequestBody serialize(Object obj, String contentType)
303246
}
304247
}
305248

306-
/**
307-
* {@link #execute(Call, Type)}
308-
*
309-
* @param <T> Type
310-
* @param call An instance of the Call object
311-
* @return ApiResponse&lt;T&gt;
312-
* @throws AlgoliaRuntimeException If fail to execute the call
313-
*/
314-
public <T> ApiResponse<T> execute(Call call) throws AlgoliaRuntimeException {
315-
return execute(call, null);
316-
}
317-
318-
/**
319-
* Execute HTTP call and deserialize the HTTP response body into the given return type.
320-
*
321-
* @param returnType The return type used to deserialize HTTP response body
322-
* @param <T> The return type corresponding to (same with) returnType
323-
* @param call Call
324-
* @return ApiResponse object containing response status, headers and data, which is a Java object
325-
* deserialized from response body and would be null when returnType is null.
326-
* @throws AlgoliaRuntimeException If fail to execute the call
327-
*/
328-
public <T> ApiResponse<T> execute(Call call, Type returnType)
329-
throws AlgoliaRuntimeException {
330-
try {
331-
Response response = call.execute();
332-
T data = handleResponse(response, returnType);
333-
return new ApiResponse<T>(
334-
response.code(),
335-
response.headers().toMultimap(),
336-
data
337-
);
338-
} catch (IOException e) {
339-
throw new AlgoliaRuntimeException(e);
340-
}
341-
}
342-
343-
/**
344-
* {@link #executeAsync(Call, Type, ApiCallback)}
345-
*
346-
* @param <T> Type
347-
* @param call An instance of the Call object
348-
* @param callback ApiCallback&lt;T&gt;
349-
*/
350-
public <T> void executeAsync(Call call, ApiCallback<T> callback) {
351-
executeAsync(call, null, callback);
352-
}
353-
354249
/**
355250
* Execute HTTP call asynchronously.
356251
*
357252
* @param <T> Type
358-
* @param call The callback to be executed when the API call finishes
359253
* @param returnType Return type
360-
* @param callback ApiCallback
361254
* @see #execute(Call, Type)
362255
*/
363-
public <T> void executeAsync(
256+
public <T> CompletableFuture<T> executeAsync(
364257
Call call,
365-
final Type returnType,
366-
final ApiCallback<T> callback
258+
final Type returnType
367259
) {
260+
final CompletableFuture<T> future = new CompletableFuture<>();
368261
call.enqueue(
369262
new Callback() {
370263
@Override
371264
public void onFailure(Call call, IOException e) {
372-
callback.onFailure(new AlgoliaRuntimeException(e), 0, null);
265+
future.completeExceptionally(new AlgoliaRuntimeException(e));
373266
}
374267

375268
@Override
376269
public void onResponse(Call call, Response response)
377270
throws IOException {
378-
T result;
379271
try {
380-
result = (T) handleResponse(response, returnType);
272+
T result = requester.handleResponse(response, returnType);
273+
future.complete(result);
381274
} catch (AlgoliaRuntimeException e) {
382-
callback.onFailure(
383-
e,
384-
response.code(),
385-
response.headers().toMultimap()
386-
);
387-
return;
275+
future.completeExceptionally(e);
388276
} catch (Exception e) {
389-
callback.onFailure(
390-
new AlgoliaRuntimeException(e),
391-
response.code(),
392-
response.headers().toMultimap()
393-
);
394-
return;
277+
future.completeExceptionally(new AlgoliaRuntimeException(e));
395278
}
396-
callback.onSuccess(
397-
result,
398-
response.code(),
399-
response.headers().toMultimap()
400-
);
401279
}
402280
}
403281
);
404-
}
405-
406-
/**
407-
* Handle the given response, return the deserialized object when the response is successful.
408-
*
409-
* @param <T> Type
410-
* @param response Response
411-
* @param returnType Return type
412-
* @return Type
413-
* @throws AlgoliaRuntimeException If the response has an unsuccessful status code or fail to
414-
* deserialize the response body
415-
*/
416-
public <T> T handleResponse(Response response, Type returnType)
417-
throws AlgoliaRuntimeException {
418-
if (response.isSuccessful()) {
419-
if (returnType == null || response.code() == 204) {
420-
// returning null if the returnType is not defined,
421-
// or the status code is 204 (No Content)
422-
if (response.body() != null) {
423-
try {
424-
response.body().close();
425-
} catch (Exception e) {
426-
throw new AlgoliaApiException(
427-
response.message(),
428-
e,
429-
response.code()
430-
);
431-
}
432-
}
433-
return null;
434-
} else {
435-
return deserialize(response, returnType);
436-
}
437-
} else {
438-
if (response.body() != null) {
439-
try {
440-
response.body().string();
441-
} catch (IOException e) {
442-
throw new AlgoliaApiException(response.message(), e, response.code());
443-
}
444-
}
445-
throw new AlgoliaApiException(response.message(), response.code());
446-
}
282+
return future;
447283
}
448284

449285
/**
@@ -455,7 +291,6 @@ public <T> T handleResponse(Response response, Type returnType)
455291
* @param queryParams The query parameters
456292
* @param body The request body object
457293
* @param headerParams The header parameters
458-
* @param callback Callback for upload/download progress
459294
* @return The HTTP call
460295
* @throws AlgoliaRuntimeException If fail to serialize the request body object
461296
*/
@@ -464,16 +299,14 @@ public Call buildCall(
464299
String method,
465300
Map<String, String> queryParams,
466301
Object body,
467-
Map<String, String> headerParams,
468-
ApiCallback callback
302+
Map<String, String> headerParams
469303
) throws AlgoliaRuntimeException {
470304
Request request = buildRequest(
471305
path,
472306
method,
473307
queryParams,
474308
body,
475-
headerParams,
476-
callback
309+
headerParams
477310
);
478311

479312
return requester.newCall(request);
@@ -488,7 +321,6 @@ public Call buildCall(
488321
* @param queryParams The query parameters
489322
* @param body The request body object
490323
* @param headerParams The header parameters
491-
* @param callback Callback for upload/download progress
492324
* @return The HTTP request
493325
* @throws AlgoliaRuntimeException If fail to serialize the request body object
494326
*/
@@ -497,20 +329,21 @@ public Request buildRequest(
497329
String method,
498330
Map<String, String> queryParams,
499331
Object body,
500-
Map<String, String> headerParams,
501-
ApiCallback callback
332+
Map<String, String> headerParams
502333
) throws AlgoliaRuntimeException {
503334
headerParams.put("X-Algolia-Application-Id", this.appId);
504335
headerParams.put("X-Algolia-API-Key", this.apiKey);
505336
headerParams.put("Accept", "application/json");
506337
headerParams.put("Content-Type", "application/json");
507338

339+
String contentType = "application/json";
340+
headerParams.put("Accept", contentType);
341+
headerParams.put("Content-Type", contentType);
342+
508343
final String url = buildUrl(path, queryParams);
509344
final Request.Builder reqBuilder = new Request.Builder().url(url);
510345
processHeaderParams(headerParams, reqBuilder);
511346

512-
String contentType = (String) headerParams.get("Content-Type");
513-
514347
RequestBody reqBody;
515348
if (!HttpMethod.permitsRequestBody(method)) {
516349
reqBody = null;
@@ -526,23 +359,7 @@ public Request buildRequest(
526359
reqBody = serialize(body, contentType);
527360
}
528361

529-
// Associate callback with request (if not null) so interceptor can
530-
// access it when creating ProgressResponseBody
531-
reqBuilder.tag(callback);
532-
533-
Request request = null;
534-
535-
if (callback != null && reqBody != null) {
536-
ProgressRequestBody progressRequestBody = new ProgressRequestBody(
537-
reqBody,
538-
callback
539-
);
540-
request = reqBuilder.method(method, progressRequestBody).build();
541-
} else {
542-
request = reqBuilder.method(method, reqBody).build();
543-
}
544-
545-
return request;
362+
return reqBuilder.method(method, reqBody).build();
546363
}
547364

548365
/**

0 commit comments

Comments
 (0)