Skip to content

Commit

Permalink
Generated code for commit e4a064f. (#298)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>

Co-authored-by: api-clients-bot <bot@algolia.com>
  • Loading branch information
millotp and api-clients-bot authored Mar 28, 2022
1 parent 0cebca1 commit 3dfaf94
Show file tree
Hide file tree
Showing 4 changed files with 788 additions and 662 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia;

import com.algolia.exceptions.AlgoliaRuntimeException;
import java.util.List;
import java.util.Map;

Expand All @@ -17,7 +18,7 @@ public interface ApiCallback<T> {
* @param responseHeaders Headers of the response if available, otherwise it would be null
*/
void onFailure(
ApiException e,
AlgoliaRuntimeException e,
int statusCode,
Map<String, List<String>> responseHeaders
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia;

import com.algolia.exceptions.*;
import com.algolia.utils.Requester;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -18,7 +19,6 @@ public class ApiClient {
private boolean debugging = false;
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();

private String basePath;
private String appId, apiKey;

private DateFormat dateFormat;
Expand All @@ -31,7 +31,6 @@ public class ApiClient {
public ApiClient(String appId, String apiKey, Requester requester) {
setUserAgent("OpenAPI-Generator/0.1.0/java");

this.basePath = "https://" + appId + "-1.algolianet.com";
this.appId = appId;
this.apiKey = apiKey;
this.requester = requester;
Expand Down Expand Up @@ -330,11 +329,11 @@ public String escapeString(String str) {
* @param response HTTP response
* @param returnType The type of the Java object
* @return The deserialized Java object
* @throws ApiException If fail to deserialize response body, i.e. cannot read response body or
* the Content-Type of the response is not supported.
* @throws AlgoliaRuntimeException If fail to deserialize response body, i.e. cannot read response
* body or the Content-Type of the response is not supported.
*/
public <T> T deserialize(Response response, Type returnType)
throws ApiException {
throws AlgoliaRuntimeException {
if (response == null || returnType == null) {
return null;
}
Expand All @@ -344,7 +343,7 @@ public <T> T deserialize(Response response, Type returnType)
try {
return (T) response.body().bytes();
} catch (IOException e) {
throw new ApiException(e);
throw new AlgoliaRuntimeException(e);
}
}

Expand All @@ -353,7 +352,7 @@ public <T> T deserialize(Response response, Type returnType)
if (response.body() != null) respBody =
response.body().string(); else respBody = null;
} catch (IOException e) {
throw new ApiException(e);
throw new AlgoliaRuntimeException(e);
}

if (respBody == null || "".equals(respBody)) {
Expand All @@ -371,14 +370,12 @@ public <T> T deserialize(Response response, Type returnType)
// Expecting string, return the raw response body.
return (T) respBody;
} else {
throw new ApiException(
throw new AlgoliaApiException(
"Content type \"" +
contentType +
"\" is not supported for type: " +
returnType,
response.code(),
response.headers().toMultimap(),
respBody
response.code()
);
}
}
Expand All @@ -390,10 +387,10 @@ public <T> T deserialize(Response response, Type returnType)
* @param obj The Java object
* @param contentType The request Content-Type
* @return The serialized request body
* @throws ApiException If fail to serialize the given object
* @throws AlgoliaRuntimeException If fail to serialize the given object
*/
public RequestBody serialize(Object obj, String contentType)
throws ApiException {
throws AlgoliaRuntimeException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
Expand All @@ -406,7 +403,7 @@ public RequestBody serialize(Object obj, String contentType)
}
return RequestBody.create(content, MediaType.parse(contentType));
} else {
throw new ApiException(
throw new AlgoliaRuntimeException(
"Content type \"" + contentType + "\" is not supported"
);
}
Expand All @@ -418,9 +415,9 @@ public RequestBody serialize(Object obj, String contentType)
* @param <T> Type
* @param call An instance of the Call object
* @return ApiResponse&lt;T&gt;
* @throws ApiException If fail to execute the call
* @throws AlgoliaRuntimeException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call) throws ApiException {
public <T> ApiResponse<T> execute(Call call) throws AlgoliaRuntimeException {
return execute(call, null);
}

Expand All @@ -432,10 +429,10 @@ public <T> ApiResponse<T> execute(Call call) throws ApiException {
* @param call Call
* @return ApiResponse object containing response status, headers and data, which is a Java object
* deserialized from response body and would be null when returnType is null.
* @throws ApiException If fail to execute the call
* @throws AlgoliaRuntimeException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call, Type returnType)
throws ApiException {
throws AlgoliaRuntimeException {
try {
Response response = call.execute();
T data = handleResponse(response, returnType);
Expand All @@ -445,7 +442,7 @@ public <T> ApiResponse<T> execute(Call call, Type returnType)
data
);
} catch (IOException e) {
throw new ApiException(e);
throw new AlgoliaRuntimeException(e);
}
}

Expand Down Expand Up @@ -478,7 +475,7 @@ public <T> void executeAsync(
new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onFailure(new ApiException(e), 0, null);
callback.onFailure(new AlgoliaRuntimeException(e), 0, null);
}

@Override
Expand All @@ -487,7 +484,7 @@ public void onResponse(Call call, Response response)
T result;
try {
result = (T) handleResponse(response, returnType);
} catch (ApiException e) {
} catch (AlgoliaRuntimeException e) {
callback.onFailure(
e,
response.code(),
Expand All @@ -496,7 +493,7 @@ public void onResponse(Call call, Response response)
return;
} catch (Exception e) {
callback.onFailure(
new ApiException(e),
new AlgoliaRuntimeException(e),
response.code(),
response.headers().toMultimap()
);
Expand All @@ -519,11 +516,11 @@ public void onResponse(Call call, Response response)
* @param response Response
* @param returnType Return type
* @return Type
* @throws ApiException If the response has an unsuccessful status code or fail to deserialize the
* response body
* @throws AlgoliaRuntimeException If the response has an unsuccessful status code or fail to
* deserialize the response body
*/
public <T> T handleResponse(Response response, Type returnType)
throws ApiException {
throws AlgoliaRuntimeException {
if (response.isSuccessful()) {
if (returnType == null || response.code() == 204) {
// returning null if the returnType is not defined,
Expand All @@ -532,11 +529,10 @@ public <T> T handleResponse(Response response, Type returnType)
try {
response.body().close();
} catch (Exception e) {
throw new ApiException(
throw new AlgoliaApiException(
response.message(),
e,
response.code(),
response.headers().toMultimap()
response.code()
);
}
}
Expand All @@ -545,25 +541,14 @@ public <T> T handleResponse(Response response, Type returnType)
return deserialize(response, returnType);
}
} else {
String respBody = null;
if (response.body() != null) {
try {
respBody = response.body().string();
response.body().string();
} catch (IOException e) {
throw new ApiException(
response.message(),
e,
response.code(),
response.headers().toMultimap()
);
throw new AlgoliaApiException(response.message(), e, response.code());
}
}
throw new ApiException(
response.message(),
response.code(),
response.headers().toMultimap(),
respBody
);
throw new AlgoliaApiException(response.message(), response.code());
}
}

Expand All @@ -578,7 +563,7 @@ public <T> T handleResponse(Response response, Type returnType)
* @param headerParams The header parameters
* @param callback Callback for upload/download progress
* @return The HTTP call
* @throws ApiException If fail to serialize the request body object
* @throws AlgoliaRuntimeException If fail to serialize the request body object
*/
public Call buildCall(
String path,
Expand All @@ -587,7 +572,7 @@ public Call buildCall(
Object body,
Map<String, String> headerParams,
ApiCallback callback
) throws ApiException {
) throws AlgoliaRuntimeException {
Request request = buildRequest(
path,
method,
Expand All @@ -611,7 +596,7 @@ public Call buildCall(
* @param headerParams The header parameters
* @param callback Callback for upload/download progress
* @return The HTTP request
* @throws ApiException If fail to serialize the request body object
* @throws AlgoliaRuntimeException If fail to serialize the request body object
*/
public Request buildRequest(
String path,
Expand All @@ -620,7 +605,7 @@ public Request buildRequest(
Object body,
Map<String, String> headerParams,
ApiCallback callback
) throws ApiException {
) throws AlgoliaRuntimeException {
headerParams.put("X-Algolia-Application-Id", this.appId);
headerParams.put("X-Algolia-API-Key", this.apiKey);

Expand Down Expand Up @@ -677,7 +662,9 @@ public Request buildRequest(
*/
public String buildUrl(String path, List<Pair> queryParams) {
final StringBuilder url = new StringBuilder();
url.append(basePath).append(path);

// The real host will be assigned by the retry strategy
url.append("http://temp.path").append(path);

if (queryParams != null && !queryParams.isEmpty()) {
// support (constant) query string in `path`, e.g. "/posts?draft=1"
Expand Down
Loading

0 comments on commit 3dfaf94

Please sign in to comment.