Skip to content

Add few missing method #473

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

Merged
merged 1 commit into from
Oct 26, 2022
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
48 changes: 48 additions & 0 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.meilisearch.sdk.json.JsonHandler;
import com.meilisearch.sdk.model.Key;
import com.meilisearch.sdk.model.Result;
import com.meilisearch.sdk.model.Stats;
import com.meilisearch.sdk.model.Task;
import java.util.Date;
import java.util.Map;
Expand All @@ -18,6 +19,7 @@
public class Client {
public Config config;
public IndexesHandler indexesHandler;
public InstanceHandler instanceHandler;
public TasksHandler tasksHandler;
public KeysHandler keysHandler;
public JsonHandler jsonHandler;
Expand All @@ -30,6 +32,7 @@ public class Client {
public Client(Config config) {
this.config = config;
this.indexesHandler = new IndexesHandler(config);
this.instanceHandler = new InstanceHandler(config);
this.tasksHandler = new TasksHandler(config);
this.keysHandler = new KeysHandler(config);
this.jsonHandler = config.jsonHandler;
Expand Down Expand Up @@ -171,6 +174,51 @@ public Task deleteIndex(String uid) throws MeilisearchException {
// return this.meiliSearchHttpRequest.post("/dumps", "", Dump.class);
// }

/**
* Gets the status and availability of a Meilisearch instance
* https://docs.meilisearch.com/reference/api/health.html#health
*
* @return String containing the status of the Meilisearch instance from Meilisearch API
* response
* @throws MeilisearchException if an error occurs
*/
public String health() throws MeilisearchException {
return this.instanceHandler.health();
}

/**
* Gets the status and availability of a Meilisearch instance
* https://docs.meilisearch.com/reference/api/health.html#health
*
* @return True if the Meilisearch instance is available or false if it is not
* @throws MeilisearchException if an error occurs
*/
public Boolean isHealthy() throws MeilisearchException {
return this.instanceHandler.isHealthy();
}

/**
* Gets extended information and metrics about indexes and the Meilisearch database
* https://docs.meilisearch.com/reference/api/stats.html#stats-object
*
* @return Stats instance from Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public Stats getStats() throws MeilisearchException {
return this.instanceHandler.getStats();
}

/**
* Gets the version of Meilisearch instance
* https://docs.meilisearch.com/reference/api/version.html#version
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public String getVersion() throws MeilisearchException {
return this.instanceHandler.getVersion();
}

/**
* Retrieves a task with the specified uid
*
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/meilisearch/sdk/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters

if (httpResponse.getStatusCode() >= 400) {
throw new MeilisearchApiException(
jsonHandler.decode(httpResponse.getContent(), APIError.class));
jsonHandler.decode(httpRequest.getContent(), APIError.class));
}
return httpResponse.getContent();
}
Expand All @@ -95,7 +95,7 @@ <S, T> T post(String api, S body, Class<T> targetClass) throws MeilisearchExcept

if (httpResponse.getStatusCode() >= 400) {
throw new MeilisearchApiException(
jsonHandler.decode(httpResponse.getContent(), APIError.class));
jsonHandler.decode(httpRequest.getContent(), APIError.class));
}
return httpResponse.getContent();
}
Expand All @@ -116,7 +116,7 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti

if (httpResponse.getStatusCode() >= 400) {
throw new MeilisearchApiException(
jsonHandler.decode(httpResponse.getContent(), APIError.class));
jsonHandler.decode(httpRequest.getContent(), APIError.class));
}
return httpResponse.getContent();
}
Expand All @@ -136,7 +136,7 @@ <T> T delete(String api, Class<T> targetClass) throws MeilisearchException {

if (httpResponse.getStatusCode() >= 400) {
throw new MeilisearchApiException(
jsonHandler.decode(httpResponse.getContent(), APIError.class));
jsonHandler.decode(httpRequest.getContent(), APIError.class));
}
return httpResponse.getContent();
}
Expand Down
14 changes: 14 additions & 0 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.IndexStats;
import com.meilisearch.sdk.model.Result;
import com.meilisearch.sdk.model.SearchResult;
import com.meilisearch.sdk.model.Settings;
Expand All @@ -26,6 +27,7 @@ public class Index implements Serializable {
@ToString.Exclude protected TasksHandler tasksHandler;
@ToString.Exclude protected Search search;
@ToString.Exclude protected SettingsHandler settingsHandler;
@ToString.Exclude protected InstanceHandler instanceHandler;

/**
* Sets the Meilisearch configuration for the index
Expand All @@ -38,6 +40,7 @@ void setConfig(Config config) {
this.tasksHandler = new TasksHandler(config);
this.search = new Search(config);
this.settingsHandler = new SettingsHandler(config);
this.instanceHandler = new InstanceHandler(config);
}

/**
Expand Down Expand Up @@ -600,6 +603,17 @@ public Task resetTypoToleranceSettings() throws MeilisearchException {
return this.settingsHandler.resetTypoToleranceSettings(this.uid);
}

/**
* Gets extended information and metrics about indexes and the Meilisearch database
* https://docs.meilisearch.com/reference/api/stats.html#stats-object
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public IndexStats getStats() throws MeilisearchException {
return this.instanceHandler.getIndexStats(this.uid);
}

/**
* Retrieves an index tasks by its ID
*
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/meilisearch/sdk/InstanceHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.meilisearch.sdk;

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.IndexStats;
import com.meilisearch.sdk.model.Stats;

public class InstanceHandler {
HttpClient httpClient;

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

/**
* Gets the status and availability of a Meilisearch instance
* https://docs.meilisearch.com/reference/api/health.html#health
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public String health() throws MeilisearchException {
return httpClient.get("/health", String.class);
}

/**
* Gets the status and availability of a Meilisearch instance
* https://docs.meilisearch.com/reference/api/health.html#health
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public boolean isHealthy() throws MeilisearchException {
try {
this.health();
return true;
} catch (MeilisearchException e) {
return false;
}
}

/**
* Gets extended information and metrics about indexes and the Meilisearch database
* https://docs.meilisearch.com/reference/api/stats.html#stats-object
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public Stats getStats() throws MeilisearchException {
return httpClient.get("/stats", Stats.class);
}

/**
* Gets extended information and metrics about indexes and the Meilisearch database
* https://docs.meilisearch.com/reference/api/stats.html#stats-object
*
* @param uid Index identifier to the requested
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public IndexStats getIndexStats(String uid) throws MeilisearchException {
String requestQuery = "/indexes/" + uid + "/stats";
return httpClient.<IndexStats>get(requestQuery, IndexStats.class);
}

/**
* Gets the version of Meilisearch instance
* https://docs.meilisearch.com/reference/api/version.html#version
*
* @return Meilisearch API response
* @throws MeilisearchException if an error occurs
*/
public String getVersion() throws MeilisearchException {
return httpClient.get("/version", String.class);
}
}
77 changes: 77 additions & 0 deletions src/test/java/com/meilisearch/integration/InstanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.meilisearch.integration;

import static org.junit.jupiter.api.Assertions.*;

import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.model.IndexStats;
import com.meilisearch.sdk.model.Stats;
import com.meilisearch.sdk.model.Task;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("integration")
public class InstanceTest extends AbstractIT {

@BeforeEach
public void initialize() {
setUp();
}

@AfterAll
static void cleanMeilisearch() {
cleanup();
}

/** Test Health */
@Test
public void testHealth() throws Exception {
String health = client.health();

assertEquals(health, "{\"status\":\"available\"}");
}

/** Test Is Healthy */
@Test
public void testIsHealthy() throws Exception {
Boolean healthy = client.isHealthy();

assertTrue(healthy);
}

/** Test Get Version */
@Test
public void testGetVersion() throws Exception {
String version = client.getVersion();

assertNotNull(version);
}

/** Test Get Stats */
@Test
public void testGetStats() throws Exception {
Stats stats = client.getStats();

assertNotNull(stats);
assertNotNull(stats.getDatabaseSize());
assertNotNull(stats.getIndexes());
}

/** Test Get Index Stats */
@Test
public void testGetIndexStats() throws Exception {
String indexUid = "IndexStats";
Index index = client.index(indexUid);
Task task = client.createIndex(indexUid);

client.waitForTask(task.getUid());
IndexStats stats = index.getStats();

assertNotNull(stats);
assertEquals(0, stats.getNumberOfDocuments());
assertFalse(stats.isIndexing());
assertNotNull(stats.getFieldDistribution());
}
}