use super::*;
use helix::RequestGet;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct GetBroadcasterSubscriptionsRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(default)]
pub user_id: Vec<types::UserId>,
#[builder(default)]
pub after: Option<helix::Cursor>,
#[builder(setter(into), default)]
pub first: Option<String>,
}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct BroadcasterSubscription {
pub broadcaster_id: types::UserId,
pub broadcaster_login: types::UserName,
pub broadcaster_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_login: Option<types::UserName>,
#[serde(
default,
deserialize_with = "helix::deserialize_none_from_empty_string"
)]
pub gifter_name: Option<types::DisplayName>,
pub is_gift: bool,
pub tier: types::SubscriptionTier,
pub plan_name: String,
pub user_id: types::UserId,
pub user_login: types::UserName,
pub user_name: types::DisplayName,
}
impl Request for GetBroadcasterSubscriptionsRequest {
type Response = Vec<BroadcasterSubscription>;
const PATH: &'static str = "subscriptions";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ChannelReadSubscriptions];
}
impl RequestGet for GetBroadcasterSubscriptionsRequest {}
impl helix::Paginated for GetBroadcasterSubscriptionsRequest {
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) { self.after = cursor }
}
impl helix::Response<GetBroadcasterSubscriptionsRequest, Vec<BroadcasterSubscription>> {
pub fn points(&self) -> Result<i64, BroadcasterSubscriptionPointsError> {
let points = self.get_other("points")?;
if let Some(points) = points {
Ok(points)
} else {
Err(BroadcasterSubscriptionPointsError::PointsNotFound)
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum BroadcasterSubscriptionPointsError {
#[error(transparent)]
DeserError(#[from] serde_json::Error),
#[error("`points` not found in the response")]
PointsNotFound,
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetBroadcasterSubscriptionsRequest::builder()
.broadcaster_id("123".to_string())
.build();
let data = br#"
{
"data": [
{
"broadcaster_id": "141981764",
"broadcaster_login": "twitchdev",
"broadcaster_name": "TwitchDev",
"gifter_id": "12826",
"gifter_login": "twitch",
"gifter_name": "Twitch",
"is_gift": true,
"tier": "1000",
"plan_name": "Channel Subscription (twitchdev)",
"user_id": "527115020",
"user_name": "twitchgaming",
"user_login": "twitchgaming"
}
],
"pagination": {
"cursor": "xxxx"
},
"total": 13,
"points": 13
}
"#
.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?broadcaster_id=123"
);
let resp =
dbg!(
GetBroadcasterSubscriptionsRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
);
assert_eq!(resp.total, Some(13));
assert_eq!(resp.points().unwrap(), 13);
}