Skip to content

Add possibility to provide a generic to http methods #392

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

Closed
bidoubiwa opened this issue May 24, 2022 · 1 comment · Fixed by #466
Closed

Add possibility to provide a generic to http methods #392

bidoubiwa opened this issue May 24, 2022 · 1 comment · Fixed by #466
Labels
enhancement New feature or request

Comments

@bidoubiwa
Copy link
Contributor

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

Example:

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:

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:

String rawSearch(String uid, String q) throws Exception {
String requestQuery = "/indexes/" + uid + "/search";
SearchRequest sr = new SearchRequest(q);
return meilisearchHttpRequest.post(requestQuery, sr.getQuery());
}

@bidoubiwa bidoubiwa added the enhancement New feature or request label May 24, 2022
@alallema alallema linked a pull request Oct 24, 2022 that will close this issue
@alallema
Copy link
Contributor

Closed by #466

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants