Skip to content

Commit 8f9e509

Browse files
authored
Merge pull request #469 from meilisearch/patch-put-method
Provide a generic to http put method
2 parents 12202db + 46f8c55 commit 8f9e509

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

src/main/java/com/meilisearch/sdk/Client.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ public String getRawIndex(String uid) throws MeilisearchException {
135135
* @throws MeilisearchException if an error occurs
136136
*/
137137
public Task updateIndex(String uid, String primaryKey) throws MeilisearchException {
138-
Task task =
139-
jsonHandler.decode(
140-
this.indexesHandler.updatePrimaryKey(uid, primaryKey), Task.class);
141-
return task;
138+
return this.indexesHandler.updatePrimaryKey(uid, primaryKey);
142139
}
143140

144141
/**

src/main/java/com/meilisearch/sdk/Documents.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ Task updateDocuments(String uid, String document, String primaryKey)
128128
if (primaryKey != null) {
129129
urlPath += "?primaryKey=" + primaryKey;
130130
}
131-
Task task = httpClient.jsonHandler.decode(httpClient.put(urlPath, document), Task.class);
132-
133-
return task;
131+
return httpClient.put(urlPath, document, Task.class);
134132
}
135133

136134
/**

src/main/java/com/meilisearch/sdk/HttpClient.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,17 @@ <S, T> T post(String api, S body, Class<T> targetClass) throws MeilisearchExcept
108108
* @return updated resource
109109
* @throws MeilisearchException if the response is an error
110110
*/
111-
<T> String put(String api, T body) throws MeilisearchException {
112-
HttpResponse httpResponse =
113-
this.client.put(request.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
111+
<S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchException {
112+
HttpRequest requestConfig =
113+
request.create(HttpMethod.PUT, api, Collections.emptyMap(), body);
114+
HttpResponse<T> httpRequest = this.client.put(requestConfig);
115+
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
116+
114117
if (httpResponse.getStatusCode() >= 400) {
115118
throw new MeilisearchApiException(
116119
jsonHandler.decode(httpResponse.getContent(), APIError.class));
117120
}
118-
return new String(httpResponse.getContentAsBytes());
121+
return httpResponse.getContent();
119122
}
120123

121124
/**

src/main/java/com/meilisearch/sdk/IndexesHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ String getAll() throws MeilisearchException {
7575
* @return Meilisearch API response
7676
* @throws MeilisearchException if an error occurs
7777
*/
78-
String updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
78+
Task updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
7979
HashMap<String, Object> index = new HashMap<String, Object>();
8080
index.put("primaryKey", primaryKey);
8181

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

8686
/**

0 commit comments

Comments
 (0)