Skip to content

Commit 5380e4e

Browse files
authored
Merge pull request #466 from meilisearch/patch-get-method
Provide a generic to http get methods
2 parents 147ce19 + 662fcc3 commit 5380e4e

13 files changed

+169
-192
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public Task createIndex(String uid) throws MeilisearchException {
5555
* @throws MeilisearchException if an error occurs
5656
*/
5757
public Task createIndex(String uid, String primaryKey) throws MeilisearchException {
58-
Task task = jsonHandler.decode(this.indexesHandler.create(uid, primaryKey), Task.class);
59-
return task;
58+
return this.indexesHandler.create(uid, primaryKey);
6059
}
6160

6261
/**
@@ -136,10 +135,7 @@ public String getRawIndex(String uid) throws MeilisearchException {
136135
* @throws MeilisearchException if an error occurs
137136
*/
138137
public Task updateIndex(String uid, String primaryKey) throws MeilisearchException {
139-
Task task =
140-
jsonHandler.decode(
141-
this.indexesHandler.updatePrimaryKey(uid, primaryKey), Task.class);
142-
return task;
138+
return this.indexesHandler.updatePrimaryKey(uid, primaryKey);
143139
}
144140

145141
/**
@@ -151,8 +147,7 @@ public Task updateIndex(String uid, String primaryKey) throws MeilisearchExcepti
151147
* @throws MeilisearchException if an error occurs
152148
*/
153149
public Task deleteIndex(String uid) throws MeilisearchException {
154-
Task task = jsonHandler.decode(this.indexesHandler.delete(uid), Task.class);
155-
return task;
150+
return this.indexesHandler.delete(uid);
156151
}
157152

158153
// TODO createDump will return a Task in v0.28
@@ -173,7 +168,7 @@ public Task deleteIndex(String uid) throws MeilisearchException {
173168
// * @throws MeilisearchException if an error occurs
174169
// */
175170
// public Dump createDump() throws MeilisearchException {
176-
// return jsonHandler.decode(this.meiliSearchHttpRequest.post("/dumps", ""), Dump.class);
171+
// return this.meiliSearchHttpRequest.post("/dumps", "", Dump.class);
177172
// }
178173

179174
/**

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

Lines changed: 10 additions & 21 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
/**
@@ -110,9 +110,7 @@ Task addDocuments(String uid, String document, String primaryKey) throws Meilise
110110
if (primaryKey != null) {
111111
urlQuery += "?primaryKey=" + primaryKey;
112112
}
113-
Task task = httpClient.jsonHandler.decode(httpClient.post(urlQuery, document), Task.class);
114-
115-
return task;
113+
return httpClient.post(urlQuery, document, Task.class);
116114
}
117115

118116
/**
@@ -130,9 +128,7 @@ Task updateDocuments(String uid, String document, String primaryKey)
130128
if (primaryKey != null) {
131129
urlPath += "?primaryKey=" + primaryKey;
132130
}
133-
Task task = httpClient.jsonHandler.decode(httpClient.put(urlPath, document), Task.class);
134-
135-
return task;
131+
return httpClient.put(urlPath, document, Task.class);
136132
}
137133

138134
/**
@@ -145,9 +141,7 @@ Task updateDocuments(String uid, String document, String primaryKey)
145141
*/
146142
Task deleteDocument(String uid, String identifier) throws MeilisearchException {
147143
String urlPath = "/indexes/" + uid + "/documents/" + identifier;
148-
Task task = httpClient.jsonHandler.decode(httpClient.delete(urlPath), Task.class);
149-
150-
return task;
144+
return httpClient.delete(urlPath, Task.class);
151145
}
152146

153147
/**
@@ -160,10 +154,7 @@ Task deleteDocument(String uid, String identifier) throws MeilisearchException {
160154
*/
161155
Task deleteDocuments(String uid, List<String> identifiers) throws MeilisearchException {
162156
String urlPath = "/indexes/" + uid + "/documents/" + "delete-batch";
163-
Task task =
164-
httpClient.jsonHandler.decode(httpClient.post(urlPath, identifiers), Task.class);
165-
166-
return task;
157+
return httpClient.post(urlPath, identifiers, Task.class);
167158
}
168159

169160
/**
@@ -175,8 +166,6 @@ Task deleteDocuments(String uid, List<String> identifiers) throws MeilisearchExc
175166
*/
176167
Task deleteAllDocuments(String uid) throws MeilisearchException {
177168
String urlPath = "/indexes/" + uid + "/documents";
178-
Task task = httpClient.jsonHandler.decode(httpClient.delete(urlPath), Task.class);
179-
180-
return task;
169+
return httpClient.delete(urlPath, Task.class);
181170
}
182171
}

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

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.meilisearch.sdk.http.CustomOkHttpClient;
77
import com.meilisearch.sdk.http.request.BasicRequest;
88
import com.meilisearch.sdk.http.request.HttpMethod;
9+
import com.meilisearch.sdk.http.request.HttpRequest;
910
import com.meilisearch.sdk.http.response.BasicResponse;
1011
import com.meilisearch.sdk.http.response.HttpResponse;
1112
import com.meilisearch.sdk.json.GsonJsonHandler;
@@ -51,8 +52,9 @@ public HttpClient(CustomOkHttpClient client, BasicRequest request) {
5152
* @return document that was requested
5253
* @throws MeilisearchException if the response is an error
5354
*/
54-
public String get(String api) throws MeilisearchException {
55-
return this.get(api, "");
55+
<T> T get(String api, Class<T> targetClass, Class<?>... parameters)
56+
throws MeilisearchException {
57+
return this.get(api, "", targetClass, parameters);
5658
}
5759

5860
/**
@@ -63,15 +65,18 @@ public String get(String api) throws MeilisearchException {
6365
* @return document that was requested
6466
* @throws MeilisearchException if the response is an error
6567
*/
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));
68+
<T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters)
69+
throws MeilisearchException {
70+
HttpRequest requestConfig =
71+
request.create(HttpMethod.GET, api + param, Collections.emptyMap(), null);
72+
HttpResponse<T> httpRequest = this.client.get(requestConfig);
73+
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass, parameters);
74+
7075
if (httpResponse.getStatusCode() >= 400) {
7176
throw new MeilisearchApiException(
7277
jsonHandler.decode(httpResponse.getContent(), APIError.class));
7378
}
74-
return new String(httpResponse.getContentAsBytes());
79+
return httpResponse.getContent();
7580
}
7681

7782
/**
@@ -82,15 +87,17 @@ String get(String api, String param) throws MeilisearchException {
8287
* @return results of the search
8388
* @throws MeilisearchException if the response is an error
8489
*/
85-
<T> String post(String api, T body) throws MeilisearchException {
86-
HttpResponse httpResponse =
87-
this.client.post(
88-
request.create(HttpMethod.POST, api, Collections.emptyMap(), body));
90+
<S, T> T post(String api, S body, Class<T> targetClass) throws MeilisearchException {
91+
HttpRequest requestConfig =
92+
request.create(HttpMethod.POST, api, Collections.emptyMap(), body);
93+
HttpResponse<T> httpRequest = this.client.post(requestConfig);
94+
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
95+
8996
if (httpResponse.getStatusCode() >= 400) {
9097
throw new MeilisearchApiException(
9198
jsonHandler.decode(httpResponse.getContent(), APIError.class));
9299
}
93-
return new String(httpResponse.getContentAsBytes());
100+
return httpResponse.getContent();
94101
}
95102

96103
/**
@@ -101,14 +108,17 @@ <T> String post(String api, T body) throws MeilisearchException {
101108
* @return updated resource
102109
* @throws MeilisearchException if the response is an error
103110
*/
104-
<T> String put(String api, T body) throws MeilisearchException {
105-
HttpResponse httpResponse =
106-
this.client.put(request.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
111+
<S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchException {
112+
HttpRequest requestConfig =
113+
request.create(HttpMethod.PUT, api, Collections.emptyMap(), body);
114+
HttpResponse<T> httpRequest = this.client.put(requestConfig);
115+
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
116+
107117
if (httpResponse.getStatusCode() >= 400) {
108118
throw new MeilisearchApiException(
109119
jsonHandler.decode(httpResponse.getContent(), APIError.class));
110120
}
111-
return new String(httpResponse.getContentAsBytes());
121+
return httpResponse.getContent();
112122
}
113123

114124
/**
@@ -118,14 +128,16 @@ <T> String put(String api, T body) throws MeilisearchException {
118128
* @return deleted resource
119129
* @throws MeilisearchException if the response is an error
120130
*/
121-
String delete(String api) throws MeilisearchException {
122-
HttpResponse httpResponse =
123-
this.client.put(
124-
request.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
131+
<T> T delete(String api, Class<T> targetClass) throws MeilisearchException {
132+
HttpRequest requestConfig =
133+
request.create(HttpMethod.DELETE, api, Collections.emptyMap(), null);
134+
HttpResponse<T> httpRequest = this.client.delete(requestConfig);
135+
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
136+
125137
if (httpResponse.getStatusCode() >= 400) {
126138
throw new MeilisearchApiException(
127139
jsonHandler.decode(httpResponse.getContent(), APIError.class));
128140
}
129-
return new String(httpResponse.getContentAsBytes());
141+
return httpResponse.getContent();
130142
}
131143
}

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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.meilisearch.sdk;
22

33
import com.meilisearch.sdk.exceptions.MeilisearchException;
4+
import com.meilisearch.sdk.model.Task;
45
import java.util.HashMap;
56

67
/** Wrapper around the HttpClient class to ease usage for Meilisearch indexes */
@@ -24,7 +25,7 @@ class IndexesHandler {
2425
* @return Meilisearch API response
2526
* @throws MeilisearchException if an error occurs
2627
*/
27-
String create(String uid) throws MeilisearchException {
28+
Task create(String uid) throws MeilisearchException {
2829
return this.create(uid, null);
2930
}
3031

@@ -36,12 +37,12 @@ String create(String uid) throws MeilisearchException {
3637
* @return Meilisearch API response
3738
* @throws MeilisearchException if an error occurs
3839
*/
39-
String create(String uid, String primaryKey) throws MeilisearchException {
40+
Task create(String uid, String primaryKey) throws MeilisearchException {
4041
HashMap<String, Object> index = new HashMap<String, Object>();
4142
index.put("uid", uid);
4243
index.put("primaryKey", primaryKey);
4344

44-
return httpClient.post("/indexes", index);
45+
return httpClient.post("/indexes", index, Task.class);
4546
}
4647

4748
/**
@@ -53,7 +54,7 @@ String create(String uid, String primaryKey) throws MeilisearchException {
5354
*/
5455
String get(String uid) throws MeilisearchException {
5556
String requestQuery = "/indexes/" + uid;
56-
return httpClient.get(requestQuery);
57+
return httpClient.get(requestQuery, String.class);
5758
}
5859

5960
/**
@@ -63,7 +64,7 @@ String get(String uid) throws MeilisearchException {
6364
* @throws MeilisearchException if an error occurs
6465
*/
6566
String getAll() throws MeilisearchException {
66-
return httpClient.get("/indexes");
67+
return httpClient.get("/indexes", String.class);
6768
}
6869

6970
/**
@@ -74,12 +75,12 @@ String getAll() throws MeilisearchException {
7475
* @return Meilisearch API response
7576
* @throws MeilisearchException if an error occurs
7677
*/
77-
String updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
78+
Task updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
7879
HashMap<String, Object> index = new HashMap<String, Object>();
7980
index.put("primaryKey", primaryKey);
8081

8182
String requestQuery = "/indexes/" + uid;
82-
return httpClient.put(requestQuery, index);
83+
return httpClient.put(requestQuery, index, Task.class);
8384
}
8485

8586
/**
@@ -89,8 +90,8 @@ String updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcepti
8990
* @return Meilisearch API response
9091
* @throws MeilisearchException if an error occurs
9192
*/
92-
String delete(String uid) throws MeilisearchException {
93+
Task delete(String uid) throws MeilisearchException {
9394
String requestQuery = "/indexes/" + uid;
94-
return httpClient.delete(requestQuery);
95+
return httpClient.delete(requestQuery, Task.class);
9596
}
9697
}

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

Lines changed: 4 additions & 6 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

@@ -56,7 +54,7 @@ public Result<Key> getKeys() throws MeilisearchException {
5654
*/
5755
public Key createKey(Key options) throws MeilisearchException {
5856
String urlPath = "/keys";
59-
return httpClient.jsonHandler.decode(this.httpClient.post(urlPath, options), Key.class);
57+
return httpClient.post(urlPath, options, Key.class);
6058
}
6159

6260
/**
@@ -67,6 +65,6 @@ public Key createKey(Key options) throws MeilisearchException {
6765
*/
6866
public void deleteKey(String key) throws MeilisearchException {
6967
String urlPath = "/keys/" + key;
70-
this.httpClient.delete(urlPath);
68+
httpClient.delete(urlPath, String.class);
7169
}
7270
}

0 commit comments

Comments
 (0)