Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor handlers for the usage of URLBuilder #524

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions src/main/java/com/meilisearch/sdk/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.DocumentQuery;
import com.meilisearch.sdk.model.DocumentsQuery;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.TaskInfo;
Expand All @@ -15,8 +16,14 @@
class Documents {
private final HttpClient httpClient;

/**
* Creates and sets up an instance of Documents to simplify Meilisearch API calls to manage
* documents
*
* @param config Meilisearch configuration
*/
protected Documents(Config config) {
httpClient = config.httpClient;
this.httpClient = config.httpClient;
}

/**
Expand All @@ -31,7 +38,7 @@ protected Documents(Config config) {
*/
<T> T getDocument(String uid, String identifier, Class<T> targetClass)
throws MeilisearchException {
return httpClient.<T>get(new DocumentsQuery().toQuery(uid, identifier), targetClass);
return httpClient.<T>get(documentPath(uid, identifier).getURL(), targetClass);
}

/**
Expand All @@ -44,9 +51,10 @@ <T> T getDocument(String uid, String identifier, Class<T> targetClass)
* @return Object containing the requested document
* @throws MeilisearchException if client request causes an error
*/
<T> T getDocument(String uid, String identifier, DocumentsQuery param, Class<T> targetClass)
<T> T getDocument(String uid, String identifier, DocumentQuery param, Class<T> targetClass)
throws MeilisearchException {
return httpClient.<T>get(param.toQuery(uid, identifier, param), targetClass);
return httpClient.<T>get(
documentPath(uid, identifier).addQuery(param.toQuery()).getURL(), targetClass);
}

/**
Expand All @@ -58,7 +66,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 {
return httpClient.<String>get(new DocumentsQuery().toQuery(uid, identifier), String.class);
return httpClient.<String>get(documentPath(uid, identifier).getURL(), String.class);
}

/**
Expand All @@ -70,9 +78,10 @@ String getRawDocument(String uid, String identifier) throws MeilisearchException
* @return String containing the requested document
* @throws MeilisearchException if client request causes an error
*/
String getRawDocument(String uid, String identifier, DocumentsQuery param)
String getRawDocument(String uid, String identifier, DocumentQuery param)
throws MeilisearchException {
return httpClient.<String>get(param.toQuery(uid, identifier, param), String.class);
return httpClient.<String>get(
documentPath(uid, identifier).addQuery(param.toQuery()).getURL(), String.class);
}

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

/**
Expand All @@ -101,7 +109,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 {
return httpClient.<Results>get(param.toQuery(uid, param), Results.class, targetClass);
return httpClient.<Results>get(
documentPath(uid).addQuery(param.toQuery()).getURL(), Results.class, targetClass);
}

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

/**
Expand All @@ -124,7 +133,8 @@ String getRawDocuments(String uid) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchException {
return httpClient.<String>get(param.toQuery(uid, param), String.class);
return httpClient.<String>get(
documentPath(uid).addQuery(param.toQuery()).getURL(), String.class);
}

/**
Expand All @@ -138,13 +148,11 @@ String getRawDocuments(String uid, DocumentsQuery param) throws MeilisearchExcep
*/
TaskInfo addDocuments(String uid, String document, String primaryKey)
throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes").addSubroute(uid).addSubroute("documents");
URLBuilder urlb = documentPath(uid);
if (primaryKey != null) {
urlb.addParameter("primaryKey", primaryKey);
}
String urlQuery = urlb.getURL();
return httpClient.post(urlQuery, document, TaskInfo.class);
return httpClient.post(urlb.getURL(), document, TaskInfo.class);
}

/**
Expand All @@ -158,13 +166,11 @@ TaskInfo addDocuments(String uid, String document, String primaryKey)
*/
TaskInfo updateDocuments(String uid, String document, String primaryKey)
throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes").addSubroute(uid).addSubroute("documents");
URLBuilder urlb = documentPath(uid);
if (primaryKey != null) {
urlb.addParameter("primaryKey", primaryKey);
}
String urlPath = urlb.getURL();
return httpClient.put(urlPath, document, TaskInfo.class);
return httpClient.put(urlb.getURL(), document, TaskInfo.class);
}

/**
Expand All @@ -176,8 +182,7 @@ 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 {
return httpClient.<TaskInfo>delete(
new DocumentsQuery().toQuery(uid, identifier), TaskInfo.class);
return httpClient.<TaskInfo>delete(documentPath(uid, identifier).getURL(), TaskInfo.class);
}

/**
Expand All @@ -189,13 +194,8 @@ TaskInfo deleteDocument(String uid, String identifier) throws MeilisearchExcepti
* @throws MeilisearchException if the client request causes an error
*/
TaskInfo deleteDocuments(String uid, List<String> identifiers) throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("indexes")
.addSubroute(uid)
.addSubroute("documents")
.addSubroute("delete-batch");
String urlPath = urlb.getURL();
return httpClient.post(urlPath, identifiers, TaskInfo.class);
URLBuilder urlb = documentPath(uid).addSubroute("delete-batch");
return httpClient.post(urlb.getURL(), identifiers, TaskInfo.class);
}

/**
Expand All @@ -206,6 +206,20 @@ TaskInfo deleteDocuments(String uid, List<String> identifiers) throws Meilisearc
* @throws MeilisearchException if the client request causes an error
*/
TaskInfo deleteAllDocuments(String uid) throws MeilisearchException {
return httpClient.<TaskInfo>delete(new DocumentsQuery().toQuery(uid), TaskInfo.class);
return httpClient.<TaskInfo>delete(documentPath(uid).getURL(), TaskInfo.class);
}

/** Creates an URLBuilder for the constant route documents. */
private URLBuilder documentPath(String uid) {
return new URLBuilder().addSubroute("indexes").addSubroute(uid).addSubroute("documents");
}

/** Creates an URLBuilder for the constant route documents */
private URLBuilder documentPath(String uid, String identifier) {
return new URLBuilder()
.addSubroute("indexes")
.addSubroute(uid)
.addSubroute("documents")
.addSubroute(identifier);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.meilisearch.sdk;

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.DocumentQuery;
import com.meilisearch.sdk.model.DocumentsQuery;
import com.meilisearch.sdk.model.IndexStats;
import com.meilisearch.sdk.model.Results;
Expand Down Expand Up @@ -72,7 +73,7 @@ public <T> T getDocument(String identifier, Class<T> targetClass) throws Meilise
* @return Object containing the requested document
* @throws MeilisearchException if an error occurs
*/
public <T> T getDocument(String identifier, DocumentsQuery param, Class<T> targetClass)
public <T> T getDocument(String identifier, DocumentQuery param, Class<T> targetClass)
throws MeilisearchException {
return this.documents.<T>getDocument(this.uid, identifier, param, targetClass);
}
Expand All @@ -98,7 +99,7 @@ public String getRawDocument(String identifier) throws MeilisearchException {
* @return String containing the requested document
* @throws MeilisearchException if an error occurs
*/
public String getRawDocument(String identifier, DocumentsQuery param)
public String getRawDocument(String identifier, DocumentQuery param)
throws MeilisearchException {
return this.documents.getRawDocument(this.uid, identifier, param);
}
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/meilisearch/sdk/IndexesHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.meilisearch.sdk;

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.IndexesQuery;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.TaskInfo;
Expand All @@ -12,15 +13,15 @@
* <p>https://docs.meilisearch.com/reference/api/indexes.html
*/
class IndexesHandler {
HttpClient httpClient;
private final HttpClient httpClient;

/**
* Creates and sets up an instance of IndexesHandler to simplify Meilisearch API calls to manage
* indexes
*
* @param config Meilisearch configuration
*/
IndexesHandler(Config config) {
protected IndexesHandler(Config config) {
this.httpClient = config.httpClient;
}

Expand Down Expand Up @@ -48,7 +49,7 @@ TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException
index.put("uid", uid);
index.put("primaryKey", primaryKey);

return httpClient.post("/indexes", index, TaskInfo.class);
return httpClient.post(indexesPath().getURL(), index, TaskInfo.class);
}

/**
Expand All @@ -59,7 +60,7 @@ TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException
* @throws MeilisearchException if an error occurs
*/
Index getIndex(String uid) throws MeilisearchException {
return httpClient.get(new IndexesQuery().toQuery(uid), Index.class);
return httpClient.get(indexesPath().addSubroute(uid).getURL(), Index.class);
}

/**
Expand All @@ -69,7 +70,7 @@ Index getIndex(String uid) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
Results<Index> getIndexes() throws MeilisearchException {
return httpClient.get("/indexes", Results.class, Index.class);
return httpClient.get(indexesPath().getURL(), Results.class, Index.class);
}

/**
Expand All @@ -80,7 +81,8 @@ Results<Index> getIndexes() throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
return httpClient.get(params.toQuery(params), Results.class, Index.class);
return httpClient.get(
indexesPath().addQuery(params.toQuery()).getURL(), Results.class, Index.class);
}

/**
Expand All @@ -90,7 +92,7 @@ Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
* @throws MeilisearchException if an error occurs
*/
String getRawIndexes() throws MeilisearchException {
return httpClient.get("/indexes", String.class);
return httpClient.get(indexesPath().getURL(), String.class);
}

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

return httpClient.patch(new IndexesQuery().toQuery(uid), index, TaskInfo.class);
return httpClient.patch(indexesPath().addSubroute(uid).getURL(), index, TaskInfo.class);
}

/**
Expand All @@ -116,6 +118,11 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
* @throws MeilisearchException if an error occurs
*/
TaskInfo deleteIndex(String uid) throws MeilisearchException {
return httpClient.delete(new IndexesQuery().toQuery(uid), TaskInfo.class);
return httpClient.delete(indexesPath().addSubroute(uid).getURL(), TaskInfo.class);
}

/** Creates an URLBuilder for the constant route indexes */
private URLBuilder indexesPath() {
return new URLBuilder("/indexes");
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/meilisearch/sdk/InstanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
* <p>https://docs.meilisearch.com/reference/api/keys.html
*/
public class InstanceHandler {
HttpClient httpClient;
private final HttpClient httpClient;

/**
* Creates and sets up an instance of InstanceHandler
*
* @param config Meilisearch configuration
*/
InstanceHandler(Config config) {
protected InstanceHandler(Config config) {
this.httpClient = config.httpClient;
}

Expand Down
Loading