Skip to content

Commit

Permalink
Make MeilisearchClient to extends Client.
Browse files Browse the repository at this point in the history
Changed MeilisearchClient to extends com.meilisearch.sdk.Client.
  • Loading branch information
junghoon-vans authored Nov 8, 2023
1 parent b371ed6 commit bf72bb4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/main/asciidoc/reference/meilisearch-repositories.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The Spring Data Meilisearch repositories can be configured using the `meilisearc
<bean name="meilisearchTemplate" <.>
class="io.vanslog.spring.data.meilisearch.core.MeilisearchTemplate">
<constructor-arg name="client" ref="meilisearchClient"/> <.>
<constructor-arg name="meilisearchClient" ref="meilisearchClient"/> <.>
</bean>
<meilisearch:meilisearch-client id="meilisearchClient" api-key="masterKey"/> <.>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
Expand All @@ -88,7 +80,7 @@ public <T> List<T> save(List<T> 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);
});

Expand Down Expand Up @@ -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;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<bean name="meilisearchTemplate"
class="io.vanslog.spring.data.meilisearch.core.MeilisearchTemplate">
<constructor-arg name="client" ref="meilisearchClient"/>
<constructor-arg name="meilisearchClient" ref="meilisearchClient"/>
</bean>

<meilisearch:repositories
Expand Down

0 comments on commit bf72bb4

Please sign in to comment.