meilisearch-java icon indicating copy to clipboard operation
meilisearch-java copied to clipboard

Add possibility to provide a generic to http methods

Open bidoubiwa opened this issue 3 years ago • 0 comments

Description Every method that makes an HTTP request also decode the JSON from that request inside its method.

Example:

https://github.com/meilisearch/meilisearch-java/blob/04244faad4b653fe4cdc5eff871463ce6e8b83bc/src/main/java/com/meilisearch/sdk/TasksHandler.java#L48-L51

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:

https://github.com/meilisearch/meilisearch-java/blob/04244faad4b653fe4cdc5eff871463ce6e8b83bc/src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java#L51-L53

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:

https://github.com/meilisearch/meilisearch-java/blob/04244faad4b653fe4cdc5eff871463ce6e8b83bc/src/main/java/com/meilisearch/sdk/Search.java#L28-L32

bidoubiwa avatar May 24 '22 16:05 bidoubiwa