Skip to content
Open
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
17 changes: 17 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,20 @@ compact_index_1: |-
client.index("INDEX_NAME").compact();
rename_an_index_1: |-
client.updateIndex("indexA", null, "indexB");
get_network_1: |-
Network networkState = client.getNetwork();
update_network_1: |-
HashMap<String, NetworksRemote> remotes = new HashMap<>();
remotes.put("ms-00", new NetworksRemote(
"http://INSTANCE_URL",
"INSTANCE_API_KEY",
null));
remotes.put("ms-01", new NetworksRemote(
"http://ANOTHER_INSTANCE_URL",
"ANOTHER_INSTANCE_API_KEY",
null));
UpdateNetwork newNetwork = new UpdateNetwork();
newNetwork.setSelf("ms-00");
newNetwork.setRemotes(remotes);

var network = this.client.updateNetwork(newNetwork);
25 changes: 24 additions & 1 deletion src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Client {
private TasksHandler tasksHandler;
private KeysHandler keysHandler;
private JsonHandler jsonHandler;
private NetworkHandler networkHandler;

/**
* Calls instance for Meilisearch client
Expand All @@ -33,6 +34,7 @@ public Client(Config config) {
this.tasksHandler = new TasksHandler(config);
this.keysHandler = new KeysHandler(config);
this.jsonHandler = config.jsonHandler;
this.networkHandler = new NetworkHandler(config);
}

/**
Expand All @@ -42,7 +44,7 @@ public Client(Config config) {
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if an error occurs
* @see <a href="https://www.meilisearch.com/docs/reference/api/indexes#create-an-index">API
* specification</a>
* specification</a>
*/
public TaskInfo createIndex(String uid) throws MeilisearchException {
return this.createIndex(uid, null);
Expand Down Expand Up @@ -544,6 +546,27 @@ public String generateTenantToken(
return jwtToken;
}

/**
* Returns the current value of the instance’s network object.
*
* @return Network object
*/
public Network getNetwork() {
return this.networkHandler.getNetworkState();
}

/**
* Update the fields of the network object. Updates to the network object are partial. Only provide the fields you intend to update.
* Fields that are null in the payload will remain unchanged. To reset self and remotes to their original value, set them to null.
* To remove a single remote from your network, set the value of its name to null.
*
* @param updatedNetwork Updated network configs
* @return Updated Network
*/
public Network updateNetwork(UpdateNetwork updatedNetwork) {
return this.networkHandler.updateNetwork(updatedNetwork);
}

private Boolean isValidUUID(String apiKeyUid) {
try {
UUID uuid = UUID.fromString(apiKeyUid);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/meilisearch/sdk/NetworkHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.meilisearch.sdk;

import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.Network;
import com.meilisearch.sdk.model.UpdateNetwork;

public class NetworkHandler {
public final HttpClient httpClient;

protected NetworkHandler(Config config) {
this.httpClient = config.httpClient;
}

/**
* Returns the current value of the instance’s network object.
*
* @return Network object
*/
public Network getNetworkState() {
return this.httpClient.get(networkPath().getURL(), Network.class);
}

/**
* Update the fields of the network object. Updates to the network object are partial. Only provide the fields you intend to update.
* Fields that are null in the payload will remain unchanged. To reset self and remotes to their original value, set them to null.
* To remove a single remote from your network, set the value of its name to null.
*
* @param updatedNetwork Updated network configs
* @return Updated Network
*/
public Network updateNetwork(UpdateNetwork updatedNetwork) {
return this.httpClient.patch(networkPath().getURL(), updatedNetwork, Network.class);
}

private URLBuilder networkPath() {
return new URLBuilder("/network");
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/Network.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.meilisearch.sdk.model;

import lombok.Getter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.UUID;

/**
* Meilisearch Network data structure.
* <p>
* <a href="https://www.meilisearch.com/docs/reference/api/network?utm_campaign=oss&utm_source=github">API Reference</a>
*/
public class Network implements Serializable {
@Getter
protected String self;
@Getter
protected String leader;
@Getter
protected UUID version;
@Getter
protected HashMap<String, NetworksRemote> remotes;
}

29 changes: 29 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/NetworksRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.meilisearch.sdk.model;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
* Meilisearch Network's remote instance's data structure.
* <p>
* <a href="https://www.meilisearch.com/docs/reference/api/network?utm_campaign=oss&utm_source=github">API Reference</a>
*/
public class NetworksRemote implements Serializable {
@Getter
@Setter
protected String url;
@Getter
@Setter
protected String searchApiKey;
@Getter
@Setter
protected String writeApiKey;

public NetworksRemote(String url, String searchApiKey, String writeApiKey) {
this.url = url;
this.searchApiKey = searchApiKey;
this.writeApiKey = writeApiKey;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/UpdateNetwork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.meilisearch.sdk.model;


import lombok.Setter;

import java.util.HashMap;


/**
* Data structure for updating a network instance.
* <p>
* <a href="https://www.meilisearch.com/docs/reference/api/network?utm_campaign=oss&utm_source=github">API Reference</a>
*/
public class UpdateNetwork {
@Setter
protected String self;
@Setter
protected String leader;
@Setter
protected HashMap<String, NetworksRemote> remotes;

public UpdateNetwork() {
}
}
59 changes: 59 additions & 0 deletions src/test/java/com/meilisearch/integration/NetworkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.meilisearch.integration;

import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.sdk.model.Network;
import com.meilisearch.sdk.model.NetworksRemote;
import com.meilisearch.sdk.model.UpdateNetwork;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.util.HashMap;
import java.util.TimeZone;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class NetworkTest extends AbstractIT {
@BeforeEach
void initialize() {
this.setUp();
this.setUpJacksonClient();
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

@AfterEach
void cleanMeilisearch() {
cleanup();
}

@Test
public void testGetNetworkState() {
Network networkState = this.client.getNetwork();

assertThat(networkState, is(notNullValue()));
assertThat(networkState, is(instanceOf(Network.class)));
}

@Test
public void patchNetworkState() {
HashMap<String, NetworksRemote> remotes = new HashMap<>();
remotes.put("ms-00", new NetworksRemote(
"http://INSTANCE_URL",
"INSTANCE_API_KEY",
null));
remotes.put("ms-01", new NetworksRemote(
"http://ANOTHER_INSTANCE_URL",
"ANOTHER_INSTANCE_API_KEY",
null));
UpdateNetwork newNetwork = new UpdateNetwork();
newNetwork.setSelf("ms-00");
newNetwork.setRemotes(remotes);

var network = this.client.updateNetwork(newNetwork);

assertThat(network, is(notNullValue()));
assertThat(network.getSelf(), is("ms-00"));
assertThat(network.getRemotes(), aMapWithSize(2));
}
}