Skip to content

Commit 82d32f9

Browse files
committed
Modification of the get method with generic parameters
1 parent 147ce19 commit 82d32f9

10 files changed

+58
-62
lines changed

src/main/java/com/meilisearch/sdk/Documents.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected Documents(Config config) {
2424
*/
2525
String getDocument(String uid, String identifier) throws MeilisearchException {
2626
String urlPath = "/indexes/" + uid + "/documents/" + identifier;
27-
return httpClient.get(urlPath);
27+
return httpClient.get(urlPath, String.class);
2828
}
2929

3030
/**
@@ -36,7 +36,7 @@ String getDocument(String uid, String identifier) throws MeilisearchException {
3636
*/
3737
String getDocuments(String uid) throws MeilisearchException {
3838
String urlPath = "/indexes/" + uid + "/documents";
39-
return httpClient.get(urlPath);
39+
return httpClient.get(urlPath, String.class);
4040
}
4141

4242
/**
@@ -49,7 +49,7 @@ String getDocuments(String uid) throws MeilisearchException {
4949
*/
5050
String getDocuments(String uid, int limit) throws MeilisearchException {
5151
String urlQuery = "/indexes/" + uid + "/documents?limit=" + limit;
52-
return httpClient.get(urlQuery);
52+
return httpClient.get(urlQuery, String.class);
5353
}
5454

5555
/**
@@ -63,7 +63,7 @@ String getDocuments(String uid, int limit) throws MeilisearchException {
6363
*/
6464
String getDocuments(String uid, int limit, int offset) throws MeilisearchException {
6565
String urlQuery = "/indexes/" + uid + "/documents?limit=" + limit + "&offset=" + offset;
66-
return httpClient.get(urlQuery);
66+
return httpClient.get(urlQuery, String.class);
6767
}
6868

6969
/**
@@ -93,7 +93,7 @@ String getDocuments(String uid, int limit, int offset, List<String> attributesTo
9393
+ "&attributesToRetrieve="
9494
+ attributesToRetrieveCommaSeparated;
9595

96-
return httpClient.get(urlQuery);
96+
return httpClient.get(urlQuery, String.class);
9797
}
9898

9999
/**

src/main/java/com/meilisearch/sdk/HttpClient.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public HttpClient(CustomOkHttpClient client, BasicRequest request) {
5151
* @return document that was requested
5252
* @throws MeilisearchException if the response is an error
5353
*/
54-
public String get(String api) throws MeilisearchException {
55-
return this.get(api, "");
54+
<T> T get(String api, Class<T> targetClass, Class<?>... parameters)
55+
throws MeilisearchException {
56+
return this.get(api, "", targetClass, parameters);
5657
}
5758

5859
/**
@@ -63,15 +64,20 @@ public String get(String api) throws MeilisearchException {
6364
* @return document that was requested
6465
* @throws MeilisearchException if the response is an error
6566
*/
66-
String get(String api, String param) throws MeilisearchException {
67-
HttpResponse httpResponse =
68-
this.client.get(
69-
request.create(HttpMethod.GET, api + param, Collections.emptyMap(), null));
67+
<T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters)
68+
throws MeilisearchException {
69+
HttpResponse<T> httpResponse =
70+
response.create(
71+
this.client.get(
72+
request.create(
73+
HttpMethod.GET, api + param, Collections.emptyMap(), null)),
74+
targetClass,
75+
parameters);
7076
if (httpResponse.getStatusCode() >= 400) {
7177
throw new MeilisearchApiException(
7278
jsonHandler.decode(httpResponse.getContent(), APIError.class));
7379
}
74-
return new String(httpResponse.getContentAsBytes());
80+
return httpResponse.getContent();
7581
}
7682

7783
/**

src/main/java/com/meilisearch/sdk/Index.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public void waitForTask(int taskId, int timeoutInMs, int intervalInMs)
652652
public void fetchPrimaryKey() throws MeilisearchException {
653653
String requestQuery = "/indexes/" + this.uid;
654654
HttpClient httpClient = config.httpClient;
655-
Index retrievedIndex = config.jsonHandler.decode(httpClient.get(requestQuery), Index.class);
655+
Index retrievedIndex = httpClient.get(requestQuery, Index.class);
656656
this.primaryKey = retrievedIndex.getPrimaryKey();
657657
}
658658
}

src/main/java/com/meilisearch/sdk/IndexesHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ String create(String uid, String primaryKey) throws MeilisearchException {
5353
*/
5454
String get(String uid) throws MeilisearchException {
5555
String requestQuery = "/indexes/" + uid;
56-
return httpClient.get(requestQuery);
56+
return httpClient.get(requestQuery, String.class);
5757
}
5858

5959
/**
@@ -63,7 +63,7 @@ String get(String uid) throws MeilisearchException {
6363
* @throws MeilisearchException if an error occurs
6464
*/
6565
String getAll() throws MeilisearchException {
66-
return httpClient.get("/indexes");
66+
return httpClient.get("/indexes", String.class);
6767
}
6868

6969
/**

src/main/java/com/meilisearch/sdk/KeysHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public KeysHandler(Config config) {
3030
*/
3131
public Key getKey(String uid) throws MeilisearchException {
3232
String urlPath = "/keys/" + uid;
33-
return httpClient.jsonHandler.decode(this.httpClient.get(urlPath), Key.class);
33+
return httpClient.get(urlPath, Key.class);
3434
}
3535

3636
/**
@@ -41,9 +41,7 @@ public Key getKey(String uid) throws MeilisearchException {
4141
*/
4242
public Result<Key> getKeys() throws MeilisearchException {
4343
String urlPath = "/keys";
44-
Result<Key> result =
45-
httpClient.jsonHandler.decode(
46-
this.httpClient.get(urlPath), Result.class, Key.class);
44+
Result<Key> result = httpClient.get(urlPath, Result.class, Key.class);
4745
return result;
4846
}
4947

src/main/java/com/meilisearch/sdk/SettingsHandler.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public SettingsHandler(Config config) {
3232
* @throws MeilisearchException if an error occurs
3333
*/
3434
public Settings getSettings(String uid) throws MeilisearchException {
35-
return httpClient.jsonHandler.decode(
36-
httpClient.get("/indexes/" + uid + "/settings"), Settings.class);
35+
String urlPath = "/indexes/" + uid + "/settings";
36+
return httpClient.get(urlPath, Settings.class);
3737
}
3838

3939
/**
@@ -72,8 +72,8 @@ public Task resetSettings(String uid) throws MeilisearchException {
7272
* @throws MeilisearchException if an error occurs
7373
*/
7474
public String[] getRankingRuleSettings(String uid) throws MeilisearchException {
75-
return httpClient.jsonHandler.decode(
76-
httpClient.get("/indexes/" + uid + "/settings/ranking-rules"), String[].class);
75+
String urlPath = "/indexes/" + uid + "/settings/ranking-rules";
76+
return httpClient.get(urlPath, String[].class);
7777
}
7878

7979
/**
@@ -118,8 +118,8 @@ public Task resetRankingRulesSettings(String uid) throws MeilisearchException {
118118
* @throws MeilisearchException if an error occurs
119119
*/
120120
public Map<String, String[]> getSynonymsSettings(String uid) throws MeilisearchException {
121-
return httpClient.jsonHandler.decode(
122-
httpClient.get("/indexes/" + uid + "/settings/synonyms"), Map.class);
121+
String urlPath = "/indexes/" + uid + "/settings/synonyms";
122+
return httpClient.jsonHandler.decode(httpClient.get(urlPath, String.class), Map.class);
123123
}
124124

125125
/**
@@ -162,8 +162,8 @@ public Task resetSynonymsSettings(String uid) throws MeilisearchException {
162162
* @throws MeilisearchException if an error occurs
163163
*/
164164
public String[] getStopWordsSettings(String uid) throws MeilisearchException {
165-
return httpClient.jsonHandler.decode(
166-
httpClient.get("/indexes/" + uid + "/settings/stop-words"), String[].class);
165+
String urlPath = "/indexes/" + uid + "/settings/stop-words";
166+
return httpClient.get(urlPath, String[].class);
167167
}
168168

169169
/**
@@ -206,9 +206,8 @@ public Task resetStopWordsSettings(String uid) throws MeilisearchException {
206206
* @throws MeilisearchException if an error occurs
207207
*/
208208
public String[] getSearchableAttributesSettings(String uid) throws MeilisearchException {
209-
return httpClient.jsonHandler.decode(
210-
httpClient.get("/indexes/" + uid + "/settings/searchable-attributes"),
211-
String[].class);
209+
String urlPath = "/indexes/" + uid + "/settings/searchable-attributes";
210+
return httpClient.get(urlPath, String[].class);
212211
}
213212

214213
/**
@@ -255,9 +254,8 @@ public Task resetSearchableAttributesSettings(String uid) throws MeilisearchExce
255254
* @throws MeilisearchException if an error occurs
256255
*/
257256
public String[] getDisplayedAttributesSettings(String uid) throws MeilisearchException {
258-
return httpClient.jsonHandler.decode(
259-
httpClient.get("/indexes/" + uid + "/settings/displayed-attributes"),
260-
String[].class);
257+
String urlPath = "/indexes/" + uid + "/settings/displayed-attributes";
258+
return httpClient.get(urlPath, String[].class);
261259
}
262260

263261
/**
@@ -304,9 +302,8 @@ public Task resetDisplayedAttributesSettings(String uid) throws MeilisearchExcep
304302
* @throws MeilisearchException if an error occurs
305303
*/
306304
public String[] getFilterableAttributesSettings(String uid) throws MeilisearchException {
307-
return httpClient.jsonHandler.decode(
308-
httpClient.get("/indexes/" + uid + "/settings/filterable-attributes"),
309-
String[].class);
305+
String urlPath = "/indexes/" + uid + "/settings/filterable-attributes";
306+
return httpClient.get(urlPath, String[].class);
310307
}
311308

312309
/**
@@ -353,10 +350,8 @@ public Task resetFilterableAttributesSettings(String uid) throws MeilisearchExce
353350
* @throws MeilisearchException if an error occurs
354351
*/
355352
public String getDistinctAttributeSettings(String uid) throws MeilisearchException {
356-
String response =
357-
httpClient.jsonHandler.decode(
358-
httpClient.get("/indexes/" + uid + "/settings/distinct-attribute"),
359-
String.class);
353+
String urlPath = "/indexes/" + uid + "/settings/distinct-attribute";
354+
String response = httpClient.get(urlPath, String.class);
360355
return response.equals("null") ? null : response.substring(1, response.length() - 1);
361356
}
362357

@@ -402,9 +397,8 @@ public Task resetDistinctAttributeSettings(String uid) throws MeilisearchExcepti
402397
* @throws MeilisearchException if an error occurs
403398
*/
404399
public TypoTolerance getTypoToleranceSettings(String uid) throws MeilisearchException {
405-
return httpClient.jsonHandler.decode(
406-
httpClient.get("/indexes/" + uid + "/settings/typo-tolerance"),
407-
TypoTolerance.class);
400+
String urlPath = "/indexes/" + uid + "/settings/typo-tolerance";
401+
return httpClient.get(urlPath, TypoTolerance.class);
408402
}
409403

410404
/**

src/main/java/com/meilisearch/sdk/TasksHandler.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TasksHandler(Config config) {
3535
*/
3636
public Task getTask(String indexUid, int taskUid) throws MeilisearchException {
3737
String urlPath = "/indexes/" + indexUid + "/tasks/" + taskUid;
38-
return httpClient.jsonHandler.decode(this.httpClient.get(urlPath), Task.class);
38+
return httpClient.get(urlPath, Task.class);
3939
}
4040

4141
/**
@@ -48,9 +48,7 @@ public Task getTask(String indexUid, int taskUid) throws MeilisearchException {
4848
public Result<Task> getTasks(String indexUid) throws MeilisearchException {
4949
String urlPath = "/indexes/" + indexUid + "/tasks";
5050

51-
Result<Task> result =
52-
httpClient.jsonHandler.decode(
53-
this.httpClient.get(urlPath), Result.class, Task.class);
51+
Result<Task> result = httpClient.get(urlPath, Result.class, Task.class);
5452
return result;
5553
}
5654

@@ -63,7 +61,7 @@ public Result<Task> getTasks(String indexUid) throws MeilisearchException {
6361
*/
6462
public Task getTask(int taskUid) throws MeilisearchException {
6563
String urlPath = "/tasks/" + taskUid;
66-
return httpClient.jsonHandler.decode(this.httpClient.get(urlPath), Task.class);
64+
return httpClient.get(urlPath, Task.class);
6765
}
6866

6967
/**
@@ -75,9 +73,7 @@ public Task getTask(int taskUid) throws MeilisearchException {
7573
public Result<Task> getTasks() throws MeilisearchException {
7674
String urlPath = "/tasks";
7775

78-
Result<Task> result =
79-
httpClient.jsonHandler.decode(
80-
this.httpClient.get(urlPath), Result.class, Task.class);
76+
Result<Task> result = httpClient.get(urlPath, Result.class, Task.class);
8177
return result;
8278
}
8379

src/main/java/com/meilisearch/sdk/http/response/BasicResponse.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ public BasicResponse(JsonHandler jsonHandler) {
99
this.jsonHandler = jsonHandler;
1010
}
1111

12-
public <T> HttpResponse create(HttpResponse response, Class<?> targetClass) {
12+
public <T> HttpResponse<T> create(
13+
HttpResponse<T> httpResponse, Class<T> targetClass, Class<?>... parameters) {
1314
try {
14-
return new HttpResponse(
15-
response.getHeaders(),
16-
response.getStatusCode(),
17-
response.getContent() == null
15+
return new HttpResponse<T>(
16+
httpResponse.getHeaders(),
17+
httpResponse.getStatusCode(),
18+
httpResponse.getContent() == null
1819
? null
19-
: this.jsonHandler.decode(response.getContent(), targetClass));
20+
: this.jsonHandler.decode(
21+
httpResponse.getContent(), targetClass, parameters));
2022
} catch (Exception e) {
2123
throw new RuntimeException(e);
2224
}

src/main/java/com/meilisearch/sdk/http/response/HttpResponse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import lombok.Getter;
55

66
@Getter
7-
public class HttpResponse {
7+
public class HttpResponse<T> {
88
private final Map<String, String> headers;
99
private final int statusCode;
10-
private final String content;
10+
private final T content;
1111

12-
public HttpResponse(Map<String, String> headers, int statusCode, String content) {
12+
public HttpResponse(Map<String, String> headers, int statusCode, T content) {
1313
this.headers = headers;
1414
this.statusCode = statusCode;
1515
this.content = content;
@@ -20,6 +20,6 @@ public boolean hasContent() {
2020
}
2121

2222
public byte[] getContentAsBytes() {
23-
return content.getBytes();
23+
return ((String) content).getBytes();
2424
}
2525
}

src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
4949
}
5050
try {
5151
if (parameters == null || parameters.length == 0) {
52-
return (T) gson.fromJson((String) o, targetClass);
52+
return gson.<T>fromJson((String) o, targetClass);
5353
} else {
5454
TypeToken<?> parameterized = TypeToken.getParameterized(targetClass, parameters);
5555
return gson.fromJson((String) o, parameterized.getType());

0 commit comments

Comments
 (0)