withone 0.1.2

Typed Rust client for the One API: the client, webhooks and the action type every withone-<platform> crate builds on.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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)
    }

}