|
| 1 | +#![doc(alias = "extension.bits_transaction.create")] |
| 2 | +//! a user purchases a product in the extension. |
| 3 | +
|
| 4 | +use super::*; |
| 5 | +use crate::eventsub::{EventSubscription, EventType}; |
| 6 | + |
| 7 | +/// [`extension.bits_transaction.create`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#extensionbits_transactioncreate): a new transaction is created for a Twitch Extension. |
| 8 | +
|
| 9 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 10 | +#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))] |
| 11 | +#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))] |
| 12 | +#[non_exhaustive] |
| 13 | +pub struct ExtensionBitsTransactionCreateV1 { |
| 14 | + /// The ID of the extension client that you want to get bits transaction notifications for |
| 15 | + pub extension_client_id: types::ExtensionId, |
| 16 | +} |
| 17 | + |
| 18 | +impl ExtensionBitsTransactionCreateV1 { |
| 19 | + /// Get notifications when transactions are created for this extension client |
| 20 | + pub fn new(extension_client_id: impl Into<types::ExtensionId>) -> Self { |
| 21 | + Self { |
| 22 | + extension_client_id: extension_client_id.into(), |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl EventSubscription for ExtensionBitsTransactionCreateV1 { |
| 28 | + type Payload = ExtensionBitsTransactionCreateV1Payload; |
| 29 | + |
| 30 | + const EVENT_TYPE: EventType = EventType::ExtensionBitsTransactionCreate; |
| 31 | + #[cfg(feature = "twitch_oauth2")] |
| 32 | + const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![]; |
| 33 | + const VERSION: &'static str = "1"; |
| 34 | +} |
| 35 | + |
| 36 | +/// [`extension.bits_transaction.create`](ExtensionBitsTransactionCreateV1) response payload. |
| 37 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 38 | +#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))] |
| 39 | +#[non_exhaustive] |
| 40 | +pub struct ExtensionBitsTransactionCreateV1Payload { |
| 41 | + /// The ID of the bits transaction that was created |
| 42 | + pub id: types::BitsTransactionId, |
| 43 | + /// The ID of the extension client that created the bits transaction |
| 44 | + pub extension_client_id: types::ExtensionClientId, |
| 45 | + |
| 46 | + /// The transaction’s broadcaster ID. |
| 47 | + pub broadcaster_user_id: types::UserId, |
| 48 | + /// The broadcaster’s login name. |
| 49 | + pub broadcaster_user_login: types::UserName, |
| 50 | + /// The broadcaster’s display name. |
| 51 | + pub broadcaster_user_name: types::DisplayName, |
| 52 | + |
| 53 | + /// The transaction’s user ID. |
| 54 | + pub user_id: types::UserId, |
| 55 | + /// The user’s login name. |
| 56 | + pub user_login: types::UserName, |
| 57 | + /// The user’s display name. |
| 58 | + pub user_name: types::DisplayName, |
| 59 | + |
| 60 | + /// The product that was purchased in the transaction |
| 61 | + pub product: ExtensionBitsProduct, |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +#[test] |
| 66 | +fn parse_payload() { |
| 67 | + use crate::eventsub::Event; |
| 68 | + |
| 69 | + let payload = r##" |
| 70 | + { |
| 71 | + "subscription": { |
| 72 | + "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4", |
| 73 | + "type": "extension.bits_transaction.create", |
| 74 | + "version": "1", |
| 75 | + "status": "enabled", |
| 76 | + "cost": 0, |
| 77 | + "condition": { |
| 78 | + "extension_client_id": "deadbeef" |
| 79 | + }, |
| 80 | + "transport": { |
| 81 | + "method": "webhook", |
| 82 | + "callback": "https://example.com/webhooks/callback" |
| 83 | + }, |
| 84 | + "created_at": "2019-11-16T10:11:12.634234626Z" |
| 85 | + }, |
| 86 | + "event": { |
| 87 | + "id": "bits-tx-id", |
| 88 | + "extension_client_id": "deadbeef", |
| 89 | + "broadcaster_user_id": "1337", |
| 90 | + "broadcaster_user_login": "cool_user", |
| 91 | + "broadcaster_user_name": "Cool_User", |
| 92 | + "user_name": "Coolest_User", |
| 93 | + "user_login": "coolest_user", |
| 94 | + "user_id": "1236", |
| 95 | + "product": { |
| 96 | + "name": "great_product", |
| 97 | + "sku": "skuskusku", |
| 98 | + "bits": 1234, |
| 99 | + "in_development": false |
| 100 | + } |
| 101 | + } |
| 102 | + }"##; |
| 103 | + let val = Event::parse(payload).unwrap(); |
| 104 | + crate::tests::roundtrip(&val); |
| 105 | + |
| 106 | + let Event::ExtensionBitsTransactionCreateV1(_val) = val else { |
| 107 | + panic!("invalid event type"); |
| 108 | + }; |
| 109 | +} |
0 commit comments