Skip to content

Commit e1864df

Browse files
committed
added webhook api
1 parent 30d3d60 commit e1864df

7 files changed

Lines changed: 354 additions & 16 deletions

File tree

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

Lines changed: 59 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,63 @@ public String generateTenantToken(
544546
return jwtToken;
545547
}
546548

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

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
import com.meilisearch.sdk.http.response.HttpResponse;
1212
import com.meilisearch.sdk.json.GsonJsonHandler;
1313
import com.meilisearch.sdk.json.JsonHandler;
14+
1415
import java.util.Collections;
1516
import java.util.Map;
1617

17-
/** HTTP client used for API calls to Meilisearch */
18+
/**
19+
* HTTP client used for API calls to Meilisearch
20+
*/
1821
public class HttpClient {
1922
private final CustomOkHttpClient client;
2023
private final BasicRequest request;
@@ -38,7 +41,7 @@ public HttpClient(Config config) {
3841
/**
3942
* Constructor for the HttpClient
4043
*
41-
* @param client HttpClient for making calls to server
44+
* @param client HttpClient for making calls to server
4245
* @param request BasicRequest for generating calls to server
4346
*/
4447
public HttpClient(CustomOkHttpClient client, BasicRequest request) {
@@ -57,56 +60,56 @@ public HttpClient(CustomOkHttpClient client, BasicRequest request) {
5760
* @throws MeilisearchException if the response is an error
5861
*/
5962
<T> T get(String api, Class<T> targetClass, Class<?>... parameters)
60-
throws MeilisearchException {
63+
throws MeilisearchException {
6164
return this.get(api, "", targetClass, parameters);
6265
}
6366

6467
/**
6568
* Gets the specified resource from the specified path with a given parameter
6669
*
67-
* @param api Path to document
70+
* @param api Path to document
6871
* @param param Parameter to be passed
6972
* @return document that was requested
7073
* @throws MeilisearchException if the response is an error
7174
*/
7275
<T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters)
73-
throws MeilisearchException {
76+
throws MeilisearchException {
7477
HttpRequest requestConfig = request.create(HttpMethod.GET, api + param, this.headers, null);
7578
HttpResponse<T> httpRequest = this.client.get(requestConfig);
7679
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass, parameters);
7780

7881
if (httpResponse.getStatusCode() >= 400) {
7982
throw new MeilisearchApiException(
80-
jsonHandler.decode(httpRequest.getContent(), APIError.class));
83+
jsonHandler.decode(httpRequest.getContent(), APIError.class));
8184
}
8285
return httpResponse.getContent();
8386
}
8487

8588
/**
8689
* Adds the specified resource to the specified path
8790
*
88-
* @param api Path to server
91+
* @param api Path to server
8992
* @param body Query for search
9093
* @return results of the search
9194
* @throws MeilisearchException if the response is an error
9295
*/
9396
<S, T> T post(String api, S body, Class<T> targetClass, Class<?>... parameters)
94-
throws MeilisearchException {
97+
throws MeilisearchException {
9598
HttpRequest requestConfig = request.create(HttpMethod.POST, api, this.headers, body);
9699
HttpResponse<T> httpRequest = this.client.post(requestConfig);
97100
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass, parameters);
98101

99102
if (httpResponse.getStatusCode() >= 400) {
100103
throw new MeilisearchApiException(
101-
jsonHandler.decode(httpRequest.getContent(), APIError.class));
104+
jsonHandler.decode(httpRequest.getContent(), APIError.class));
102105
}
103106
return httpResponse.getContent();
104107
}
105108

106109
/**
107110
* Replaces the specified resource with new data to the specified path
108111
*
109-
* @param api Path to the requested resource
112+
* @param api Path to the requested resource
110113
* @param body Replacement data for the requested resource
111114
* @return updated resource
112115
* @throws MeilisearchException if the response is an error
@@ -118,15 +121,15 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti
118121

119122
if (httpResponse.getStatusCode() >= 400) {
120123
throw new MeilisearchApiException(
121-
jsonHandler.decode(httpRequest.getContent(), APIError.class));
124+
jsonHandler.decode(httpRequest.getContent(), APIError.class));
122125
}
123126
return httpResponse.getContent();
124127
}
125128

126129
/**
127130
* Patch the specified resource with new data to the specified path
128131
*
129-
* @param api Path to server
132+
* @param api Path to server
130133
* @param body Query for search
131134
* @return results of the search
132135
* @throws MeilisearchException if the response is an error
@@ -137,7 +140,7 @@ <S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchExcep
137140

138141
if (httpResponse.getStatusCode() >= 400) {
139142
throw new MeilisearchApiException(
140-
jsonHandler.decode(httpResponse.getContent(), APIError.class));
143+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
141144
}
142145

143146
return response.create(httpResponse, targetClass).getContent();
@@ -154,11 +157,11 @@ <T> T delete(String api, Class<T> targetClass) throws MeilisearchException {
154157
HttpRequest requestConfig = request.create(HttpMethod.DELETE, api, this.headers, null);
155158
HttpResponse<T> httpRequest = this.client.delete(requestConfig);
156159
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
157-
158160
if (httpResponse.getStatusCode() >= 400) {
159161
throw new MeilisearchApiException(
160-
jsonHandler.decode(httpRequest.getContent(), APIError.class));
162+
jsonHandler.decode(httpRequest.getContent(), APIError.class));
161163
}
162164
return httpResponse.getContent();
165+
163166
}
164167
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
13+
public class WebHooksHandler {
14+
private final HttpClient httpClient;
15+
16+
protected WebHooksHandler(Config config) {
17+
this.httpClient = config.httpClient;
18+
}
19+
20+
/**
21+
* Gets a list of webhooks
22+
*
23+
* @return List of webhooks.
24+
* @throws MeilisearchException if an error occurs
25+
*/
26+
Results<Webhook> getWebhooks() throws MeilisearchException {
27+
return this.httpClient.get(webhooksPath().getURL(), Results.class, Webhook.class);
28+
}
29+
30+
/**
31+
* Gets a webhook from its uuid
32+
*
33+
* @param uuid Unique identifier of the webhook to get
34+
* @return Meilisearch API response as Webhook instance
35+
* @throws MeilisearchException if an error occurs
36+
*/
37+
Webhook getWebhook(UUID uuid) throws MeilisearchException {
38+
return this.httpClient.get(webhooksPath().addSubroute(uuid.toString()).getURL(), Webhook.class);
39+
}
40+
41+
/**
42+
* Create a new webhook
43+
*
44+
* @param createUpdateWebhookRequest Request body for creating a new webhook
45+
* @return Meilisearch API response as Webhook instance
46+
* @throws MeilisearchException if an error occurs
47+
*/
48+
Webhook createWebhook(CreateUpdateWebhookRequest createUpdateWebhookRequest) throws MeilisearchException {
49+
return this.httpClient.post(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) throws MeilisearchException {
61+
return this.httpClient.patch(webhooksPath().addSubroute(webhookUuid.toString()).getURL(), createUpdateWebhookRequest, Webhook.class);
62+
}
63+
64+
/**
65+
* Delete a webhook
66+
*
67+
* @param webhookUuid Unique identifier of a webhook to update
68+
* @throws MeilisearchException if an error occurs
69+
*/
70+
void deleteWebhook(UUID webhookUuid) throws MeilisearchException {
71+
this.httpClient.delete(webhooksPath().addSubroute(webhookUuid.toString()).getURL(), String.class);
72+
}
73+
74+
private URLBuilder webhooksPath() {
75+
return new URLBuilder("/webhooks");
76+
}
77+
}

src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public GsonJsonHandler() {
2020
builder.registerTypeAdapter(
2121
FilterableAttributesConfig.class, new GsonFilterableAttributesConfigTypeAdapter());
2222
builder.registerTypeAdapterFactory(new GsonTaskDetailsTypeAdapterFactory());
23-
this.gson = builder.create();
23+
this.gson = builder.serializeNulls().create();
2424
}
2525

2626
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.meilisearch.sdk.model;
2+
3+
4+
import lombok.NonNull;
5+
6+
import java.io.Serializable;
7+
import java.util.HashMap;
8+
9+
/**
10+
* Data structure used in request body while creating or updating a webhook.
11+
*
12+
* @see <a href="https://www.meilisearch.com/docs/reference/api/webhooks">API Specification</a>
13+
*/
14+
15+
public class CreateUpdateWebhookRequest implements Serializable {
16+
@NonNull String url;
17+
@NonNull HashMap<String, Object> headers;
18+
19+
public CreateUpdateWebhookRequest(String url, HashMap<String, Object> headers) {
20+
this.url = url;
21+
this.headers = headers;
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.meilisearch.sdk.model;
2+
3+
4+
import lombok.Getter;
5+
6+
import java.io.Serializable;
7+
import java.util.HashMap;
8+
import java.util.UUID;
9+
10+
/**
11+
* Webhook data structure.
12+
*
13+
* @see <a href="https://www.meilisearch.com/docs/reference/api/webhooks">API
14+
* specification</a>
15+
*/
16+
public class Webhook implements Serializable {
17+
@Getter protected final UUID uuid;
18+
@Getter protected final String url;
19+
@Getter protected final HashMap<String, String> headers;
20+
@Getter protected final boolean isEditable;
21+
22+
public Webhook(UUID uuid, String url, HashMap<String, String> headers, boolean isEditable) {
23+
this.uuid = uuid;
24+
this.url = url;
25+
this.headers = headers;
26+
this.isEditable = isEditable;
27+
}
28+
29+
}
30+

0 commit comments

Comments
 (0)