use super::*;
use helix::RequestGet;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct GetBroadcasterSubscriptionsEventsRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(default)]
pub user_id: Vec<types::UserId>,
#[builder(default)]
pub after: Option<helix::Cursor>,
#[builder(default, setter(into))]
pub first: Option<usize>,
#[builder(default, setter(into))]
pub id: Option<String>,
}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct BroadcasterSubscriptionEvent {
pub id: String,
pub event_type: BroadcasterSubscriptionEventType,
pub event_timestamp: types::Timestamp,
pub version: String,
pub event_data: BroadcasterSubscriptionEventData,
}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[non_exhaustive]
pub enum BroadcasterSubscriptionEventType {
#[serde(rename = "subscriptions.subscribe")]
Subscribe,
#[serde(rename = "subscriptions.unsubscribe")]
Unsubscribe,
#[serde(rename = "subscriptions.notification")]
Notification,
}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct BroadcasterSubscriptionEventData {
pub broadcaster_id: types::UserId,
pub broadcaster_name: types::DisplayName,
pub is_gift: bool,
#[serde(deserialize_with = "subscription_tier")]
pub tier: Option<types::SubscriptionTier>,
#[serde(
default,
deserialize_with = "helix::deserialize_none_from_empty_string"
)]
pub plan_name: Option<String>,
pub user_id: types::UserId,
pub user_name: types::DisplayName,
#[serde(
default,
deserialize_with = "helix::deserialize_none_from_empty_string"
)]
pub gifter_id: Option<types::UserId>,
#[serde(
default,
deserialize_with = "helix::deserialize_none_from_empty_string"
)]
pub gifter_name: Option<types::DisplayName>,
}
fn subscription_tier<'de, D>(deserializer: D) -> Result<Option<types::SubscriptionTier>, D::Error>
where D: serde::de::Deserializer<'de> {
Ok(match types::SubscriptionTier::deserialize(deserializer)? {
types::SubscriptionTier::Other(s) if s.is_empty() => None,
other => Some(other),
})
}
impl Request for GetBroadcasterSubscriptionsEventsRequest {
type Response = Vec<BroadcasterSubscriptionEvent>;
const PATH: &'static str = "subscriptions/events";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ChannelReadSubscriptions];
}
impl RequestGet for GetBroadcasterSubscriptionsEventsRequest {}
impl helix::Paginated for GetBroadcasterSubscriptionsEventsRequest {
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) { self.after = cursor }
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetBroadcasterSubscriptionsEventsRequest::builder()
.broadcaster_id("1337".to_string())
.build();
let data = br#"
{
"data": [
{
"id": "1mZCpIomSWc9PR2Ldeadbeef",
"event_type": "subscriptions.subscribe",
"event_timestamp": "2021-01-03T16:38:27Z",
"version": "1.0",
"event_data": {
"broadcaster_id": "1337",
"broadcaster_name": "justintv",
"gifter_id": "",
"gifter_name": "",
"is_gift": false,
"plan_name": "Channel Subscription (justintv)",
"tier": "1000",
"user_id": "1336",
"user_name": "twitchuser"
}
},
{
"id": "1mY9qZVbbl77PpGydeadbeef",
"event_type": "subscriptions.unsubscribe",
"event_timestamp": "2021-01-03T07:44:08Z",
"version": "1.0",
"event_data": {
"broadcaster_id": "1337",
"broadcaster_name": "justintv",
"gifter_id": "",
"gifter_name": "",
"is_gift": false,
"plan_name": "",
"tier": "",
"user_id": "1336",
"user_name": "twitchuser"
}
}, {
"id": "1mRxcgkkAVfej5n7deadbeef",
"event_type": "subscriptions.notification",
"event_timestamp": "2021-01-01T03:04:45Z",
"version": "1.0",
"event_data": {
"broadcaster_id": "1337",
"broadcaster_name": "justintv",
"gifter_id": "",
"gifter_name": "",
"is_gift": false,
"plan_name": "Channel Subscription (justintv)",
"tier": "1000",
"user_id": "1336",
"user_name": "twitchuser"
}
}
]
}
"#
.to_vec();
let http_response = http::Response::builder().body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/subscriptions/events?broadcaster_id=1337"
);
dbg!(
GetBroadcasterSubscriptionsEventsRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
);
}