Repository navigation
Add possibility to provide a generic to http methods #392
Description
Description
Every method that makes an HTTP request also decode the JSON from that request inside its method.
Example:
meilisearch-java/src/main/java/com/meilisearch/sdk/TasksHandler.java
Lines 48 to 51 in 04244fa
| public Task[] getTasks(String indexUid) throws Exception { | |
| String urlPath = "/indexes/" + indexUid + "/tasks"; | |
| return this.gson.fromJson(this.meilisearchHttpRequest.get(urlPath), Task[].class); | |
| } |
An alternative to that would be to use this.gson.fromJson inside the HTTP methods and provide the type decoding type with a generic. We would avoid a lot of code duplication by doing so.
By adding a generic to this function for example:
meilisearch-java/src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java
Lines 51 to 53 in 04244fa
| public String get(String api) throws Exception, MeiliSearchApiException { | |
| return this.get(api, ""); | |
| } |
Basic example
pseudo code:
public String get<T>(String api) throws Exception, MeiliSearchApiException {
return this.gson.fromJson((this.get(api, ""), T);
}Which change the first example to the following:
public Task[] getTasks(String indexUid) throws Exception {
String urlPath = "/indexes/" + indexUid + "/tasks";
return this.meilisearchHttpRequest.get<Task[]>(urlPath);
} Possible issue
This method does not use the fromJson method:
meilisearch-java/src/main/java/com/meilisearch/sdk/Search.java
Lines 28 to 32 in 04244fa
| String rawSearch(String uid, String q) throws Exception { | |
| String requestQuery = "/indexes/" + uid + "/search"; | |
| SearchRequest sr = new SearchRequest(q); | |
| return meilisearchHttpRequest.post(requestQuery, sr.getQuery()); | |
| } |
Activity
alallema commentedon Oct 26, 2022
Closed by #466