Skip to content

Provide a generic to http put method #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ public String getRawIndex(String uid) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
public Task updateIndex(String uid, String primaryKey) throws MeilisearchException {
Task task =
jsonHandler.decode(
this.indexesHandler.updatePrimaryKey(uid, primaryKey), Task.class);
return task;
return this.indexesHandler.updatePrimaryKey(uid, primaryKey);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ Task updateDocuments(String uid, String document, String primaryKey)
if (primaryKey != null) {
urlPath += "?primaryKey=" + primaryKey;
}
Task task = httpClient.jsonHandler.decode(httpClient.put(urlPath, document), Task.class);

return task;
return httpClient.put(urlPath, document, Task.class);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/meilisearch/sdk/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,17 @@ <S, T> T post(String api, S body, Class<T> targetClass) throws MeilisearchExcept
* @return updated resource
* @throws MeilisearchException if the response is an error
*/
<T> String put(String api, T body) throws MeilisearchException {
HttpResponse httpResponse =
this.client.put(request.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
<S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchException {
HttpRequest requestConfig =
request.create(HttpMethod.PUT, api, Collections.emptyMap(), body);
HttpResponse<T> httpRequest = this.client.put(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);

if (httpResponse.getStatusCode() >= 400) {
throw new MeilisearchApiException(
jsonHandler.decode(httpResponse.getContent(), APIError.class));
}
return new String(httpResponse.getContentAsBytes());
return httpResponse.getContent();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/meilisearch/sdk/IndexesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ String getAll() throws MeilisearchException {
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
String updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
Task updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
HashMap<String, Object> index = new HashMap<String, Object>();
index.put("primaryKey", primaryKey);

String requestQuery = "/indexes/" + uid;
return httpClient.put(requestQuery, index);
return httpClient.put(requestQuery, index, Task.class);
}

/**
Expand Down