From 93177133a2a1787b4b701c93330b5c3381073e68 Mon Sep 17 00:00:00 2001 From: Ali Tariq Date: Tue, 3 Feb 2026 16:54:19 +0500 Subject: [PATCH 1/4] added webhook api --- .code-samples.meilisearch.yaml | 22 +++ src/main/java/com/meilisearch/sdk/Client.java | 59 +++++++ .../java/com/meilisearch/sdk/HttpClient.java | 33 ++-- .../com/meilisearch/sdk/WebHooksHandler.java | 77 +++++++++ .../meilisearch/sdk/json/GsonJsonHandler.java | 2 +- .../sdk/model/CreateUpdateWebhookRequest.java | 23 +++ .../com/meilisearch/sdk/model/Webhook.java | 30 ++++ .../meilisearch/integration/WebhooksTest.java | 146 ++++++++++++++++++ 8 files changed, 376 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/meilisearch/sdk/WebHooksHandler.java create mode 100644 src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java create mode 100644 src/main/java/com/meilisearch/sdk/model/Webhook.java create mode 100644 src/test/java/com/meilisearch/integration/WebhooksTest.java diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index d0afe54d2..ba5a00e18 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -874,3 +874,25 @@ compact_index_1: |- client.index("INDEX_NAME").compact(); rename_an_index_1: |- client.updateIndex("indexA", null, "indexB"); +get_webhooks_1: |- + client.getWebhook(); +get_webhook_1: |- + client.getWebhook(); +create_webhook_1: |- + HashMap headers = new HashMap<>(); + headers.put("authorization", "MASTER_KEY"); + headers.put("referer", "http://example.com"); + CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + + Webhook webhook = client.createWebhook(); +update_webhook_1: |- + Webhook webhook = this.client.createWebhook(webhookReq1); + HashMap headers = new HashMap<>(); + headers.put("referer", null); + CreateUpdateWebhookRequest webhookReq2 = new CreateUpdateWebhookRequest(webhook.getUrl(), headers); + + Webhook updated_webhook = this.client.updateWebhook(webhook.getUuid(), webhookReq2); +delete_webhook_1: |- + this.client.deleteWebhook("WEBHOOK_UUID"); + + diff --git a/src/main/java/com/meilisearch/sdk/Client.java b/src/main/java/com/meilisearch/sdk/Client.java index 888a52dd4..961d26f11 100644 --- a/src/main/java/com/meilisearch/sdk/Client.java +++ b/src/main/java/com/meilisearch/sdk/Client.java @@ -20,6 +20,7 @@ public class Client { private TasksHandler tasksHandler; private KeysHandler keysHandler; private JsonHandler jsonHandler; + private WebHooksHandler webHooksHandler; /** * Calls instance for Meilisearch client @@ -33,6 +34,7 @@ public Client(Config config) { this.tasksHandler = new TasksHandler(config); this.keysHandler = new KeysHandler(config); this.jsonHandler = config.jsonHandler; + this.webHooksHandler = new WebHooksHandler(config); } /** @@ -544,6 +546,63 @@ public String generateTenantToken( return jwtToken; } + /** + * Get a list of all webhooks configured in the current Meilisearch instance. + * + * + * @return List of all webhooks. + * @throws MeilisearchException if an error occurs. + */ + public Results getWebhooks() throws MeilisearchException { + return this.webHooksHandler.getWebhooks(); + } + + /** + * Get a webhook specified by its unique Uuid. + * + * + * @return A single Webhook instance. + * @param webhookUuid Uuid v4 identifier of a webhook. + * @throws MeilisearchException if an error occurs. + */ + public Webhook getWebhook(UUID webhookUuid) throws MeilisearchException { + return this.webHooksHandler.getWebhook(webhookUuid); + } + + /** + * Create a new webhook. When Meilisearch finishes processing a task, + * it sends the relevant task object to all configured webhooks + * + * + * @return A single Webhook instance. + * @param createUpdateWebhookRequest Request body containing headers and url for the new webhook. + * @throws MeilisearchException If an error occurs. + */ + public Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { + return this.webHooksHandler.createWebhook(createUpdateWebhookRequest); + } + + /** + * Update the configuration for the specified webhook. To remove a field, set its value to null. + * + * @param webhook_uuid Uuid v4 identifier of a webhook. + * @param createUpdateWebhookRequest Request body containing new header or url. + * @return A single webhook instance. + * @throws MeilisearchException If an error occurs. + */ + public Webhook updateWebhook(UUID webhook_uuid, CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { + return this.webHooksHandler.updateWebhook(webhook_uuid, createUpdateWebhookRequest); + } + + /** + * Delete a webhook and stop sending task completion data to the target URL. + * @param webhook_uuid Uuid v4 identifier of a webhook. + */ + public void deleteWebhook(UUID webhook_uuid) throws MeilisearchException { + this.webHooksHandler.deleteWebhook(webhook_uuid); + } + + private Boolean isValidUUID(String apiKeyUid) { try { UUID uuid = UUID.fromString(apiKeyUid); diff --git a/src/main/java/com/meilisearch/sdk/HttpClient.java b/src/main/java/com/meilisearch/sdk/HttpClient.java index 4b450036b..4a0f43b3e 100644 --- a/src/main/java/com/meilisearch/sdk/HttpClient.java +++ b/src/main/java/com/meilisearch/sdk/HttpClient.java @@ -11,10 +11,13 @@ import com.meilisearch.sdk.http.response.HttpResponse; import com.meilisearch.sdk.json.GsonJsonHandler; import com.meilisearch.sdk.json.JsonHandler; + import java.util.Collections; import java.util.Map; -/** HTTP client used for API calls to Meilisearch */ +/** + * HTTP client used for API calls to Meilisearch + */ public class HttpClient { private final CustomOkHttpClient client; private final BasicRequest request; @@ -38,7 +41,7 @@ public HttpClient(Config config) { /** * Constructor for the HttpClient * - * @param client HttpClient for making calls to server + * @param client HttpClient for making calls to server * @param request BasicRequest for generating calls to server */ public HttpClient(CustomOkHttpClient client, BasicRequest request) { @@ -57,27 +60,27 @@ public HttpClient(CustomOkHttpClient client, BasicRequest request) { * @throws MeilisearchException if the response is an error */ T get(String api, Class targetClass, Class... parameters) - throws MeilisearchException { + throws MeilisearchException { return this.get(api, "", targetClass, parameters); } /** * Gets the specified resource from the specified path with a given parameter * - * @param api Path to document + * @param api Path to document * @param param Parameter to be passed * @return document that was requested * @throws MeilisearchException if the response is an error */ T get(String api, String param, Class targetClass, Class... parameters) - throws MeilisearchException { + throws MeilisearchException { HttpRequest requestConfig = request.create(HttpMethod.GET, api + param, this.headers, null); HttpResponse httpRequest = this.client.get(requestConfig); HttpResponse httpResponse = response.create(httpRequest, targetClass, parameters); if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -85,20 +88,20 @@ T get(String api, String param, Class targetClass, Class... parameters /** * Adds the specified resource to the specified path * - * @param api Path to server + * @param api Path to server * @param body Query for search * @return results of the search * @throws MeilisearchException if the response is an error */ T post(String api, S body, Class targetClass, Class... parameters) - throws MeilisearchException { + throws MeilisearchException { HttpRequest requestConfig = request.create(HttpMethod.POST, api, this.headers, body); HttpResponse httpRequest = this.client.post(requestConfig); HttpResponse httpResponse = response.create(httpRequest, targetClass, parameters); if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -106,7 +109,7 @@ T post(String api, S body, Class targetClass, Class... parameters) /** * Replaces the specified resource with new data to the specified path * - * @param api Path to the requested resource + * @param api Path to the requested resource * @param body Replacement data for the requested resource * @return updated resource * @throws MeilisearchException if the response is an error @@ -118,7 +121,7 @@ T put(String api, S body, Class targetClass) throws MeilisearchExcepti if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -126,7 +129,7 @@ T put(String api, S body, Class targetClass) throws MeilisearchExcepti /** * Patch the specified resource with new data to the specified path * - * @param api Path to server + * @param api Path to server * @param body Query for search * @return results of the search * @throws MeilisearchException if the response is an error @@ -137,7 +140,7 @@ T patch(String api, S body, Class targetClass) throws MeilisearchExcep if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); + jsonHandler.decode(httpResponse.getContent(), APIError.class)); } return response.create(httpResponse, targetClass).getContent(); @@ -154,11 +157,11 @@ T delete(String api, Class targetClass) throws MeilisearchException { HttpRequest requestConfig = request.create(HttpMethod.DELETE, api, this.headers, null); HttpResponse httpRequest = this.client.delete(requestConfig); HttpResponse httpResponse = response.create(httpRequest, targetClass); - if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); + } } diff --git a/src/main/java/com/meilisearch/sdk/WebHooksHandler.java b/src/main/java/com/meilisearch/sdk/WebHooksHandler.java new file mode 100644 index 000000000..af7e9757c --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/WebHooksHandler.java @@ -0,0 +1,77 @@ +package com.meilisearch.sdk; + +import com.meilisearch.sdk.exceptions.MeilisearchException; +import com.meilisearch.sdk.http.URLBuilder; +import com.meilisearch.sdk.model.CreateUpdateWebhookRequest; +import com.meilisearch.sdk.model.Results; +import com.meilisearch.sdk.model.Webhook; + +import java.util.List; +import java.util.UUID; + + +public class WebHooksHandler { + private final HttpClient httpClient; + + protected WebHooksHandler(Config config) { + this.httpClient = config.httpClient; + } + + /** + * Gets a list of webhooks + * + * @return List of webhooks. + * @throws MeilisearchException if an error occurs + */ + Results getWebhooks() throws MeilisearchException { + return this.httpClient.get(webhooksPath().getURL(), Results.class, Webhook.class); + } + + /** + * Gets a webhook from its uuid + * + * @param uuid Unique identifier of the webhook to get + * @return Meilisearch API response as Webhook instance + * @throws MeilisearchException if an error occurs + */ + Webhook getWebhook(UUID uuid) throws MeilisearchException { + return this.httpClient.get(webhooksPath().addSubroute(uuid.toString()).getURL(), Webhook.class); + } + + /** + * Create a new webhook + * + * @param createUpdateWebhookRequest Request body for creating a new webhook + * @return Meilisearch API response as Webhook instance + * @throws MeilisearchException if an error occurs + */ + Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { + return this.httpClient.post(webhooksPath().getURL(), createUpdateWebhookRequest, Webhook.class); + } + + /** + * Update a webhook + * + * @param webhookUuid Unique identifier of a webhook to update + * @param createUpdateWebhookRequest Request body for updating a webhook + * @return Meilisearch API response as Webhook instance + * @throws MeilisearchException if an error occurs + */ + Webhook updateWebhook(UUID webhookUuid, CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { + return this.httpClient.patch(webhooksPath().addSubroute(webhookUuid.toString()).getURL(), createUpdateWebhookRequest, Webhook.class); + } + + /** + * Delete a webhook + * + * @param webhookUuid Unique identifier of a webhook to update + * @throws MeilisearchException if an error occurs + */ + void deleteWebhook(UUID webhookUuid) throws MeilisearchException { + this.httpClient.delete(webhooksPath().addSubroute(webhookUuid.toString()).getURL(), String.class); + } + + private URLBuilder webhooksPath() { + return new URLBuilder("/webhooks"); + } +} diff --git a/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java b/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java index d798dfe73..049f7c965 100644 --- a/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java +++ b/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java @@ -20,7 +20,7 @@ public GsonJsonHandler() { builder.registerTypeAdapter( FilterableAttributesConfig.class, new GsonFilterableAttributesConfigTypeAdapter()); builder.registerTypeAdapterFactory(new GsonTaskDetailsTypeAdapterFactory()); - this.gson = builder.create(); + this.gson = builder.serializeNulls().create(); } @Override diff --git a/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java b/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java new file mode 100644 index 000000000..dc3c1156f --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java @@ -0,0 +1,23 @@ +package com.meilisearch.sdk.model; + + +import lombok.NonNull; + +import java.io.Serializable; +import java.util.HashMap; + +/** + * Data structure used in request body while creating or updating a webhook. + * + * @see API Specification + */ + +public class CreateUpdateWebhookRequest implements Serializable { + final String url; + final HashMap headers; + + public CreateUpdateWebhookRequest(@NonNull String url, @NonNull HashMap headers) { + this.url = url; + this.headers = headers; + } +} diff --git a/src/main/java/com/meilisearch/sdk/model/Webhook.java b/src/main/java/com/meilisearch/sdk/model/Webhook.java new file mode 100644 index 000000000..6a6a68de7 --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/model/Webhook.java @@ -0,0 +1,30 @@ +package com.meilisearch.sdk.model; + + +import lombok.Getter; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.UUID; + +/** + * Webhook data structure. + * + * @see API + * specification + */ +public class Webhook implements Serializable { + @Getter protected final UUID uuid; + @Getter protected final String url; + @Getter protected final HashMap headers; + @Getter protected final boolean isEditable; + + public Webhook(UUID uuid, String url, HashMap headers, boolean isEditable) { + this.uuid = uuid; + this.url = url; + this.headers = headers; + this.isEditable = isEditable; + } + +} + diff --git a/src/test/java/com/meilisearch/integration/WebhooksTest.java b/src/test/java/com/meilisearch/integration/WebhooksTest.java new file mode 100644 index 000000000..ba03adfd2 --- /dev/null +++ b/src/test/java/com/meilisearch/integration/WebhooksTest.java @@ -0,0 +1,146 @@ +package com.meilisearch.integration; + +import com.meilisearch.integration.classes.AbstractIT; +import com.meilisearch.sdk.model.CreateUpdateWebhookRequest; +import com.meilisearch.sdk.model.Results; +import com.meilisearch.sdk.model.Webhook; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.TimeZone; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +@Tag("integration") +public class WebhooksTest extends AbstractIT { + + @BeforeEach + public void initialize() { + this.setUp(); + this.setUpJacksonClient(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + @AfterAll + static void cleanMeilisearch() { + cleanup(); + } + + void cleanUp() { + Results webhooks = client.getWebhooks(); + for (Webhook wb : webhooks.getResults()) { + client.deleteWebhook(wb.getUuid()); + } + } + + @Test + public void testCreateWebhook() throws Exception { + HashMap headers = new HashMap<>(); + headers.put("authorization", "MASTER_KEY"); + headers.put("referer", "http://example.com"); + CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + + Webhook webhook = this.client.createWebhook(webhookReq1); + + assertThat(webhook, is(instanceOf(Webhook.class))); + assertThat(webhook.getUuid(), is(notNullValue())); + assertThat(webhook.getHeaders(), is(notNullValue())); + assertThat(webhook.getUrl(), is(notNullValue())); + assertThat(webhook.isEditable(), is(notNullValue())); + cleanUp(); + } + + @Test + public void testGetWebhooks() throws Exception { + HashMap headers = new HashMap<>(); + headers.put("authorization", "MASTER_KEY"); + headers.put("referer", "http://example.com"); + CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + CreateUpdateWebhookRequest webhookReq2 = new CreateUpdateWebhookRequest("http://webiste1.com", headers); + + this.client.createWebhook(webhookReq1); + this.client.createWebhook(webhookReq2); + + Results webhooks = client.getWebhooks(); + Webhook webhook = webhooks.getResults()[0]; + Webhook webhook1 = webhooks.getResults()[1]; + + assertThat(webhook, is(instanceOf(Webhook.class))); + assertThat(webhook.getUuid(), is(notNullValue())); + assertThat(webhook.getHeaders(), is(notNullValue())); + assertThat(webhook.getUrl(), is(notNullValue())); + assertThat(webhook.isEditable(), is(notNullValue())); + + assertThat(webhook1, is(instanceOf(Webhook.class))); + assertThat(webhook1.getUuid(), is(notNullValue())); + assertThat(webhook1.getHeaders(), is(notNullValue())); + assertThat(webhook1.getUrl(), is(notNullValue())); + assertThat(webhook1.isEditable(), is(notNullValue())); + cleanUp(); + } + + @Test + public void testGetWebhook() throws Exception { + HashMap headers = new HashMap<>(); + headers.put("authorization", "MASTER_KEY"); + headers.put("referer", "http://example.com"); + CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + + Webhook webhook = this.client.createWebhook(webhookReq1); + + Webhook webhook_res = client.getWebhook(webhook.getUuid()); + + + assertThat(webhook_res, is(instanceOf(Webhook.class))); + assertThat(webhook_res.getUuid(), is(notNullValue())); + assertThat(webhook_res.getHeaders(), is(notNullValue())); + assertThat(webhook_res.getUrl(), is(notNullValue())); + assertThat(webhook_res.isEditable(), is(notNullValue())); + cleanUp(); + } + + @Test + public void testUpdateWebhook() throws Exception { + HashMap headers = new HashMap<>(); + headers.put("authorization", "MASTER_KEY"); + headers.put("referer", "http://example.com"); + CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + Webhook webhook = this.client.createWebhook(webhookReq1); + + headers.put("referer", null); + CreateUpdateWebhookRequest webhookReq2 = new CreateUpdateWebhookRequest(webhook.getUrl(), headers); + + Webhook updated_webhook = this.client.updateWebhook(webhook.getUuid(), webhookReq2); + + assertThat(updated_webhook, is(instanceOf(Webhook.class))); + assertThat(updated_webhook.getUuid(), is(notNullValue())); + assertThat(updated_webhook.getHeaders(), is(notNullValue())); + assertThat(updated_webhook.getUrl(), is(notNullValue())); + assertThat(updated_webhook.isEditable(), is(notNullValue())); + assertThat(updated_webhook.getHeaders(), is(aMapWithSize(1))); + cleanUp(); + } + + @Test + public void testDeleteWebhook() throws Exception { + HashMap headers = new HashMap<>(); + headers.put("authorization", "MASTER_KEY"); + headers.put("referer", "http://example.com"); + CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + Webhook webhook = this.client.createWebhook(webhookReq1); + + + this.client.deleteWebhook(webhook.getUuid()); + + List webhooks = Arrays.asList(this.client.getWebhooks().getResults()); + + assertThat(webhooks, hasSize(0)); + cleanUp(); + } +} From d43bd5956f2ec461ebdab055620755da51d0f570 Mon Sep 17 00:00:00 2001 From: Strift Date: Tue, 17 Feb 2026 13:24:27 +0800 Subject: [PATCH 2/4] Lint --- src/main/java/com/meilisearch/sdk/Client.java | 19 +++++----- .../java/com/meilisearch/sdk/HttpClient.java | 32 +++++++--------- .../com/meilisearch/sdk/WebHooksHandler.java | 25 +++++++----- .../sdk/model/CreateUpdateWebhookRequest.java | 8 ++-- .../com/meilisearch/sdk/model/Webhook.java | 9 +---- .../meilisearch/integration/WebhooksTest.java | 38 ++++++++++--------- 6 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/Client.java b/src/main/java/com/meilisearch/sdk/Client.java index 961d26f11..356817f44 100644 --- a/src/main/java/com/meilisearch/sdk/Client.java +++ b/src/main/java/com/meilisearch/sdk/Client.java @@ -549,7 +549,6 @@ public String generateTenantToken( /** * Get a list of all webhooks configured in the current Meilisearch instance. * - * * @return List of all webhooks. * @throws MeilisearchException if an error occurs. */ @@ -560,7 +559,6 @@ public Results getWebhooks() throws MeilisearchException { /** * Get a webhook specified by its unique Uuid. * - * * @return A single Webhook instance. * @param webhookUuid Uuid v4 identifier of a webhook. * @throws MeilisearchException if an error occurs. @@ -570,15 +568,16 @@ public Webhook getWebhook(UUID webhookUuid) throws MeilisearchException { } /** - * Create a new webhook. When Meilisearch finishes processing a task, - * it sends the relevant task object to all configured webhooks - * + * Create a new webhook. When Meilisearch finishes processing a task, it sends the relevant task + * object to all configured webhooks * * @return A single Webhook instance. - * @param createUpdateWebhookRequest Request body containing headers and url for the new webhook. + * @param createUpdateWebhookRequest Request body containing headers and url for the new + * webhook. * @throws MeilisearchException If an error occurs. */ - public Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { + public Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) + throws MeilisearchException { return this.webHooksHandler.createWebhook(createUpdateWebhookRequest); } @@ -590,19 +589,21 @@ public Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookReque * @return A single webhook instance. * @throws MeilisearchException If an error occurs. */ - public Webhook updateWebhook(UUID webhook_uuid, CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { + public Webhook updateWebhook( + UUID webhook_uuid, CreateUpdateWebhookRequest createUpdateWebhookRequest) + throws MeilisearchException { return this.webHooksHandler.updateWebhook(webhook_uuid, createUpdateWebhookRequest); } /** * Delete a webhook and stop sending task completion data to the target URL. + * * @param webhook_uuid Uuid v4 identifier of a webhook. */ public void deleteWebhook(UUID webhook_uuid) throws MeilisearchException { this.webHooksHandler.deleteWebhook(webhook_uuid); } - private Boolean isValidUUID(String apiKeyUid) { try { UUID uuid = UUID.fromString(apiKeyUid); diff --git a/src/main/java/com/meilisearch/sdk/HttpClient.java b/src/main/java/com/meilisearch/sdk/HttpClient.java index 4a0f43b3e..f6474d533 100644 --- a/src/main/java/com/meilisearch/sdk/HttpClient.java +++ b/src/main/java/com/meilisearch/sdk/HttpClient.java @@ -11,13 +11,10 @@ import com.meilisearch.sdk.http.response.HttpResponse; import com.meilisearch.sdk.json.GsonJsonHandler; import com.meilisearch.sdk.json.JsonHandler; - import java.util.Collections; import java.util.Map; -/** - * HTTP client used for API calls to Meilisearch - */ +/** HTTP client used for API calls to Meilisearch */ public class HttpClient { private final CustomOkHttpClient client; private final BasicRequest request; @@ -41,7 +38,7 @@ public HttpClient(Config config) { /** * Constructor for the HttpClient * - * @param client HttpClient for making calls to server + * @param client HttpClient for making calls to server * @param request BasicRequest for generating calls to server */ public HttpClient(CustomOkHttpClient client, BasicRequest request) { @@ -60,27 +57,27 @@ public HttpClient(CustomOkHttpClient client, BasicRequest request) { * @throws MeilisearchException if the response is an error */ T get(String api, Class targetClass, Class... parameters) - throws MeilisearchException { + throws MeilisearchException { return this.get(api, "", targetClass, parameters); } /** * Gets the specified resource from the specified path with a given parameter * - * @param api Path to document + * @param api Path to document * @param param Parameter to be passed * @return document that was requested * @throws MeilisearchException if the response is an error */ T get(String api, String param, Class targetClass, Class... parameters) - throws MeilisearchException { + throws MeilisearchException { HttpRequest requestConfig = request.create(HttpMethod.GET, api + param, this.headers, null); HttpResponse httpRequest = this.client.get(requestConfig); HttpResponse httpResponse = response.create(httpRequest, targetClass, parameters); if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -88,20 +85,20 @@ T get(String api, String param, Class targetClass, Class... parameters /** * Adds the specified resource to the specified path * - * @param api Path to server + * @param api Path to server * @param body Query for search * @return results of the search * @throws MeilisearchException if the response is an error */ T post(String api, S body, Class targetClass, Class... parameters) - throws MeilisearchException { + throws MeilisearchException { HttpRequest requestConfig = request.create(HttpMethod.POST, api, this.headers, body); HttpResponse httpRequest = this.client.post(requestConfig); HttpResponse httpResponse = response.create(httpRequest, targetClass, parameters); if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -109,7 +106,7 @@ T post(String api, S body, Class targetClass, Class... parameters) /** * Replaces the specified resource with new data to the specified path * - * @param api Path to the requested resource + * @param api Path to the requested resource * @param body Replacement data for the requested resource * @return updated resource * @throws MeilisearchException if the response is an error @@ -121,7 +118,7 @@ T put(String api, S body, Class targetClass) throws MeilisearchExcepti if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); } @@ -129,7 +126,7 @@ T put(String api, S body, Class targetClass) throws MeilisearchExcepti /** * Patch the specified resource with new data to the specified path * - * @param api Path to server + * @param api Path to server * @param body Query for search * @return results of the search * @throws MeilisearchException if the response is an error @@ -140,7 +137,7 @@ T patch(String api, S body, Class targetClass) throws MeilisearchExcep if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); + jsonHandler.decode(httpResponse.getContent(), APIError.class)); } return response.create(httpResponse, targetClass).getContent(); @@ -159,9 +156,8 @@ T delete(String api, Class targetClass) throws MeilisearchException { HttpResponse httpResponse = response.create(httpRequest, targetClass); if (httpResponse.getStatusCode() >= 400) { throw new MeilisearchApiException( - jsonHandler.decode(httpRequest.getContent(), APIError.class)); + jsonHandler.decode(httpRequest.getContent(), APIError.class)); } return httpResponse.getContent(); - } } diff --git a/src/main/java/com/meilisearch/sdk/WebHooksHandler.java b/src/main/java/com/meilisearch/sdk/WebHooksHandler.java index af7e9757c..4b0b376d0 100644 --- a/src/main/java/com/meilisearch/sdk/WebHooksHandler.java +++ b/src/main/java/com/meilisearch/sdk/WebHooksHandler.java @@ -5,11 +5,8 @@ import com.meilisearch.sdk.model.CreateUpdateWebhookRequest; import com.meilisearch.sdk.model.Results; import com.meilisearch.sdk.model.Webhook; - -import java.util.List; import java.util.UUID; - public class WebHooksHandler { private final HttpClient httpClient; @@ -35,7 +32,8 @@ Results getWebhooks() throws MeilisearchException { * @throws MeilisearchException if an error occurs */ Webhook getWebhook(UUID uuid) throws MeilisearchException { - return this.httpClient.get(webhooksPath().addSubroute(uuid.toString()).getURL(), Webhook.class); + return this.httpClient.get( + webhooksPath().addSubroute(uuid.toString()).getURL(), Webhook.class); } /** @@ -45,20 +43,26 @@ Webhook getWebhook(UUID uuid) throws MeilisearchException { * @return Meilisearch API response as Webhook instance * @throws MeilisearchException if an error occurs */ - Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { - return this.httpClient.post(webhooksPath().getURL(), createUpdateWebhookRequest, Webhook.class); + Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) + throws MeilisearchException { + return this.httpClient.post( + webhooksPath().getURL(), createUpdateWebhookRequest, Webhook.class); } /** * Update a webhook * - * @param webhookUuid Unique identifier of a webhook to update + * @param webhookUuid Unique identifier of a webhook to update * @param createUpdateWebhookRequest Request body for updating a webhook * @return Meilisearch API response as Webhook instance * @throws MeilisearchException if an error occurs */ - Webhook updateWebhook(UUID webhookUuid, CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException { - return this.httpClient.patch(webhooksPath().addSubroute(webhookUuid.toString()).getURL(), createUpdateWebhookRequest, Webhook.class); + Webhook updateWebhook(UUID webhookUuid, CreateUpdateWebhookRequest createUpdateWebhookRequest) + throws MeilisearchException { + return this.httpClient.patch( + webhooksPath().addSubroute(webhookUuid.toString()).getURL(), + createUpdateWebhookRequest, + Webhook.class); } /** @@ -68,7 +72,8 @@ Webhook updateWebhook(UUID webhookUuid, CreateUpdateWebhookRequest createUpdateW * @throws MeilisearchException if an error occurs */ void deleteWebhook(UUID webhookUuid) throws MeilisearchException { - this.httpClient.delete(webhooksPath().addSubroute(webhookUuid.toString()).getURL(), String.class); + this.httpClient.delete( + webhooksPath().addSubroute(webhookUuid.toString()).getURL(), String.class); } private URLBuilder webhooksPath() { diff --git a/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java b/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java index dc3c1156f..0d4b5851b 100644 --- a/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java +++ b/src/main/java/com/meilisearch/sdk/model/CreateUpdateWebhookRequest.java @@ -1,22 +1,20 @@ package com.meilisearch.sdk.model; - -import lombok.NonNull; - import java.io.Serializable; import java.util.HashMap; +import lombok.NonNull; /** * Data structure used in request body while creating or updating a webhook. * * @see API Specification */ - public class CreateUpdateWebhookRequest implements Serializable { final String url; final HashMap headers; - public CreateUpdateWebhookRequest(@NonNull String url, @NonNull HashMap headers) { + public CreateUpdateWebhookRequest( + @NonNull String url, @NonNull HashMap headers) { this.url = url; this.headers = headers; } diff --git a/src/main/java/com/meilisearch/sdk/model/Webhook.java b/src/main/java/com/meilisearch/sdk/model/Webhook.java index 6a6a68de7..ed832af95 100644 --- a/src/main/java/com/meilisearch/sdk/model/Webhook.java +++ b/src/main/java/com/meilisearch/sdk/model/Webhook.java @@ -1,17 +1,14 @@ package com.meilisearch.sdk.model; - -import lombok.Getter; - import java.io.Serializable; import java.util.HashMap; import java.util.UUID; +import lombok.Getter; /** * Webhook data structure. * - * @see API - * specification + * @see API specification */ public class Webhook implements Serializable { @Getter protected final UUID uuid; @@ -25,6 +22,4 @@ public Webhook(UUID uuid, String url, HashMap headers, boolean i this.headers = headers; this.isEditable = isEditable; } - } - diff --git a/src/test/java/com/meilisearch/integration/WebhooksTest.java b/src/test/java/com/meilisearch/integration/WebhooksTest.java index ba03adfd2..851d34212 100644 --- a/src/test/java/com/meilisearch/integration/WebhooksTest.java +++ b/src/test/java/com/meilisearch/integration/WebhooksTest.java @@ -1,21 +1,20 @@ package com.meilisearch.integration; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + import com.meilisearch.integration.classes.AbstractIT; import com.meilisearch.sdk.model.CreateUpdateWebhookRequest; import com.meilisearch.sdk.model.Results; import com.meilisearch.sdk.model.Webhook; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; - import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.TimeZone; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; +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 WebhooksTest extends AbstractIT { @@ -44,7 +43,8 @@ public void testCreateWebhook() throws Exception { HashMap headers = new HashMap<>(); headers.put("authorization", "MASTER_KEY"); headers.put("referer", "http://example.com"); - CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + CreateUpdateWebhookRequest webhookReq1 = + new CreateUpdateWebhookRequest("http://webiste.com", headers); Webhook webhook = this.client.createWebhook(webhookReq1); @@ -61,8 +61,10 @@ public void testGetWebhooks() throws Exception { HashMap headers = new HashMap<>(); headers.put("authorization", "MASTER_KEY"); headers.put("referer", "http://example.com"); - CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); - CreateUpdateWebhookRequest webhookReq2 = new CreateUpdateWebhookRequest("http://webiste1.com", headers); + CreateUpdateWebhookRequest webhookReq1 = + new CreateUpdateWebhookRequest("http://webiste.com", headers); + CreateUpdateWebhookRequest webhookReq2 = + new CreateUpdateWebhookRequest("http://webiste1.com", headers); this.client.createWebhook(webhookReq1); this.client.createWebhook(webhookReq2); @@ -90,13 +92,13 @@ public void testGetWebhook() throws Exception { HashMap headers = new HashMap<>(); headers.put("authorization", "MASTER_KEY"); headers.put("referer", "http://example.com"); - CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + CreateUpdateWebhookRequest webhookReq1 = + new CreateUpdateWebhookRequest("http://webiste.com", headers); Webhook webhook = this.client.createWebhook(webhookReq1); Webhook webhook_res = client.getWebhook(webhook.getUuid()); - assertThat(webhook_res, is(instanceOf(Webhook.class))); assertThat(webhook_res.getUuid(), is(notNullValue())); assertThat(webhook_res.getHeaders(), is(notNullValue())); @@ -110,11 +112,13 @@ public void testUpdateWebhook() throws Exception { HashMap headers = new HashMap<>(); headers.put("authorization", "MASTER_KEY"); headers.put("referer", "http://example.com"); - CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + CreateUpdateWebhookRequest webhookReq1 = + new CreateUpdateWebhookRequest("http://webiste.com", headers); Webhook webhook = this.client.createWebhook(webhookReq1); headers.put("referer", null); - CreateUpdateWebhookRequest webhookReq2 = new CreateUpdateWebhookRequest(webhook.getUrl(), headers); + CreateUpdateWebhookRequest webhookReq2 = + new CreateUpdateWebhookRequest(webhook.getUrl(), headers); Webhook updated_webhook = this.client.updateWebhook(webhook.getUuid(), webhookReq2); @@ -132,10 +136,10 @@ public void testDeleteWebhook() throws Exception { HashMap headers = new HashMap<>(); headers.put("authorization", "MASTER_KEY"); headers.put("referer", "http://example.com"); - CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers); + CreateUpdateWebhookRequest webhookReq1 = + new CreateUpdateWebhookRequest("http://webiste.com", headers); Webhook webhook = this.client.createWebhook(webhookReq1); - this.client.deleteWebhook(webhook.getUuid()); List webhooks = Arrays.asList(this.client.getWebhooks().getResults()); From 53a24afea51b44be70e850fd38e91e09ab1389a9 Mon Sep 17 00:00:00 2001 From: Strift Date: Tue, 17 Feb 2026 13:27:20 +0800 Subject: [PATCH 3/4] Remove extra empty lines --- .code-samples.meilisearch.yaml | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index ba5a00e18..77edfcc83 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -272,8 +272,7 @@ getting_started_typo_tolerance: |- typoTolerance.setMinWordSizeForTypos(minWordSizeTypos); client.index("movies").updateTypoToleranceSettings(typoTolerance); -get_typo_tolerance_1: - client.index("books").getTypoToleranceSettings(); +get_typo_tolerance_1: client.index("books").getTypoToleranceSettings(); update_typo_tolerance_1: |- TypoTolerance typoTolerance = new TypoTolerance(); HashMap minWordSizeTypos = @@ -323,7 +322,7 @@ update_non_separator_tokens_1: |- String[] newSeparatorTokens = { "@", "#" }; client.index("articles").updateNonSeparatorTokensSettings(newSeparatorTokens); reset_non_separator_tokens_1: |- - client.index("articles").resetNonSeparatorTokensSettings(); + client.index("articles").resetNonSeparatorTokensSettings(); get_dictionary_1: |- client.index("books").getDictionarySettings(); update_dictionary_1: |- @@ -609,9 +608,9 @@ search_parameter_guide_facet_stats_1: |- faceted_search_update_settings_1: client.index("movie_ratings").updateFilterableAttributesSettings(new String[] { - "genres", - "director", - "language" + "genres", + "director", + "language" }); faceted_search_walkthrough_filter_1: |- SearchRequest searchRequest = @@ -835,10 +834,9 @@ multi_search_1: |- multiIndexSearch.addQuery(new IndexSearchRequest("movie_ratings").setQuery("us")); client.multiSearch(multiSearchRequest); -get_similar_post_1: - SimilarDocumentRequest query = new SimilarDocumentRequest() - .setId("143") - .setEmbedder("manual"); +get_similar_post_1: SimilarDocumentRequest query = new SimilarDocumentRequest() + .setId("143") + .setEmbedder("manual"); client.index("movies").searchSimilarDocuments(query) search_parameter_reference_distinct_1: |- SearchRequest searchRequest = SearchRequest.builder().q("QUERY TERMS").distinct("ATTRIBUTE_A").build(); @@ -894,5 +892,3 @@ update_webhook_1: |- Webhook updated_webhook = this.client.updateWebhook(webhook.getUuid(), webhookReq2); delete_webhook_1: |- this.client.deleteWebhook("WEBHOOK_UUID"); - - From 2f45138530a074fdc9eea1841d26ad2ecb86404a Mon Sep 17 00:00:00 2001 From: Ali Tariq Date: Tue, 17 Feb 2026 14:42:13 +0500 Subject: [PATCH 4/4] fixed ci error --- src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java b/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java index 049f7c965..d798dfe73 100644 --- a/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java +++ b/src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java @@ -20,7 +20,7 @@ public GsonJsonHandler() { builder.registerTypeAdapter( FilterableAttributesConfig.class, new GsonFilterableAttributesConfigTypeAdapter()); builder.registerTypeAdapterFactory(new GsonTaskDetailsTypeAdapterFactory()); - this.gson = builder.serializeNulls().create(); + this.gson = builder.create(); } @Override