1use crate::transport::types::{ApiKeyCreatedPayload, ApiKeyRevokedPayload, ConnectionCreatedPayload, ConnectionDeletedPayload, ConnectionOAuthCreatedPayload, ConnectionUpdatedPayload, GrantCreatedPayload, GrantRevokedPayload, GrantUpdatedPayload, MemberAddedPayload, MemberRemovedPayload, MemberRoleUpdatedPayload, OAuthFailedPayload, OAuthRefreshedPayload, OrganizationCreatedPayload, OrganizationDeletedPayload, OrganizationUpdatedPayload, PassthroughExecutedPayload, ProjectCreatedPayload, ProjectDeletedPayload, ProjectUpdatedPayload};
2use serde::{Deserialize, Serialize, de::value::{Error as DeserializeError, StrDeserializer}};
3use serde_json::{Error as JsonError, Value as JsonValue};
4use std::future::Future;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10pub enum WebhookEventType {
11 #[serde(rename = "api_key.created")]
12 ApiKeyCreated,
13 #[serde(rename = "api_key.revoked")]
14 ApiKeyRevoked,
15 #[serde(rename = "connection.created")]
16 ConnectionCreated,
17 #[serde(rename = "connection.deleted")]
18 ConnectionDeleted,
19 #[serde(rename = "connection.oauth.created")]
20 ConnectionOAuthCreated,
21 #[serde(rename = "connection.updated")]
22 ConnectionUpdated,
23 #[serde(rename = "grant.created")]
24 GrantCreated,
25 #[serde(rename = "grant.revoked")]
26 GrantRevoked,
27 #[serde(rename = "grant.updated")]
28 GrantUpdated,
29 #[serde(rename = "member.added")]
30 MemberAdded,
31 #[serde(rename = "member.removed")]
32 MemberRemoved,
33 #[serde(rename = "member.role_updated")]
34 MemberRoleUpdated,
35 #[serde(rename = "oauth.failed")]
36 OAuthFailed,
37 #[serde(rename = "oauth.refreshed")]
38 OAuthRefreshed,
39 #[serde(rename = "organization.created")]
40 OrganizationCreated,
41 #[serde(rename = "organization.deleted")]
42 OrganizationDeleted,
43 #[serde(rename = "organization.updated")]
44 OrganizationUpdated,
45 #[serde(rename = "passthrough.executed")]
46 PassthroughExecuted,
47 #[serde(rename = "project.created")]
48 ProjectCreated,
49 #[serde(rename = "project.deleted")]
50 ProjectDeleted,
51 #[serde(rename = "project.updated")]
52 ProjectUpdated,
53}
54
55impl WebhookEventType {
56 pub fn parse(event: &str) -> Option<Self> {
60 Self::deserialize(StrDeserializer::<DeserializeError>::new(event)).ok()
61 }
62
63 pub fn as_str(&self) -> &'static str {
65 match self {
66 Self::ApiKeyCreated => "api_key.created",
67 Self::ApiKeyRevoked => "api_key.revoked",
68 Self::ConnectionCreated => "connection.created",
69 Self::ConnectionDeleted => "connection.deleted",
70 Self::ConnectionOAuthCreated => "connection.oauth.created",
71 Self::ConnectionUpdated => "connection.updated",
72 Self::GrantCreated => "grant.created",
73 Self::GrantRevoked => "grant.revoked",
74 Self::GrantUpdated => "grant.updated",
75 Self::MemberAdded => "member.added",
76 Self::MemberRemoved => "member.removed",
77 Self::MemberRoleUpdated => "member.role_updated",
78 Self::OAuthFailed => "oauth.failed",
79 Self::OAuthRefreshed => "oauth.refreshed",
80 Self::OrganizationCreated => "organization.created",
81 Self::OrganizationDeleted => "organization.deleted",
82 Self::OrganizationUpdated => "organization.updated",
83 Self::PassthroughExecuted => "passthrough.executed",
84 Self::ProjectCreated => "project.created",
85 Self::ProjectDeleted => "project.deleted",
86 Self::ProjectUpdated => "project.updated",
87 }
88 }
89
90 pub const ALL: &'static [Self] = &[Self::ApiKeyCreated, Self::ApiKeyRevoked, Self::ConnectionCreated, Self::ConnectionDeleted, Self::ConnectionOAuthCreated, Self::ConnectionUpdated, Self::GrantCreated, Self::GrantRevoked, Self::GrantUpdated, Self::MemberAdded, Self::MemberRemoved, Self::MemberRoleUpdated, Self::OAuthFailed, Self::OAuthRefreshed, Self::OrganizationCreated, Self::OrganizationDeleted, Self::OrganizationUpdated, Self::PassthroughExecuted, Self::ProjectCreated, Self::ProjectDeleted, Self::ProjectUpdated];
94
95 pub const CONNECTION_SCOPED: &'static [Self] = &[Self::ConnectionCreated, Self::ConnectionDeleted, Self::ConnectionOAuthCreated, Self::ConnectionUpdated, Self::OAuthFailed, Self::OAuthRefreshed, Self::PassthroughExecuted];
100
101 pub fn connection_key_of(
106 &self,
107 payload: &JsonValue
108 ) -> Result<Option<String>, JsonError> {
109 match self {
110 Self::ApiKeyCreated => serde_json::from_value::<ApiKeyCreatedPayload>(payload.clone()).map(|_| None),
111 Self::ApiKeyRevoked => serde_json::from_value::<ApiKeyRevokedPayload>(payload.clone()).map(|_| None),
112 Self::ConnectionCreated => serde_json::from_value::<ConnectionCreatedPayload>(payload.clone()).map(|payload| Some(payload.key)),
113 Self::ConnectionDeleted => serde_json::from_value::<ConnectionDeletedPayload>(payload.clone()).map(|payload| payload.key),
114 Self::ConnectionOAuthCreated => serde_json::from_value::<ConnectionOAuthCreatedPayload>(payload.clone()).map(|payload| Some(payload.key)),
115 Self::ConnectionUpdated => serde_json::from_value::<ConnectionUpdatedPayload>(payload.clone()).map(|payload| Some(payload.key)),
116 Self::GrantCreated => serde_json::from_value::<GrantCreatedPayload>(payload.clone()).map(|_| None),
117 Self::GrantRevoked => serde_json::from_value::<GrantRevokedPayload>(payload.clone()).map(|_| None),
118 Self::GrantUpdated => serde_json::from_value::<GrantUpdatedPayload>(payload.clone()).map(|_| None),
119 Self::MemberAdded => serde_json::from_value::<MemberAddedPayload>(payload.clone()).map(|_| None),
120 Self::MemberRemoved => serde_json::from_value::<MemberRemovedPayload>(payload.clone()).map(|_| None),
121 Self::MemberRoleUpdated => serde_json::from_value::<MemberRoleUpdatedPayload>(payload.clone()).map(|_| None),
122 Self::OAuthFailed => serde_json::from_value::<OAuthFailedPayload>(payload.clone()).map(|payload| payload.key),
123 Self::OAuthRefreshed => serde_json::from_value::<OAuthRefreshedPayload>(payload.clone()).map(|payload| payload.key),
124 Self::OrganizationCreated => serde_json::from_value::<OrganizationCreatedPayload>(payload.clone()).map(|_| None),
125 Self::OrganizationDeleted => serde_json::from_value::<OrganizationDeletedPayload>(payload.clone()).map(|_| None),
126 Self::OrganizationUpdated => serde_json::from_value::<OrganizationUpdatedPayload>(payload.clone()).map(|_| None),
127 Self::PassthroughExecuted => serde_json::from_value::<PassthroughExecutedPayload>(payload.clone()).map(|payload| payload.key),
128 Self::ProjectCreated => serde_json::from_value::<ProjectCreatedPayload>(payload.clone()).map(|_| None),
129 Self::ProjectDeleted => serde_json::from_value::<ProjectDeletedPayload>(payload.clone()).map(|_| None),
130 Self::ProjectUpdated => serde_json::from_value::<ProjectUpdatedPayload>(payload.clone()).map(|_| None),
131 }
132 }
133}
134
135impl Webhooks {
136 pub fn api_key_created<F, Fut, Output>(self, handler: F) -> Self
138 where
139 F: Fn(Delivery<ApiKeyCreatedPayload>) -> Fut + Send + Sync + 'static,
140 Fut: Future<Output = Output> + Send + 'static,
141 Output: WebhookHandlerOutput + Send + 'static
142 {
143 self.register(WebhookEventType::ApiKeyCreated, None, handler)
144 }
145
146 pub fn api_key_revoked<F, Fut, Output>(self, handler: F) -> Self
148 where
149 F: Fn(Delivery<ApiKeyRevokedPayload>) -> Fut + Send + Sync + 'static,
150 Fut: Future<Output = Output> + Send + 'static,
151 Output: WebhookHandlerOutput + Send + 'static
152 {
153 self.register(WebhookEventType::ApiKeyRevoked, None, handler)
154 }
155
156 pub fn connection_created<F, Fut, Output>(self, handler: F) -> Self
158 where
159 F: Fn(Delivery<ConnectionCreatedPayload>) -> Fut + Send + Sync + 'static,
160 Fut: Future<Output = Output> + Send + 'static,
161 Output: WebhookHandlerOutput + Send + 'static
162 {
163 self.register(WebhookEventType::ConnectionCreated, None, handler)
164 }
165
166 pub fn connection_deleted<F, Fut, Output>(self, handler: F) -> Self
168 where
169 F: Fn(Delivery<ConnectionDeletedPayload>) -> Fut + Send + Sync + 'static,
170 Fut: Future<Output = Output> + Send + 'static,
171 Output: WebhookHandlerOutput + Send + 'static
172 {
173 self.register(WebhookEventType::ConnectionDeleted, None, handler)
174 }
175
176 pub fn connection_oauth_created<F, Fut, Output>(self, handler: F) -> Self
178 where
179 F: Fn(Delivery<ConnectionOAuthCreatedPayload>) -> Fut + Send + Sync + 'static,
180 Fut: Future<Output = Output> + Send + 'static,
181 Output: WebhookHandlerOutput + Send + 'static
182 {
183 self.register(WebhookEventType::ConnectionOAuthCreated, None, handler)
184 }
185
186 pub fn connection_updated<F, Fut, Output>(self, handler: F) -> Self
188 where
189 F: Fn(Delivery<ConnectionUpdatedPayload>) -> Fut + Send + Sync + 'static,
190 Fut: Future<Output = Output> + Send + 'static,
191 Output: WebhookHandlerOutput + Send + 'static
192 {
193 self.register(WebhookEventType::ConnectionUpdated, None, handler)
194 }
195
196 pub fn grant_created<F, Fut, Output>(self, handler: F) -> Self
198 where
199 F: Fn(Delivery<GrantCreatedPayload>) -> Fut + Send + Sync + 'static,
200 Fut: Future<Output = Output> + Send + 'static,
201 Output: WebhookHandlerOutput + Send + 'static
202 {
203 self.register(WebhookEventType::GrantCreated, None, handler)
204 }
205
206 pub fn grant_revoked<F, Fut, Output>(self, handler: F) -> Self
208 where
209 F: Fn(Delivery<GrantRevokedPayload>) -> Fut + Send + Sync + 'static,
210 Fut: Future<Output = Output> + Send + 'static,
211 Output: WebhookHandlerOutput + Send + 'static
212 {
213 self.register(WebhookEventType::GrantRevoked, None, handler)
214 }
215
216 pub fn grant_updated<F, Fut, Output>(self, handler: F) -> Self
218 where
219 F: Fn(Delivery<GrantUpdatedPayload>) -> Fut + Send + Sync + 'static,
220 Fut: Future<Output = Output> + Send + 'static,
221 Output: WebhookHandlerOutput + Send + 'static
222 {
223 self.register(WebhookEventType::GrantUpdated, None, handler)
224 }
225
226 pub fn member_added<F, Fut, Output>(self, handler: F) -> Self
228 where
229 F: Fn(Delivery<MemberAddedPayload>) -> Fut + Send + Sync + 'static,
230 Fut: Future<Output = Output> + Send + 'static,
231 Output: WebhookHandlerOutput + Send + 'static
232 {
233 self.register(WebhookEventType::MemberAdded, None, handler)
234 }
235
236 pub fn member_removed<F, Fut, Output>(self, handler: F) -> Self
238 where
239 F: Fn(Delivery<MemberRemovedPayload>) -> Fut + Send + Sync + 'static,
240 Fut: Future<Output = Output> + Send + 'static,
241 Output: WebhookHandlerOutput + Send + 'static
242 {
243 self.register(WebhookEventType::MemberRemoved, None, handler)
244 }
245
246 pub fn member_role_updated<F, Fut, Output>(self, handler: F) -> Self
248 where
249 F: Fn(Delivery<MemberRoleUpdatedPayload>) -> Fut + Send + Sync + 'static,
250 Fut: Future<Output = Output> + Send + 'static,
251 Output: WebhookHandlerOutput + Send + 'static
252 {
253 self.register(WebhookEventType::MemberRoleUpdated, None, handler)
254 }
255
256 pub fn oauth_failed<F, Fut, Output>(self, handler: F) -> Self
258 where
259 F: Fn(Delivery<OAuthFailedPayload>) -> Fut + Send + Sync + 'static,
260 Fut: Future<Output = Output> + Send + 'static,
261 Output: WebhookHandlerOutput + Send + 'static
262 {
263 self.register(WebhookEventType::OAuthFailed, None, handler)
264 }
265
266 pub fn oauth_refreshed<F, Fut, Output>(self, handler: F) -> Self
268 where
269 F: Fn(Delivery<OAuthRefreshedPayload>) -> Fut + Send + Sync + 'static,
270 Fut: Future<Output = Output> + Send + 'static,
271 Output: WebhookHandlerOutput + Send + 'static
272 {
273 self.register(WebhookEventType::OAuthRefreshed, None, handler)
274 }
275
276 pub fn organization_created<F, Fut, Output>(self, handler: F) -> Self
278 where
279 F: Fn(Delivery<OrganizationCreatedPayload>) -> Fut + Send + Sync + 'static,
280 Fut: Future<Output = Output> + Send + 'static,
281 Output: WebhookHandlerOutput + Send + 'static
282 {
283 self.register(WebhookEventType::OrganizationCreated, None, handler)
284 }
285
286 pub fn organization_deleted<F, Fut, Output>(self, handler: F) -> Self
288 where
289 F: Fn(Delivery<OrganizationDeletedPayload>) -> Fut + Send + Sync + 'static,
290 Fut: Future<Output = Output> + Send + 'static,
291 Output: WebhookHandlerOutput + Send + 'static
292 {
293 self.register(WebhookEventType::OrganizationDeleted, None, handler)
294 }
295
296 pub fn organization_updated<F, Fut, Output>(self, handler: F) -> Self
298 where
299 F: Fn(Delivery<OrganizationUpdatedPayload>) -> Fut + Send + Sync + 'static,
300 Fut: Future<Output = Output> + Send + 'static,
301 Output: WebhookHandlerOutput + Send + 'static
302 {
303 self.register(WebhookEventType::OrganizationUpdated, None, handler)
304 }
305
306 pub fn passthrough_executed<F, Fut, Output>(self, handler: F) -> Self
308 where
309 F: Fn(Delivery<PassthroughExecutedPayload>) -> Fut + Send + Sync + 'static,
310 Fut: Future<Output = Output> + Send + 'static,
311 Output: WebhookHandlerOutput + Send + 'static
312 {
313 self.register(WebhookEventType::PassthroughExecuted, None, handler)
314 }
315
316 pub fn project_created<F, Fut, Output>(self, handler: F) -> Self
318 where
319 F: Fn(Delivery<ProjectCreatedPayload>) -> Fut + Send + Sync + 'static,
320 Fut: Future<Output = Output> + Send + 'static,
321 Output: WebhookHandlerOutput + Send + 'static
322 {
323 self.register(WebhookEventType::ProjectCreated, None, handler)
324 }
325
326 pub fn project_deleted<F, Fut, Output>(self, handler: F) -> Self
328 where
329 F: Fn(Delivery<ProjectDeletedPayload>) -> Fut + Send + Sync + 'static,
330 Fut: Future<Output = Output> + Send + 'static,
331 Output: WebhookHandlerOutput + Send + 'static
332 {
333 self.register(WebhookEventType::ProjectDeleted, None, handler)
334 }
335
336 pub fn project_updated<F, Fut, Output>(self, handler: F) -> Self
338 where
339 F: Fn(Delivery<ProjectUpdatedPayload>) -> Fut + Send + Sync + 'static,
340 Fut: Future<Output = Output> + Send + 'static,
341 Output: WebhookHandlerOutput + Send + 'static
342 {
343 self.register(WebhookEventType::ProjectUpdated, None, handler)
344 }
345
346}
347
348impl ConnectionWebhooks {
349 pub fn connection_created<F, Fut, Output>(self, handler: F) -> Self
351 where
352 F: Fn(Delivery<ConnectionCreatedPayload>) -> Fut + Send + Sync + 'static,
353 Fut: Future<Output = Output> + Send + 'static,
354 Output: WebhookHandlerOutput + Send + 'static
355 {
356 self.register(WebhookEventType::ConnectionCreated, handler)
357 }
358
359 pub fn connection_deleted<F, Fut, Output>(self, handler: F) -> Self
361 where
362 F: Fn(Delivery<ConnectionDeletedPayload>) -> Fut + Send + Sync + 'static,
363 Fut: Future<Output = Output> + Send + 'static,
364 Output: WebhookHandlerOutput + Send + 'static
365 {
366 self.register(WebhookEventType::ConnectionDeleted, handler)
367 }
368
369 pub fn connection_oauth_created<F, Fut, Output>(self, handler: F) -> Self
371 where
372 F: Fn(Delivery<ConnectionOAuthCreatedPayload>) -> Fut + Send + Sync + 'static,
373 Fut: Future<Output = Output> + Send + 'static,
374 Output: WebhookHandlerOutput + Send + 'static
375 {
376 self.register(WebhookEventType::ConnectionOAuthCreated, handler)
377 }
378
379 pub fn connection_updated<F, Fut, Output>(self, handler: F) -> Self
381 where
382 F: Fn(Delivery<ConnectionUpdatedPayload>) -> Fut + Send + Sync + 'static,
383 Fut: Future<Output = Output> + Send + 'static,
384 Output: WebhookHandlerOutput + Send + 'static
385 {
386 self.register(WebhookEventType::ConnectionUpdated, handler)
387 }
388
389 pub fn oauth_failed<F, Fut, Output>(self, handler: F) -> Self
391 where
392 F: Fn(Delivery<OAuthFailedPayload>) -> Fut + Send + Sync + 'static,
393 Fut: Future<Output = Output> + Send + 'static,
394 Output: WebhookHandlerOutput + Send + 'static
395 {
396 self.register(WebhookEventType::OAuthFailed, handler)
397 }
398
399 pub fn oauth_refreshed<F, Fut, Output>(self, handler: F) -> Self
401 where
402 F: Fn(Delivery<OAuthRefreshedPayload>) -> Fut + Send + Sync + 'static,
403 Fut: Future<Output = Output> + Send + 'static,
404 Output: WebhookHandlerOutput + Send + 'static
405 {
406 self.register(WebhookEventType::OAuthRefreshed, handler)
407 }
408
409 pub fn passthrough_executed<F, Fut, Output>(self, handler: F) -> Self
411 where
412 F: Fn(Delivery<PassthroughExecutedPayload>) -> Fut + Send + Sync + 'static,
413 Fut: Future<Output = Output> + Send + 'static,
414 Output: WebhookHandlerOutput + Send + 'static
415 {
416 self.register(WebhookEventType::PassthroughExecuted, handler)
417 }
418
419}