use crate::transport::types::{ApiKeyCreatedPayload, ApiKeyRevokedPayload, ConnectionCreatedPayload, ConnectionDeletedPayload, ConnectionOAuthCreatedPayload, ConnectionUpdatedPayload, GrantCreatedPayload, GrantRevokedPayload, GrantUpdatedPayload, MemberAddedPayload, MemberRemovedPayload, MemberRoleUpdatedPayload, OAuthFailedPayload, OAuthRefreshedPayload, OrganizationCreatedPayload, OrganizationDeletedPayload, OrganizationUpdatedPayload, PassthroughExecutedPayload, ProjectCreatedPayload, ProjectDeletedPayload, ProjectUpdatedPayload};
use serde::{Deserialize, Serialize, de::value::{Error as DeserializeError, StrDeserializer}};
use serde_json::{Error as JsonError, Value as JsonValue};
use std::future::Future;
/// Every event a One webhook subscription can deliver.
///
/// Generated from the SDK spec; the wire strings are the server contract.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum WebhookEventType {
#[serde(rename = "api_key.created")]
ApiKeyCreated,
#[serde(rename = "api_key.revoked")]
ApiKeyRevoked,
#[serde(rename = "connection.created")]
ConnectionCreated,
#[serde(rename = "connection.deleted")]
ConnectionDeleted,
#[serde(rename = "connection.oauth.created")]
ConnectionOAuthCreated,
#[serde(rename = "connection.updated")]
ConnectionUpdated,
#[serde(rename = "grant.created")]
GrantCreated,
#[serde(rename = "grant.revoked")]
GrantRevoked,
#[serde(rename = "grant.updated")]
GrantUpdated,
#[serde(rename = "member.added")]
MemberAdded,
#[serde(rename = "member.removed")]
MemberRemoved,
#[serde(rename = "member.role_updated")]
MemberRoleUpdated,
#[serde(rename = "oauth.failed")]
OAuthFailed,
#[serde(rename = "oauth.refreshed")]
OAuthRefreshed,
#[serde(rename = "organization.created")]
OrganizationCreated,
#[serde(rename = "organization.deleted")]
OrganizationDeleted,
#[serde(rename = "organization.updated")]
OrganizationUpdated,
#[serde(rename = "passthrough.executed")]
PassthroughExecuted,
#[serde(rename = "project.created")]
ProjectCreated,
#[serde(rename = "project.deleted")]
ProjectDeleted,
#[serde(rename = "project.updated")]
ProjectUpdated,
}
impl WebhookEventType {
/// Parses the on-the-wire event string, or `None` for an event this
/// SDK build does not recognize. Such a delivery still verifies and
/// reaches [`Webhooks::on_any`]; it simply has no typed handler.
pub fn parse(event: &str) -> Option<Self> {
Self::deserialize(StrDeserializer::<DeserializeError>::new(event)).ok()
}
/// The on-the-wire event string.
pub fn as_str(&self) -> &'static str {
match self {
Self::ApiKeyCreated => "api_key.created",
Self::ApiKeyRevoked => "api_key.revoked",
Self::ConnectionCreated => "connection.created",
Self::ConnectionDeleted => "connection.deleted",
Self::ConnectionOAuthCreated => "connection.oauth.created",
Self::ConnectionUpdated => "connection.updated",
Self::GrantCreated => "grant.created",
Self::GrantRevoked => "grant.revoked",
Self::GrantUpdated => "grant.updated",
Self::MemberAdded => "member.added",
Self::MemberRemoved => "member.removed",
Self::MemberRoleUpdated => "member.role_updated",
Self::OAuthFailed => "oauth.failed",
Self::OAuthRefreshed => "oauth.refreshed",
Self::OrganizationCreated => "organization.created",
Self::OrganizationDeleted => "organization.deleted",
Self::OrganizationUpdated => "organization.updated",
Self::PassthroughExecuted => "passthrough.executed",
Self::ProjectCreated => "project.created",
Self::ProjectDeleted => "project.deleted",
Self::ProjectUpdated => "project.updated",
}
}
/// Every event in this build's catalog, which is what a catch-all
/// handler subscribes to. Regenerate the SDK to pick up events the
/// server adds later.
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];
/// The events that carry a connection, and so can be scoped to one.
/// A connection-scoped catch-all subscribes to just these: the server
/// cannot filter by connection, so subscribing to the rest would only
/// ship deliveries the client discards.
pub const CONNECTION_SCOPED: &'static [Self] = &[Self::ConnectionCreated, Self::ConnectionDeleted, Self::ConnectionOAuthCreated, Self::ConnectionUpdated, Self::OAuthFailed, Self::OAuthRefreshed, Self::PassthroughExecuted];
/// Validates `payload` through this event's own payload type and
/// returns its connection key when it carries one. A renamed or
/// reshaped field becomes a decode failure instead of a typed
/// handler receiving arbitrary JSON.
pub fn connection_key_of(
&self,
payload: &JsonValue
) -> Result<Option<String>, JsonError> {
match self {
Self::ApiKeyCreated => serde_json::from_value::<ApiKeyCreatedPayload>(payload.clone()).map(|_| None),
Self::ApiKeyRevoked => serde_json::from_value::<ApiKeyRevokedPayload>(payload.clone()).map(|_| None),
Self::ConnectionCreated => serde_json::from_value::<ConnectionCreatedPayload>(payload.clone()).map(|payload| Some(payload.key)),
Self::ConnectionDeleted => serde_json::from_value::<ConnectionDeletedPayload>(payload.clone()).map(|payload| payload.key),
Self::ConnectionOAuthCreated => serde_json::from_value::<ConnectionOAuthCreatedPayload>(payload.clone()).map(|payload| Some(payload.key)),
Self::ConnectionUpdated => serde_json::from_value::<ConnectionUpdatedPayload>(payload.clone()).map(|payload| Some(payload.key)),
Self::GrantCreated => serde_json::from_value::<GrantCreatedPayload>(payload.clone()).map(|_| None),
Self::GrantRevoked => serde_json::from_value::<GrantRevokedPayload>(payload.clone()).map(|_| None),
Self::GrantUpdated => serde_json::from_value::<GrantUpdatedPayload>(payload.clone()).map(|_| None),
Self::MemberAdded => serde_json::from_value::<MemberAddedPayload>(payload.clone()).map(|_| None),
Self::MemberRemoved => serde_json::from_value::<MemberRemovedPayload>(payload.clone()).map(|_| None),
Self::MemberRoleUpdated => serde_json::from_value::<MemberRoleUpdatedPayload>(payload.clone()).map(|_| None),
Self::OAuthFailed => serde_json::from_value::<OAuthFailedPayload>(payload.clone()).map(|payload| payload.key),
Self::OAuthRefreshed => serde_json::from_value::<OAuthRefreshedPayload>(payload.clone()).map(|payload| payload.key),
Self::OrganizationCreated => serde_json::from_value::<OrganizationCreatedPayload>(payload.clone()).map(|_| None),
Self::OrganizationDeleted => serde_json::from_value::<OrganizationDeletedPayload>(payload.clone()).map(|_| None),
Self::OrganizationUpdated => serde_json::from_value::<OrganizationUpdatedPayload>(payload.clone()).map(|_| None),
Self::PassthroughExecuted => serde_json::from_value::<PassthroughExecutedPayload>(payload.clone()).map(|payload| payload.key),
Self::ProjectCreated => serde_json::from_value::<ProjectCreatedPayload>(payload.clone()).map(|_| None),
Self::ProjectDeleted => serde_json::from_value::<ProjectDeletedPayload>(payload.clone()).map(|_| None),
Self::ProjectUpdated => serde_json::from_value::<ProjectUpdatedPayload>(payload.clone()).map(|_| None),
}
}
}
impl Webhooks {
/// Registers a handler for `api_key.created`.
pub fn api_key_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ApiKeyCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ApiKeyCreated, None, handler)
}
/// Registers a handler for `api_key.revoked`.
pub fn api_key_revoked<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ApiKeyRevokedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ApiKeyRevoked, None, handler)
}
/// Registers a handler for `connection.created`.
pub fn connection_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionCreated, None, handler)
}
/// Registers a handler for `connection.deleted`.
pub fn connection_deleted<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionDeletedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionDeleted, None, handler)
}
/// Registers a handler for `connection.oauth.created`.
pub fn connection_oauth_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionOAuthCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionOAuthCreated, None, handler)
}
/// Registers a handler for `connection.updated`.
pub fn connection_updated<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionUpdatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionUpdated, None, handler)
}
/// Registers a handler for `grant.created`.
pub fn grant_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<GrantCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::GrantCreated, None, handler)
}
/// Registers a handler for `grant.revoked`.
pub fn grant_revoked<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<GrantRevokedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::GrantRevoked, None, handler)
}
/// Registers a handler for `grant.updated`.
pub fn grant_updated<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<GrantUpdatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::GrantUpdated, None, handler)
}
/// Registers a handler for `member.added`.
pub fn member_added<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<MemberAddedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::MemberAdded, None, handler)
}
/// Registers a handler for `member.removed`.
pub fn member_removed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<MemberRemovedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::MemberRemoved, None, handler)
}
/// Registers a handler for `member.role_updated`.
pub fn member_role_updated<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<MemberRoleUpdatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::MemberRoleUpdated, None, handler)
}
/// Registers a handler for `oauth.failed`.
pub fn oauth_failed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OAuthFailedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OAuthFailed, None, handler)
}
/// Registers a handler for `oauth.refreshed`.
pub fn oauth_refreshed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OAuthRefreshedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OAuthRefreshed, None, handler)
}
/// Registers a handler for `organization.created`.
pub fn organization_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OrganizationCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OrganizationCreated, None, handler)
}
/// Registers a handler for `organization.deleted`.
pub fn organization_deleted<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OrganizationDeletedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OrganizationDeleted, None, handler)
}
/// Registers a handler for `organization.updated`.
pub fn organization_updated<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OrganizationUpdatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OrganizationUpdated, None, handler)
}
/// Registers a handler for `passthrough.executed`.
pub fn passthrough_executed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<PassthroughExecutedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::PassthroughExecuted, None, handler)
}
/// Registers a handler for `project.created`.
pub fn project_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ProjectCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ProjectCreated, None, handler)
}
/// Registers a handler for `project.deleted`.
pub fn project_deleted<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ProjectDeletedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ProjectDeleted, None, handler)
}
/// Registers a handler for `project.updated`.
pub fn project_updated<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ProjectUpdatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ProjectUpdated, None, handler)
}
}
impl ConnectionWebhooks {
/// Registers a handler for `connection.created` on this connection.
pub fn connection_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionCreated, handler)
}
/// Registers a handler for `connection.deleted` on this connection.
pub fn connection_deleted<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionDeletedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionDeleted, handler)
}
/// Registers a handler for `connection.oauth.created` on this connection.
pub fn connection_oauth_created<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionOAuthCreatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionOAuthCreated, handler)
}
/// Registers a handler for `connection.updated` on this connection.
pub fn connection_updated<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<ConnectionUpdatedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::ConnectionUpdated, handler)
}
/// Registers a handler for `oauth.failed` on this connection.
pub fn oauth_failed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OAuthFailedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OAuthFailed, handler)
}
/// Registers a handler for `oauth.refreshed` on this connection.
pub fn oauth_refreshed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<OAuthRefreshedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::OAuthRefreshed, handler)
}
/// Registers a handler for `passthrough.executed` on this connection.
pub fn passthrough_executed<F, Fut, Output>(self, handler: F) -> Self
where
F: Fn(Delivery<PassthroughExecutedPayload>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Output> + Send + 'static,
Output: WebhookHandlerOutput + Send + 'static
{
self.register(WebhookEventType::PassthroughExecuted, handler)
}
}