Skip to content

Provide a generic to http delete method #470

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 2 commits 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
3 changes: 1 addition & 2 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public Task updateIndex(String uid, String primaryKey) throws MeilisearchExcepti
* @throws MeilisearchException if an error occurs
*/
public Task deleteIndex(String uid) throws MeilisearchException {
Task task = jsonHandler.decode(this.indexesHandler.delete(uid), Task.class);
return task;
return this.indexesHandler.delete(uid);
}

// TODO createDump will return a Task in v0.28
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ Task updateDocuments(String uid, String document, String primaryKey)
*/
Task deleteDocument(String uid, String identifier) throws MeilisearchException {
String urlPath = "/indexes/" + uid + "/documents/" + identifier;
Task task = httpClient.jsonHandler.decode(httpClient.delete(urlPath), Task.class);

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

/**
Expand All @@ -168,8 +166,6 @@ Task deleteDocuments(String uid, List<String> identifiers) throws MeilisearchExc
*/
Task deleteAllDocuments(String uid) throws MeilisearchException {
String urlPath = "/indexes/" + uid + "/documents";
Task task = httpClient.jsonHandler.decode(httpClient.delete(urlPath), Task.class);

return task;
return httpClient.delete(urlPath, Task.class);
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/meilisearch/sdk/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti
* @return deleted resource
* @throws MeilisearchException if the response is an error
*/
String delete(String api) throws MeilisearchException {
HttpResponse httpResponse =
this.client.put(
request.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
<T> T delete(String api, Class<T> targetClass) throws MeilisearchException {
HttpRequest requestConfig =
request.create(HttpMethod.DELETE, api, Collections.emptyMap(), null);
HttpResponse<T> httpRequest = this.client.delete(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();
}
}
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 @@ -90,8 +90,8 @@ Task updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
String delete(String uid) throws MeilisearchException {
Task delete(String uid) throws MeilisearchException {
String requestQuery = "/indexes/" + uid;
return httpClient.delete(requestQuery);
return httpClient.delete(requestQuery, Task.class);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/meilisearch/sdk/KeysHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public Key createKey(Key options) throws MeilisearchException {
*/
public void deleteKey(String key) throws MeilisearchException {
String urlPath = "/keys/" + key;
this.httpClient.delete(urlPath);
httpClient.delete(urlPath, String.class);
}
}
39 changes: 18 additions & 21 deletions src/main/java/com/meilisearch/sdk/SettingsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public Task updateSettings(String uid, Settings settings) throws MeilisearchExce
* @throws MeilisearchException if an error occurs
*/
public Task resetSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings"), Task.class);
String urlPath = "/indexes/" + uid + "/settings";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -103,8 +103,8 @@ public Task updateRankingRuleSettings(String uid, String[] rankingRules)
* @throws MeilisearchException if an error occurs
*/
public Task resetRankingRulesSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/ranking-rules"), Task.class);
String urlPath = "/indexes/" + uid + "/settings/ranking-rules";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -147,8 +147,8 @@ public Task updateSynonymsSettings(String uid, Map<String, String[]> synonyms)
* @throws MeilisearchException if an error occurs
*/
public Task resetSynonymsSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/synonyms"), Task.class);
String urlPath = "/indexes/" + uid + "/settings/synonyms";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -191,8 +191,8 @@ public Task updateStopWordsSettings(String uid, String[] stopWords)
* @throws MeilisearchException if an error occurs
*/
public Task resetStopWordsSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/stop-words"), Task.class);
String urlPath = "/indexes/" + uid + "/settings/stop-words";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -238,9 +238,8 @@ public Task updateSearchableAttributesSettings(String uid, String[] searchableAt
* @throws MeilisearchException if an error occurs
*/
public Task resetSearchableAttributesSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/searchable-attributes"),
Task.class);
String urlPath = "/indexes/" + uid + "/settings/searchable-attributes";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -286,9 +285,8 @@ public Task updateDisplayedAttributesSettings(String uid, String[] displayAttrib
* @throws MeilisearchException if an error occurs
*/
public Task resetDisplayedAttributesSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/displayed-attributes"),
Task.class);
String urlPath = "/indexes/" + uid + "/settings/displayed-attributes";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -334,9 +332,8 @@ public Task updateFilterableAttributesSettings(String uid, String[] filterableAt
* @throws MeilisearchException if an error occurs
*/
public Task resetFilterableAttributesSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/filterable-attributes"),
Task.class);
String urlPath = "/indexes/" + uid + "/settings/filterable-attributes";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -382,8 +379,8 @@ public Task updateDistinctAttributeSettings(String uid, String distinctAttribute
* @throws MeilisearchException if an error occurs
*/
public Task resetDistinctAttributeSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/distinct-attribute"), Task.class);
String urlPath = "/indexes/" + uid + "/settings/distinct-attribute";
return httpClient.delete(urlPath, Task.class);
}

/**
Expand Down Expand Up @@ -428,7 +425,7 @@ public Task updateTypoToleranceSettings(String uid, TypoTolerance typoTolerance)
* @throws MeilisearchException if an error occurs
*/
public Task resetTypoToleranceSettings(String uid) throws MeilisearchException {
return httpClient.jsonHandler.decode(
httpClient.delete("/indexes/" + uid + "/settings/typo-tolerance"), Task.class);
String urlPath = "/indexes/" + uid + "/settings/typo-tolerance";
return httpClient.delete(urlPath, Task.class);
}
}