Skip to content

Add possibility to provide a generic to http methods #392

Closed

Description

@bidoubiwa

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());
}

Activity

  1. linked a pull request that will close this issueProvide a generic to http get methods #466on Oct 24, 2022
  2. alallema commented on Oct 26, 2022

    @alallema
    Contributor

    Closed by #466

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions