diff --git a/src/main/asciidoc/reference/meilisearch-repositories.adoc b/src/main/asciidoc/reference/meilisearch-repositories.adoc index ebc5e9e..4f72f80 100644 --- a/src/main/asciidoc/reference/meilisearch-repositories.adoc +++ b/src/main/asciidoc/reference/meilisearch-repositories.adoc @@ -91,7 +91,7 @@ The Spring Data Meilisearch repositories can be configured using the `meilisearc class="io.vanslog.spring.data.meilisearch.core.MeilisearchTemplate"> - <.> + <.> <.> diff --git a/src/main/java/io/vanslog/spring/data/meilisearch/client/MeilisearchClient.java b/src/main/java/io/vanslog/spring/data/meilisearch/client/MeilisearchClient.java index 5dac7b7..5263554 100644 --- a/src/main/java/io/vanslog/spring/data/meilisearch/client/MeilisearchClient.java +++ b/src/main/java/io/vanslog/spring/data/meilisearch/client/MeilisearchClient.java @@ -22,34 +22,29 @@ import com.meilisearch.sdk.json.JsonHandler; /** - * Wrapper class for a Meilisearch client. + * Extension of {@link Client} that allows to configure the additional attributes that are required for request timeout + * and retry handling. And also provides {@link JsonHandler} to convert the request and response body. * * @author Junghoon Ban */ -public class MeilisearchClient { +public class MeilisearchClient extends Client { - private final Client client; private final JsonHandler jsonHandler; private final int requestTimeout; private final int requestInterval; - public MeilisearchClient(ClientConfiguration config) { - this.client = new Client(new Config(config.getHostUrl(), config.getApiKey(), config.getClientAgents())); - this.requestTimeout = config.getRequestTimeout(); - this.requestInterval = config.getRequestInterval(); - this.jsonHandler = new GsonJsonHandler(); + public MeilisearchClient(ClientConfiguration clientConfiguration) { + this(clientConfiguration, new GsonJsonHandler()); } - public MeilisearchClient(ClientConfiguration config, JsonHandler jsonHandler) { - this.client = new Client( - new Config(config.getHostUrl(), config.getApiKey(), jsonHandler, config.getClientAgents())); - this.requestTimeout = config.getRequestTimeout(); - this.requestInterval = config.getRequestInterval(); - this.jsonHandler = jsonHandler; - } + public MeilisearchClient(ClientConfiguration clientConfiguration, JsonHandler jsonHandler) { - public Client getClient() { - return client; + super(new Config(clientConfiguration.getHostUrl(), clientConfiguration.getApiKey(), jsonHandler, + clientConfiguration.getClientAgents())); + + this.requestTimeout = clientConfiguration.getRequestTimeout(); + this.requestInterval = clientConfiguration.getRequestInterval(); + this.jsonHandler = jsonHandler; } public JsonHandler getJsonHandler() { diff --git a/src/main/java/io/vanslog/spring/data/meilisearch/core/MeilisearchTemplate.java b/src/main/java/io/vanslog/spring/data/meilisearch/core/MeilisearchTemplate.java index a085335..b2e58b9 100644 --- a/src/main/java/io/vanslog/spring/data/meilisearch/core/MeilisearchTemplate.java +++ b/src/main/java/io/vanslog/spring/data/meilisearch/core/MeilisearchTemplate.java @@ -37,10 +37,8 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import com.meilisearch.sdk.Client; import com.meilisearch.sdk.exceptions.MeilisearchApiException; import com.meilisearch.sdk.exceptions.MeilisearchException; -import com.meilisearch.sdk.json.JsonHandler; import com.meilisearch.sdk.model.TaskInfo; import com.meilisearch.sdk.model.TaskStatus; @@ -53,22 +51,16 @@ */ public class MeilisearchTemplate implements MeilisearchOperations { - private final Client meilisearchClient; - private final JsonHandler jsonHandler; - private final int requestTimeout; - private final int requestInterval; + private final MeilisearchClient meilisearchClient; private final MeilisearchConverter meilisearchConverter; - public MeilisearchTemplate(MeilisearchClient client) { - this(client, null); + public MeilisearchTemplate(MeilisearchClient meilisearchClient) { + this(meilisearchClient, null); } - public MeilisearchTemplate(MeilisearchClient client, @Nullable MeilisearchConverter meilisearchConverter) { + public MeilisearchTemplate(MeilisearchClient meilisearchClient, @Nullable MeilisearchConverter meilisearchConverter) { - this.meilisearchClient = client.getClient(); - this.jsonHandler = client.getJsonHandler(); - this.requestTimeout = client.getRequestTimeout(); - this.requestInterval = client.getRequestInterval(); + this.meilisearchClient = meilisearchClient; this.meilisearchConverter = meilisearchConverter != null ? meilisearchConverter : new MappingMeilisearchConverter(new SimpleMeilisearchMappingContext()); } @@ -88,7 +80,7 @@ public List save(List entities) { String primaryKey = Objects.requireNonNull(idProperty.getField()).getName(); TaskInfo taskInfo = execute(client -> { - String document = jsonHandler.encode(entities); + String document = meilisearchClient.getJsonHandler().encode(entities); return client.index(indexUid).addDocuments(document, primaryKey); }); @@ -202,7 +194,10 @@ private boolean isTaskSucceeded(String indexUid, TaskInfo taskInfo) { int taskUid = taskInfo.getTaskUid(); execute(client -> { - client.index(indexUid).waitForTask(taskUid, requestTimeout, requestInterval); + client.index(indexUid).waitForTask( // + taskUid, meilisearchClient.getRequestTimeout(), // + meilisearchClient.getRequestInterval() // + ); return null; }); diff --git a/src/test/java/io/vanslog/spring/data/meilisearch/config/MeilisearchNamespaceHandlerTest.java b/src/test/java/io/vanslog/spring/data/meilisearch/config/MeilisearchNamespaceHandlerTest.java index b643249..c1ed9e9 100644 --- a/src/test/java/io/vanslog/spring/data/meilisearch/config/MeilisearchNamespaceHandlerTest.java +++ b/src/test/java/io/vanslog/spring/data/meilisearch/config/MeilisearchNamespaceHandlerTest.java @@ -59,11 +59,10 @@ void shouldCreateMeilisearchTemplate() { @Test void shouldUseGsonJsonHandler() throws NoSuchFieldException, IllegalAccessException { MeilisearchClient meilisearchClient = (MeilisearchClient) context.getBean("meilisearchClient"); - Client client = meilisearchClient.getClient(); - Field jsonHandlerField = client.getClass().getDeclaredField("jsonHandler"); + Field jsonHandlerField = meilisearchClient.getClass().getDeclaredField("jsonHandler"); jsonHandlerField.setAccessible(true); - assertThat(jsonHandlerField.get(client)).isInstanceOf(com.meilisearch.sdk.json.GsonJsonHandler.class); + assertThat(jsonHandlerField.get(meilisearchClient)).isInstanceOf(com.meilisearch.sdk.json.GsonJsonHandler.class); } @Test diff --git a/src/test/resources/io/vanslog/spring/data/meilisearch/config/namespace.xml b/src/test/resources/io/vanslog/spring/data/meilisearch/config/namespace.xml index 770c64b..997de6b 100644 --- a/src/test/resources/io/vanslog/spring/data/meilisearch/config/namespace.xml +++ b/src/test/resources/io/vanslog/spring/data/meilisearch/config/namespace.xml @@ -9,7 +9,7 @@ - +