Skip to content

Commit

Permalink
Add toQuery method to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
alallema committed Dec 29, 2022
1 parent 911c155 commit a7b8357
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 61 deletions.
71 changes: 10 additions & 61 deletions src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ protected Documents(Config config) {
*/
<T> T getDocument(String uid, String identifier, Class<T> targetClass)
throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes")
.addSubroute(uid)
.addSubroute("documents")
.addSubroute(identifier);
String urlPath = urlb.getURL();
return httpClient.<T>get(urlPath, targetClass);
return httpClient.<T>get(new DocumentsQuery().toQuery(uid, identifier), targetClass);
}

/**
Expand All @@ -52,16 +46,7 @@ <T> T getDocument(String uid, String identifier, Class<T> targetClass)
*/
<T> T getDocument(String uid, String identifier, DocumentsQuery param, Class<T> 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.<T>get(urlQuery, targetClass);
return httpClient.<T>get(param.toQuery(uid, identifier, param), targetClass);
}

/**
Expand All @@ -73,13 +58,7 @@ <T> T getDocument(String uid, String identifier, DocumentsQuery param, Class<T>
* @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.<String>get(new DocumentsQuery().toQuery(uid, identifier), String.class);
}

/**
Expand All @@ -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.<String>get(param.toQuery(uid, identifier, param), String.class);
}

/**
Expand Down Expand Up @@ -134,16 +104,8 @@ <T> Results<T> getDocuments(String uid, Class<T> targetClass) throws Meilisearch
*/
<T> Results<T> getDocuments(String uid, DocumentsQuery param, Class<T> 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<T> documents = httpClient.<Results>get(urlQuery, Results.class, targetClass);
Results<T> documents =
httpClient.<Results>get(param.toQuery(uid, param), Results.class, targetClass);
return documents;
}

Expand All @@ -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.<String>get(param.toQuery(uid, param), String.class);
}

/**
Expand Down Expand Up @@ -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.<TaskInfo>delete(
new DocumentsQuery().toQuery(uid, identifier), TaskInfo.class);
}

/**
Expand Down Expand Up @@ -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.<TaskInfo>delete(urlPath, TaskInfo.class);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/DocumentsQuery.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}

0 comments on commit a7b8357

Please sign in to comment.