1+ /*
2+ * Copyright 2020 Conductor Authors.
3+ * <p>
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+ * the License. You may obtain a copy of the License at
6+ * <p>
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ * <p>
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+ * specific language governing permissions and limitations under the License.
12+ */
13+ package com .netflix .conductor .client .http ;
14+
15+ import java .util .List ;
16+
17+ import org .apache .commons .lang3 .Validate ;
18+
19+ import com .netflix .conductor .client .http .ConductorClientRequest .Method ;
20+ import com .netflix .conductor .common .model .Tag ;
21+ import com .netflix .conductor .common .model .WebhookConfig ;
22+
23+ import com .fasterxml .jackson .core .type .TypeReference ;
24+
25+ /**
26+ * Client for webhook configuration operations in Conductor
27+ */
28+ public final class WebhookConfigClient {
29+
30+ private ConductorClient client ;
31+
32+ /**
33+ * Creates a default webhook config client
34+ */
35+ public WebhookConfigClient () {
36+ }
37+
38+ public WebhookConfigClient (ConductorClient client ) {
39+ this .client = client ;
40+ }
41+
42+ /**
43+ * Kept only for backwards compatibility
44+ *
45+ * @param rootUri basePath for the ApiClient
46+ */
47+ @ Deprecated
48+ public void setRootURI (String rootUri ) {
49+ if (client != null ) {
50+ client .shutdown ();
51+ }
52+ client = new ConductorClient (rootUri );
53+ }
54+
55+ /**
56+ * Create a new webhook configuration
57+ *
58+ * @param webhookConfig The webhook configuration to create
59+ * @return The created webhook configuration with assigned ID
60+ */
61+ public WebhookConfig createWebhook (WebhookConfig webhookConfig ) {
62+ Validate .notNull (webhookConfig , "WebhookConfig cannot be null" );
63+ Validate .notBlank (webhookConfig .getName (), "Webhook name cannot be blank" );
64+
65+ ConductorClientRequest request = ConductorClientRequest .builder ()
66+ .method (Method .POST )
67+ .path ("/metadata/webhook" )
68+ .body (webhookConfig )
69+ .build ();
70+
71+ ConductorClientResponse <WebhookConfig > resp = client .execute (request , new TypeReference <>() {
72+ });
73+
74+ return resp .getData ();
75+ }
76+
77+ /**
78+ * Update an existing webhook configuration
79+ *
80+ * @param id The webhook ID to update
81+ * @param webhookConfig The updated webhook configuration
82+ * @return The updated webhook configuration
83+ */
84+ public WebhookConfig updateWebhook (String id , WebhookConfig webhookConfig ) {
85+ Validate .notBlank (id , "Webhook ID cannot be blank" );
86+ Validate .notNull (webhookConfig , "WebhookConfig cannot be null" );
87+
88+ ConductorClientRequest request = ConductorClientRequest .builder ()
89+ .method (Method .PUT )
90+ .path ("/metadata/webhook/{id}" )
91+ .addPathParam ("id" , id )
92+ .body (webhookConfig )
93+ .build ();
94+
95+ ConductorClientResponse <WebhookConfig > resp = client .execute (request , new TypeReference <>() {
96+ });
97+
98+ return resp .getData ();
99+ }
100+
101+ /**
102+ * Delete a webhook configuration
103+ *
104+ * @param id The webhook ID to delete
105+ */
106+ public void deleteWebhook (String id ) {
107+ Validate .notBlank (id , "Webhook ID cannot be blank" );
108+
109+ ConductorClientRequest request = ConductorClientRequest .builder ()
110+ .method (Method .DELETE )
111+ .path ("/metadata/webhook/{id}" )
112+ .addPathParam ("id" , id )
113+ .build ();
114+
115+ client .execute (request );
116+ }
117+
118+ /**
119+ * Get a webhook configuration by ID
120+ *
121+ * @param id The webhook ID
122+ * @return The webhook configuration
123+ */
124+ public WebhookConfig getWebhook (String id ) {
125+ Validate .notBlank (id , "Webhook ID cannot be blank" );
126+
127+ ConductorClientRequest request = ConductorClientRequest .builder ()
128+ .method (Method .GET )
129+ .path ("/metadata/webhook/{id}" )
130+ .addPathParam ("id" , id )
131+ .build ();
132+
133+ ConductorClientResponse <WebhookConfig > resp = client .execute (request , new TypeReference <>() {
134+ });
135+
136+ return resp .getData ();
137+ }
138+
139+ /**
140+ * Get all webhook configurations
141+ *
142+ * @return List of all webhook configurations
143+ */
144+ public List <WebhookConfig > getAllWebhooks () {
145+ ConductorClientRequest request = ConductorClientRequest .builder ()
146+ .method (Method .GET )
147+ .path ("/metadata/webhook" )
148+ .build ();
149+
150+ ConductorClientResponse <List <WebhookConfig >> resp = client .execute (request , new TypeReference <>() {
151+ });
152+
153+ return resp .getData ();
154+ }
155+
156+ /**
157+ * Get tags for a webhook
158+ *
159+ * @param id The webhook ID
160+ * @return List of tags associated with the webhook
161+ */
162+ public List <Tag > getTagsForWebhook (String id ) {
163+ Validate .notBlank (id , "Webhook ID cannot be blank" );
164+
165+ ConductorClientRequest request = ConductorClientRequest .builder ()
166+ .method (Method .GET )
167+ .path ("/metadata/webhook/{id}/tags" )
168+ .addPathParam ("id" , id )
169+ .build ();
170+
171+ ConductorClientResponse <List <Tag >> resp = client .execute (request , new TypeReference <>() {
172+ });
173+
174+ return resp .getData ();
175+ }
176+
177+ /**
178+ * Set tags for a webhook (replaces existing tags)
179+ *
180+ * @param id The webhook ID
181+ * @param tags The tags to set
182+ */
183+ public void putTagsForWebhook (String id , List <Tag > tags ) {
184+ Validate .notBlank (id , "Webhook ID cannot be blank" );
185+ Validate .notNull (tags , "Tags list cannot be null" );
186+
187+ ConductorClientRequest request = ConductorClientRequest .builder ()
188+ .method (Method .PUT )
189+ .path ("/metadata/webhook/{id}/tags" )
190+ .addPathParam ("id" , id )
191+ .body (tags )
192+ .build ();
193+
194+ client .execute (request );
195+ }
196+
197+ /**
198+ * Delete specific tags from a webhook
199+ *
200+ * @param id The webhook ID
201+ * @param tags The tags to remove
202+ */
203+ public void deleteTagsForWebhook (String id , List <Tag > tags ) {
204+ Validate .notBlank (id , "Webhook ID cannot be blank" );
205+ Validate .notNull (tags , "Tags list cannot be null" );
206+
207+ ConductorClientRequest request = ConductorClientRequest .builder ()
208+ .method (Method .DELETE )
209+ .path ("/metadata/webhook/{id}/tags" )
210+ .addPathParam ("id" , id )
211+ .body (tags )
212+ .build ();
213+
214+ client .execute (request );
215+ }
216+
217+ /**
218+ * Add a single tag to a webhook
219+ *
220+ * @param id The webhook ID
221+ * @param tag The tag to add
222+ */
223+ public void addTagToWebhook (String id , Tag tag ) {
224+ Validate .notNull (tag , "Tag cannot be null" );
225+ putTagsForWebhook (id , List .of (tag ));
226+ }
227+
228+ /**
229+ * Remove a single tag from a webhook
230+ *
231+ * @param id The webhook ID
232+ * @param tag The tag to remove
233+ */
234+ public void removeTagFromWebhook (String id , Tag tag ) {
235+ Validate .notNull (tag , "Tag cannot be null" );
236+ deleteTagsForWebhook (id , List .of (tag ));
237+ }
238+
239+ /**
240+ * Check if a webhook exists
241+ *
242+ * @param id The webhook ID
243+ * @return true if the webhook exists, false otherwise
244+ */
245+ public boolean webhookExists (String id ) {
246+ try {
247+ getWebhook (id );
248+ return true ;
249+ } catch (Exception e ) {
250+ return false ;
251+ }
252+ }
253+ }
0 commit comments