Skip to content

Commit 1b9bfa6

Browse files
authored
Merge pull request #934 from valkrypton/add/webhooks-api
added webhook api
2 parents d74c158 + 21fee6e commit 1b9bfa6

7 files changed

Lines changed: 377 additions & 9 deletions

File tree

.code-samples.meilisearch.yaml

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ update_displayed_attributes_1: |-
260260
});
261261
reset_displayed_attributes_1: |-
262262
client.index("movies").resetDisplayedAttributesSettings();
263+
getting_started_typo_tolerance: |-
264+
HashMap<String, Integer> minWordSizeTypos =
265+
new HashMap<String, Integer>() {
266+
{
267+
put("oneTypo", 4);
268+
}
269+
};
270+
271+
TypoTolerance typoTolerance = new TypoTolerance();
272+
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
273+
274+
client.index("movies").updateTypoToleranceSettings(typoTolerance);
263275
get_typo_tolerance_1:
264276
client.index("books").getTypoToleranceSettings();
265277
update_typo_tolerance_1: |-
@@ -311,7 +323,7 @@ update_non_separator_tokens_1: |-
311323
String[] newSeparatorTokens = { "@", "#" };
312324
client.index("articles").updateNonSeparatorTokensSettings(newSeparatorTokens);
313325
reset_non_separator_tokens_1: |-
314-
client.index("articles").resetNonSeparatorTokensSettings();
326+
client.index("articles").resetNonSeparatorTokensSettings();
315327
get_dictionary_1: |-
316328
client.index("books").getDictionarySettings();
317329
update_dictionary_1: |-
@@ -511,9 +523,9 @@ search_parameter_guide_facet_stats_1: |-
511523
faceted_search_update_settings_1:
512524
client.index("movie_ratings").updateFilterableAttributesSettings(new String[]
513525
{
514-
"genres",
515-
"director",
516-
"language"
526+
"genres",
527+
"director",
528+
"language"
517529
});
518530
faceted_search_walkthrough_filter_1: |-
519531
SearchRequest searchRequest =
@@ -681,10 +693,9 @@ multi_search_1: |-
681693
multiIndexSearch.addQuery(new IndexSearchRequest("movie_ratings").setQuery("us"));
682694
683695
client.multiSearch(multiSearchRequest);
684-
get_similar_post_1:
685-
SimilarDocumentRequest query = new SimilarDocumentRequest()
686-
.setId("143")
687-
.setEmbedder("manual");
696+
get_similar_post_1: SimilarDocumentRequest query = new SimilarDocumentRequest()
697+
.setId("143")
698+
.setEmbedder("manual");
688699
client.index("movies").searchSimilarDocuments(query)
689700
search_parameter_reference_distinct_1: |-
690701
SearchRequest searchRequest = SearchRequest.builder().q("QUERY TERMS").distinct("ATTRIBUTE_A").build();
@@ -720,3 +731,23 @@ compact_index_1: |-
720731
client.index("INDEX_NAME").compact();
721732
rename_an_index_1: |-
722733
client.updateIndex("indexA", null, "indexB");
734+
get_webhooks_1: |-
735+
client.getWebhook();
736+
get_webhook_1: |-
737+
client.getWebhook();
738+
create_webhook_1: |-
739+
HashMap<String, Object> headers = new HashMap<>();
740+
headers.put("authorization", "MASTER_KEY");
741+
headers.put("referer", "http://example.com");
742+
CreateUpdateWebhookRequest webhookReq1 = new CreateUpdateWebhookRequest("http://webiste.com", headers);
743+
744+
Webhook webhook = client.createWebhook();
745+
update_webhook_1: |-
746+
Webhook webhook = this.client.createWebhook(webhookReq1);
747+
HashMap<String, Object> headers = new HashMap<>();
748+
headers.put("referer", null);
749+
CreateUpdateWebhookRequest webhookReq2 = new CreateUpdateWebhookRequest(webhook.getUrl(), headers);
750+
751+
Webhook updated_webhook = this.client.updateWebhook(webhook.getUuid(), webhookReq2);
752+
delete_webhook_1: |-
753+
this.client.deleteWebhook("WEBHOOK_UUID");

src/main/java/com/meilisearch/sdk/Client.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Client {
2020
private TasksHandler tasksHandler;
2121
private KeysHandler keysHandler;
2222
private JsonHandler jsonHandler;
23+
private WebHooksHandler webHooksHandler;
2324

2425
/**
2526
* Calls instance for Meilisearch client
@@ -33,6 +34,7 @@ public Client(Config config) {
3334
this.tasksHandler = new TasksHandler(config);
3435
this.keysHandler = new KeysHandler(config);
3536
this.jsonHandler = config.jsonHandler;
37+
this.webHooksHandler = new WebHooksHandler(config);
3638
}
3739

3840
/**
@@ -544,6 +546,64 @@ public String generateTenantToken(
544546
return jwtToken;
545547
}
546548

549+
/**
550+
* Get a list of all webhooks configured in the current Meilisearch instance.
551+
*
552+
* @return List of all webhooks.
553+
* @throws MeilisearchException if an error occurs.
554+
*/
555+
public Results<Webhook> getWebhooks() throws MeilisearchException {
556+
return this.webHooksHandler.getWebhooks();
557+
}
558+
559+
/**
560+
* Get a webhook specified by its unique Uuid.
561+
*
562+
* @return A single Webhook instance.
563+
* @param webhookUuid Uuid v4 identifier of a webhook.
564+
* @throws MeilisearchException if an error occurs.
565+
*/
566+
public Webhook getWebhook(UUID webhookUuid) throws MeilisearchException {
567+
return this.webHooksHandler.getWebhook(webhookUuid);
568+
}
569+
570+
/**
571+
* Create a new webhook. When Meilisearch finishes processing a task, it sends the relevant task
572+
* object to all configured webhooks
573+
*
574+
* @return A single Webhook instance.
575+
* @param createUpdateWebhookRequest Request body containing headers and url for the new
576+
* webhook.
577+
* @throws MeilisearchException If an error occurs.
578+
*/
579+
public Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest)
580+
throws MeilisearchException {
581+
return this.webHooksHandler.createWebhook(createUpdateWebhookRequest);
582+
}
583+
584+
/**
585+
* Update the configuration for the specified webhook. To remove a field, set its value to null.
586+
*
587+
* @param webhookUuid Uuid v4 identifier of a webhook.
588+
* @param createUpdateWebhookRequest Request body containing new header or url.
589+
* @return A single webhook instance.
590+
* @throws MeilisearchException If an error occurs.
591+
*/
592+
public Webhook updateWebhook(
593+
UUID webhookUuid, CreateUpdateWebhookRequest createUpdateWebhookRequest)
594+
throws MeilisearchException {
595+
return this.webHooksHandler.updateWebhook(webhookUuid, createUpdateWebhookRequest);
596+
}
597+
598+
/**
599+
* Delete a webhook and stop sending task completion data to the target URL.
600+
*
601+
* @param webhookUuid Uuid v4 identifier of a webhook.
602+
*/
603+
public void deleteWebhook(UUID webhookUuid) throws MeilisearchException {
604+
this.webHooksHandler.deleteWebhook(webhookUuid);
605+
}
606+
547607
private Boolean isValidUUID(String apiKeyUid) {
548608
try {
549609
UUID uuid = UUID.fromString(apiKeyUid);

src/main/java/com/meilisearch/sdk/HttpClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ <T> T delete(String api, Class<T> targetClass) throws MeilisearchException {
154154
HttpRequest requestConfig = request.create(HttpMethod.DELETE, api, this.headers, null);
155155
HttpResponse<T> httpRequest = this.client.delete(requestConfig);
156156
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
157-
158157
if (httpResponse.getStatusCode() >= 400) {
159158
throw new MeilisearchApiException(
160159
jsonHandler.decode(httpRequest.getContent(), APIError.class));
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.meilisearch.sdk;
2+
3+
import com.meilisearch.sdk.exceptions.MeilisearchException;
4+
import com.meilisearch.sdk.http.URLBuilder;
5+
import com.meilisearch.sdk.model.CreateUpdateWebhookRequest;
6+
import com.meilisearch.sdk.model.Results;
7+
import com.meilisearch.sdk.model.Webhook;
8+
import java.util.UUID;
9+
10+
public class WebHooksHandler {
11+
private final HttpClient httpClient;
12+
13+
protected WebHooksHandler(Config config) {
14+
this.httpClient = config.httpClient;
15+
}
16+
17+
/**
18+
* Gets a list of webhooks
19+
*
20+
* @return List of webhooks.
21+
* @throws MeilisearchException if an error occurs
22+
*/
23+
Results<Webhook> getWebhooks() throws MeilisearchException {
24+
return this.httpClient.get(webhooksPath().getURL(), Results.class, Webhook.class);
25+
}
26+
27+
/**
28+
* Gets a webhook from its uuid
29+
*
30+
* @param uuid Unique identifier of the webhook to get
31+
* @return Meilisearch API response as Webhook instance
32+
* @throws MeilisearchException if an error occurs
33+
*/
34+
Webhook getWebhook(UUID uuid) throws MeilisearchException {
35+
return this.httpClient.get(
36+
webhooksPath().addSubroute(uuid.toString()).getURL(), Webhook.class);
37+
}
38+
39+
/**
40+
* Create a new webhook
41+
*
42+
* @param createUpdateWebhookRequest Request body for creating a new webhook
43+
* @return Meilisearch API response as Webhook instance
44+
* @throws MeilisearchException if an error occurs
45+
*/
46+
Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest)
47+
throws MeilisearchException {
48+
return this.httpClient.post(
49+
webhooksPath().getURL(), createUpdateWebhookRequest, Webhook.class);
50+
}
51+
52+
/**
53+
* Update a webhook
54+
*
55+
* @param webhookUuid Unique identifier of a webhook to update
56+
* @param createUpdateWebhookRequest Request body for updating a webhook
57+
* @return Meilisearch API response as Webhook instance
58+
* @throws MeilisearchException if an error occurs
59+
*/
60+
Webhook updateWebhook(UUID webhookUuid, CreateUpdateWebhookRequest createUpdateWebhookRequest)
61+
throws MeilisearchException {
62+
return this.httpClient.patch(
63+
webhooksPath().addSubroute(webhookUuid.toString()).getURL(),
64+
createUpdateWebhookRequest,
65+
Webhook.class);
66+
}
67+
68+
/**
69+
* Delete a webhook
70+
*
71+
* @param webhookUuid Unique identifier of a webhook to update
72+
* @throws MeilisearchException if an error occurs
73+
*/
74+
void deleteWebhook(UUID webhookUuid) throws MeilisearchException {
75+
this.httpClient.delete(
76+
webhooksPath().addSubroute(webhookUuid.toString()).getURL(), String.class);
77+
}
78+
79+
private URLBuilder webhooksPath() {
80+
return new URLBuilder("/webhooks");
81+
}
82+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.meilisearch.sdk.model;
2+
3+
import java.io.Serializable;
4+
import java.util.HashMap;
5+
import lombok.NonNull;
6+
7+
/**
8+
* Data structure used in request body while creating or updating a webhook.
9+
*
10+
* @see <a href="https://www.meilisearch.com/docs/reference/api/webhooks">API Specification</a>
11+
*/
12+
public class CreateUpdateWebhookRequest implements Serializable {
13+
final String url;
14+
final HashMap<String, Object> headers;
15+
16+
public CreateUpdateWebhookRequest(
17+
@NonNull String url, @NonNull HashMap<String, Object> headers) {
18+
this.url = url;
19+
this.headers = headers;
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.meilisearch.sdk.model;
2+
3+
import java.io.Serializable;
4+
import java.util.HashMap;
5+
import java.util.UUID;
6+
import lombok.Getter;
7+
8+
/**
9+
* Webhook data structure.
10+
*
11+
* @see <a href="https://www.meilisearch.com/docs/reference/api/webhooks">API specification</a>
12+
*/
13+
public class Webhook implements Serializable {
14+
@Getter protected final UUID uuid;
15+
@Getter protected final String url;
16+
@Getter protected final HashMap<String, String> headers;
17+
@Getter protected final boolean isEditable;
18+
19+
public Webhook(UUID uuid, String url, HashMap<String, String> headers, boolean isEditable) {
20+
this.uuid = uuid;
21+
this.url = url;
22+
this.headers = headers;
23+
this.isEditable = isEditable;
24+
}
25+
}

0 commit comments

Comments
 (0)