Skip to content

Commit ec11162

Browse files
authored
Merge pull request #524 from meilisearch/refactor_url-builder
Refactor handlers for the usage of URLBuilder
2 parents 3370cea + 38d5494 commit ec11162

13 files changed

+200
-192
lines changed

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

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.meilisearch.sdk.exceptions.MeilisearchException;
44
import com.meilisearch.sdk.http.URLBuilder;
5+
import com.meilisearch.sdk.model.DocumentQuery;
56
import com.meilisearch.sdk.model.DocumentsQuery;
67
import com.meilisearch.sdk.model.Results;
78
import com.meilisearch.sdk.model.TaskInfo;
@@ -15,8 +16,14 @@
1516
class Documents {
1617
private final HttpClient httpClient;
1718

19+
/**
20+
* Creates and sets up an instance of Documents to simplify Meilisearch API calls to manage
21+
* documents
22+
*
23+
* @param config Meilisearch configuration
24+
*/
1825
protected Documents(Config config) {
19-
httpClient = config.httpClient;
26+
this.httpClient = config.httpClient;
2027
}
2128

2229
/**
@@ -31,7 +38,7 @@ protected Documents(Config config) {
3138
*/
3239
<T> T getDocument(String uid, String identifier, Class<T> targetClass)
3340
throws MeilisearchException {
34-
return httpClient.<T>get(new DocumentsQuery().toQuery(uid, identifier), targetClass);
41+
return httpClient.<T>get(documentPath(uid, identifier).getURL(), targetClass);
3542
}
3643

3744
/**
@@ -44,9 +51,10 @@ <T> T getDocument(String uid, String identifier, Class<T> targetClass)
4451
* @return Object containing the requested document
4552
* @throws MeilisearchException if client request causes an error
4653
*/
47-
<T> T getDocument(String uid, String identifier, DocumentsQuery param, Class<T> targetClass)
54+
<T> T getDocument(String uid, String identifier, DocumentQuery param, Class<T> targetClass)
4855
throws MeilisearchException {
49-
return httpClient.<T>get(param.toQuery(uid, identifier, param), targetClass);
56+
return httpClient.<T>get(
57+
documentPath(uid, identifier).addQuery(param.toQuery()).getURL(), targetClass);
5058
}
5159

5260
/**
@@ -58,7 +66,7 @@ <T> T getDocument(String uid, String identifier, DocumentsQuery param, Class<T>
5866
* @throws MeilisearchException if client request causes an error
5967
*/
6068
String getRawDocument(String uid, String identifier) throws MeilisearchException {
61-
return httpClient.<String>get(new DocumentsQuery().toQuery(uid, identifier), String.class);
69+
return httpClient.<String>get(documentPath(uid, identifier).getURL(), String.class);
6270
}
6371

6472
/**
@@ -70,9 +78,10 @@ String getRawDocument(String uid, String identifier) throws MeilisearchException
7078
* @return String containing the requested document
7179
* @throws MeilisearchException if client request causes an error
7280
*/
73-
String getRawDocument(String uid, String identifier, DocumentsQuery param)
81+
String getRawDocument(String uid, String identifier, DocumentQuery param)
7482
throws MeilisearchException {
75-
return httpClient.<String>get(param.toQuery(uid, identifier, param), String.class);
83+
return httpClient.<String>get(
84+
documentPath(uid, identifier).addQuery(param.toQuery()).getURL(), String.class);
7685
}
7786

7887
/**
@@ -85,8 +94,7 @@ String getRawDocument(String uid, String identifier, DocumentsQuery param)
8594
* @throws MeilisearchException if the client request causes an error
8695
*/
8796
<T> Results<T> getDocuments(String uid, Class<T> targetClass) throws MeilisearchException {
88-
return httpClient.<Results>get(
89-
new DocumentsQuery().toQuery(uid), Results.class, targetClass);
97+
return httpClient.<Results>get(documentPath(uid).getURL(), Results.class, targetClass);
9098
}
9199

92100
/**
@@ -101,7 +109,8 @@ <T> Results<T> getDocuments(String uid, Class<T> targetClass) throws Meilisearch
101109
*/
102110
<T> Results<T> getDocuments(String uid, DocumentsQuery param, Class<T> targetClass)
103111
throws MeilisearchException {
104-
return httpClient.<Results>get(param.toQuery(uid, param), Results.class, targetClass);
112+
return httpClient.<Results>get(
113+
documentPath(uid).addQuery(param.toQuery()).getURL(), Results.class, targetClass);
105114
}
106115

107116
/**
@@ -112,7 +121,7 @@ <T> Results<T> getDocuments(String uid, DocumentsQuery param, Class<T> targetCla
112121
* @throws MeilisearchException if an error occurs
113122
*/
114123
String getRawDocuments(String uid) throws MeilisearchException {
115-
return httpClient.get(new DocumentsQuery().toQuery(uid), String.class);
124+
return httpClient.get(documentPath(uid).getURL(), String.class);
116125
}
117126

118127
/**
@@ -124,7 +133,8 @@ String getRawDocuments(String uid) throws MeilisearchException {
124133
* @throws MeilisearchException if an error occurs
125134
*/
126135
String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchException {
127-
return httpClient.<String>get(param.toQuery(uid, param), String.class);
136+
return httpClient.<String>get(
137+
documentPath(uid).addQuery(param.toQuery()).getURL(), String.class);
128138
}
129139

130140
/**
@@ -138,13 +148,11 @@ String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchExcep
138148
*/
139149
TaskInfo addDocuments(String uid, String document, String primaryKey)
140150
throws MeilisearchException {
141-
URLBuilder urlb = new URLBuilder();
142-
urlb.addSubroute("indexes").addSubroute(uid).addSubroute("documents");
151+
URLBuilder urlb = documentPath(uid);
143152
if (primaryKey != null) {
144153
urlb.addParameter("primaryKey", primaryKey);
145154
}
146-
String urlQuery = urlb.getURL();
147-
return httpClient.post(urlQuery, document, TaskInfo.class);
155+
return httpClient.post(urlb.getURL(), document, TaskInfo.class);
148156
}
149157

150158
/**
@@ -158,13 +166,11 @@ TaskInfo addDocuments(String uid, String document, String primaryKey)
158166
*/
159167
TaskInfo updateDocuments(String uid, String document, String primaryKey)
160168
throws MeilisearchException {
161-
URLBuilder urlb = new URLBuilder();
162-
urlb.addSubroute("indexes").addSubroute(uid).addSubroute("documents");
169+
URLBuilder urlb = documentPath(uid);
163170
if (primaryKey != null) {
164171
urlb.addParameter("primaryKey", primaryKey);
165172
}
166-
String urlPath = urlb.getURL();
167-
return httpClient.put(urlPath, document, TaskInfo.class);
173+
return httpClient.put(urlb.getURL(), document, TaskInfo.class);
168174
}
169175

170176
/**
@@ -176,8 +182,7 @@ TaskInfo updateDocuments(String uid, String document, String primaryKey)
176182
* @throws MeilisearchException if the client request causes an error
177183
*/
178184
TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchException {
179-
return httpClient.<TaskInfo>delete(
180-
new DocumentsQuery().toQuery(uid, identifier), TaskInfo.class);
185+
return httpClient.<TaskInfo>delete(documentPath(uid, identifier).getURL(), TaskInfo.class);
181186
}
182187

183188
/**
@@ -189,13 +194,8 @@ TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchExcepti
189194
* @throws MeilisearchException if the client request causes an error
190195
*/
191196
TaskInfo deleteDocuments(String uid, List<String> identifiers) throws MeilisearchException {
192-
URLBuilder urlb = new URLBuilder();
193-
urlb.addSubroute("indexes")
194-
.addSubroute(uid)
195-
.addSubroute("documents")
196-
.addSubroute("delete-batch");
197-
String urlPath = urlb.getURL();
198-
return httpClient.post(urlPath, identifiers, TaskInfo.class);
197+
URLBuilder urlb = documentPath(uid).addSubroute("delete-batch");
198+
return httpClient.post(urlb.getURL(), identifiers, TaskInfo.class);
199199
}
200200

201201
/**
@@ -206,6 +206,20 @@ TaskInfo deleteDocuments(String uid, List<String> identifiers) throws Meilisearc
206206
* @throws MeilisearchException if the client request causes an error
207207
*/
208208
TaskInfo deleteAllDocuments(String uid) throws MeilisearchException {
209-
return httpClient.<TaskInfo>delete(new DocumentsQuery().toQuery(uid), TaskInfo.class);
209+
return httpClient.<TaskInfo>delete(documentPath(uid).getURL(), TaskInfo.class);
210+
}
211+
212+
/** Creates an URLBuilder for the constant route documents. */
213+
private URLBuilder documentPath(String uid) {
214+
return new URLBuilder().addSubroute("indexes").addSubroute(uid).addSubroute("documents");
215+
}
216+
217+
/** Creates an URLBuilder for the constant route documents */
218+
private URLBuilder documentPath(String uid, String identifier) {
219+
return new URLBuilder()
220+
.addSubroute("indexes")
221+
.addSubroute(uid)
222+
.addSubroute("documents")
223+
.addSubroute(identifier);
210224
}
211225
}

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

Lines changed: 3 additions & 2 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.DocumentQuery;
45
import com.meilisearch.sdk.model.DocumentsQuery;
56
import com.meilisearch.sdk.model.IndexStats;
67
import com.meilisearch.sdk.model.Results;
@@ -72,7 +73,7 @@ public <T> T getDocument(String identifier, Class<T> targetClass) throws Meilise
7273
* @return Object containing the requested document
7374
* @throws MeilisearchException if an error occurs
7475
*/
75-
public <T> T getDocument(String identifier, DocumentsQuery param, Class<T> targetClass)
76+
public <T> T getDocument(String identifier, DocumentQuery param, Class<T> targetClass)
7677
throws MeilisearchException {
7778
return this.documents.<T>getDocument(this.uid, identifier, param, targetClass);
7879
}
@@ -98,7 +99,7 @@ public String getRawDocument(String identifier) throws MeilisearchException {
9899
* @return String containing the requested document
99100
* @throws MeilisearchException if an error occurs
100101
*/
101-
public String getRawDocument(String identifier, DocumentsQuery param)
102+
public String getRawDocument(String identifier, DocumentQuery param)
102103
throws MeilisearchException {
103104
return this.documents.getRawDocument(this.uid, identifier, param);
104105
}

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

Lines changed: 16 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.http.URLBuilder;
45
import com.meilisearch.sdk.model.IndexesQuery;
56
import com.meilisearch.sdk.model.Results;
67
import com.meilisearch.sdk.model.TaskInfo;
@@ -12,15 +13,15 @@
1213
* <p>https://docs.meilisearch.com/reference/api/indexes.html
1314
*/
1415
class IndexesHandler {
15-
HttpClient httpClient;
16+
private final HttpClient httpClient;
1617

1718
/**
1819
* Creates and sets up an instance of IndexesHandler to simplify Meilisearch API calls to manage
1920
* indexes
2021
*
2122
* @param config Meilisearch configuration
2223
*/
23-
IndexesHandler(Config config) {
24+
protected IndexesHandler(Config config) {
2425
this.httpClient = config.httpClient;
2526
}
2627

@@ -48,7 +49,7 @@ TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException
4849
index.put("uid", uid);
4950
index.put("primaryKey", primaryKey);
5051

51-
return httpClient.post("/indexes", index, TaskInfo.class);
52+
return httpClient.post(indexesPath().getURL(), index, TaskInfo.class);
5253
}
5354

5455
/**
@@ -59,7 +60,7 @@ TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException
5960
* @throws MeilisearchException if an error occurs
6061
*/
6162
Index getIndex(String uid) throws MeilisearchException {
62-
return httpClient.get(new IndexesQuery().toQuery(uid), Index.class);
63+
return httpClient.get(indexesPath().addSubroute(uid).getURL(), Index.class);
6364
}
6465

6566
/**
@@ -69,7 +70,7 @@ Index getIndex(String uid) throws MeilisearchException {
6970
* @throws MeilisearchException if an error occurs
7071
*/
7172
Results<Index> getIndexes() throws MeilisearchException {
72-
return httpClient.get("/indexes", Results.class, Index.class);
73+
return httpClient.get(indexesPath().getURL(), Results.class, Index.class);
7374
}
7475

7576
/**
@@ -80,7 +81,8 @@ Results<Index> getIndexes() throws MeilisearchException {
8081
* @throws MeilisearchException if an error occurs
8182
*/
8283
Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
83-
return httpClient.get(params.toQuery(params), Results.class, Index.class);
84+
return httpClient.get(
85+
indexesPath().addQuery(params.toQuery()).getURL(), Results.class, Index.class);
8486
}
8587

8688
/**
@@ -90,7 +92,7 @@ Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
9092
* @throws MeilisearchException if an error occurs
9193
*/
9294
String getRawIndexes() throws MeilisearchException {
93-
return httpClient.get("/indexes", String.class);
95+
return httpClient.get(indexesPath().getURL(), String.class);
9496
}
9597

9698
/**
@@ -105,7 +107,7 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
105107
HashMap<String, String> index = new HashMap<String, String>();
106108
index.put("primaryKey", primaryKey);
107109

108-
return httpClient.patch(new IndexesQuery().toQuery(uid), index, TaskInfo.class);
110+
return httpClient.patch(indexesPath().addSubroute(uid).getURL(), index, TaskInfo.class);
109111
}
110112

111113
/**
@@ -116,6 +118,11 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
116118
* @throws MeilisearchException if an error occurs
117119
*/
118120
TaskInfo deleteIndex(String uid) throws MeilisearchException {
119-
return httpClient.delete(new IndexesQuery().toQuery(uid), TaskInfo.class);
121+
return httpClient.delete(indexesPath().addSubroute(uid).getURL(), TaskInfo.class);
122+
}
123+
124+
/** Creates an URLBuilder for the constant route indexes */
125+
private URLBuilder indexesPath() {
126+
return new URLBuilder("/indexes");
120127
}
121128
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
* <p>https://docs.meilisearch.com/reference/api/keys.html
1111
*/
1212
public class InstanceHandler {
13-
HttpClient httpClient;
13+
private final HttpClient httpClient;
1414

1515
/**
1616
* Creates and sets up an instance of InstanceHandler
1717
*
1818
* @param config Meilisearch configuration
1919
*/
20-
InstanceHandler(Config config) {
20+
protected InstanceHandler(Config config) {
2121
this.httpClient = config.httpClient;
2222
}
2323

0 commit comments

Comments
 (0)