diff --git a/src/main/java/com/meilisearch/sdk/Documents.java b/src/main/java/com/meilisearch/sdk/Documents.java index c710ec8b9..28e6aaea2 100644 --- a/src/main/java/com/meilisearch/sdk/Documents.java +++ b/src/main/java/com/meilisearch/sdk/Documents.java @@ -31,13 +31,7 @@ protected Documents(Config config) { */ T getDocument(String uid, String identifier, Class targetClass) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addSubroute(identifier); - String urlPath = urlb.getURL(); - return httpClient.get(urlPath, targetClass); + return httpClient.get(new DocumentsQuery().toQuery(uid, identifier), targetClass); } /** @@ -52,16 +46,7 @@ T getDocument(String uid, String identifier, Class targetClass) */ T getDocument(String uid, String identifier, DocumentsQuery param, Class targetClass) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addSubroute(identifier) - .addParameter("limit", param.getLimit()) - .addParameter("offset", param.getOffset()) - .addParameter("fields", param.getFields()); - String urlQuery = urlb.getURL(); - return httpClient.get(urlQuery, targetClass); + return httpClient.get(param.toQuery(uid, identifier, param), targetClass); } /** @@ -73,13 +58,7 @@ T getDocument(String uid, String identifier, DocumentsQuery param, Class * @throws MeilisearchException if client request causes an error */ String getRawDocument(String uid, String identifier) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addSubroute(identifier); - String urlPath = urlb.getURL(); - return httpClient.get(urlPath, String.class); + return httpClient.get(new DocumentsQuery().toQuery(uid, identifier), String.class); } /** @@ -93,16 +72,7 @@ String getRawDocument(String uid, String identifier) throws MeilisearchException */ String getRawDocument(String uid, String identifier, DocumentsQuery param) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addSubroute(identifier) - .addParameter("limit", param.getLimit()) - .addParameter("offset", param.getOffset()) - .addParameter("fields", param.getFields()); - String urlQuery = urlb.getURL(); - return httpClient.get(urlQuery, String.class); + return httpClient.get(param.toQuery(uid, identifier, param), String.class); } /** @@ -134,16 +104,8 @@ Results getDocuments(String uid, Class targetClass) throws Meilisearch */ Results getDocuments(String uid, DocumentsQuery param, Class targetClass) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addParameter("limit", param.getLimit()) - .addParameter("offset", param.getOffset()) - .addParameter("fields", param.getFields()); - String urlQuery = urlb.getURL(); - - Results documents = httpClient.get(urlQuery, Results.class, targetClass); + Results documents = + httpClient.get(param.toQuery(uid, param), Results.class, targetClass); return documents; } @@ -170,15 +132,7 @@ String getRawDocuments(String uid) throws MeilisearchException { * @throws MeilisearchException if an error occurs */ String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addParameter("limit", param.getLimit()) - .addParameter("offset", param.getOffset()) - .addParameter("fields", param.getFields()); - String urlQuery = urlb.getURL(); - return httpClient.get(urlQuery, String.class); + return httpClient.get(param.toQuery(uid, param), String.class); } /** @@ -230,13 +184,8 @@ TaskInfo updateDocuments(String uid, String document, String primaryKey) * @throws MeilisearchException if the client request causes an error */ TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchException { - URLBuilder urlb = new URLBuilder(); - urlb.addSubroute("indexes") - .addSubroute(uid) - .addSubroute("documents") - .addSubroute(identifier); - String urlPath = urlb.getURL(); - return httpClient.delete(urlPath, TaskInfo.class); + return httpClient.delete( + new DocumentsQuery().toQuery(uid, identifier), TaskInfo.class); } /** @@ -268,6 +217,6 @@ TaskInfo deleteAllDocuments(String uid) throws MeilisearchException { URLBuilder urlb = new URLBuilder(); urlb.addSubroute("indexes").addSubroute(uid).addSubroute("documents"); String urlPath = urlb.getURL(); - return httpClient.delete(urlPath, TaskInfo.class); + return httpClient.delete(urlPath, TaskInfo.class); } } diff --git a/src/main/java/com/meilisearch/sdk/model/DocumentsQuery.java b/src/main/java/com/meilisearch/sdk/model/DocumentsQuery.java index 1b5d123dc..546bab140 100644 --- a/src/main/java/com/meilisearch/sdk/model/DocumentsQuery.java +++ b/src/main/java/com/meilisearch/sdk/model/DocumentsQuery.java @@ -1,5 +1,6 @@ package com.meilisearch.sdk.model; +import com.meilisearch.sdk.http.URLBuilder; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @@ -18,4 +19,36 @@ public class DocumentsQuery { private String[] fields; public DocumentsQuery() {} + + public String toQuery(String uid, String identifier) { + URLBuilder urlb = new URLBuilder(); + urlb.addSubroute("indexes") + .addSubroute(uid) + .addSubroute("documents") + .addSubroute(identifier); + return urlb.getURL(); + } + + public String toQuery(String uid, DocumentsQuery param) { + URLBuilder urlb = new URLBuilder(); + urlb.addSubroute("indexes") + .addSubroute(uid) + .addSubroute("documents") + .addParameter("limit", param.getLimit()) + .addParameter("offset", param.getOffset()) + .addParameter("fields", param.getFields()); + return urlb.getURL(); + } + + public String toQuery(String uid, String identifier, DocumentsQuery param) { + URLBuilder urlb = new URLBuilder(); + urlb.addSubroute("indexes") + .addSubroute(uid) + .addSubroute("documents") + .addSubroute(identifier) + .addParameter("limit", param.getLimit()) + .addParameter("offset", param.getOffset()) + .addParameter("fields", param.getFields()); + return urlb.getURL(); + } }