use super::*;
use helix::RequestGet;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct GetHypeTrainStatusRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
}
impl<'a> GetHypeTrainStatusRequest<'a> {
pub fn new(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct HypeTrainStatus {
pub current: Option<HypeTrain>,
pub all_time_high: Option<HypeTrainRecord>,
pub shared_all_time_high: Option<HypeTrainRecord>,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct HypeTrain {
pub id: types::HypeTrainId,
pub broadcaster_user_id: types::UserId,
pub broadcaster_user_login: types::UserName,
pub broadcaster_user_name: types::DisplayName,
pub level: u64,
pub total: u64,
pub progress: u64,
pub goal: u64,
pub top_contributions: Vec<TopContribution>,
#[serde(default)]
pub shared_train_participants: Vec<SharedTrainParticipant>,
pub started_at: types::Timestamp,
pub expires_at: types::Timestamp,
#[serde(rename = "type")]
pub type_: HypeTrainType,
#[serde(default)]
pub is_shared_train: bool,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct TopContribution {
pub user_id: types::UserId,
pub user_login: types::UserName,
pub user_name: types::UserName,
#[serde(rename = "type")]
pub type_: HypeTrainContributionType,
pub total: u64,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct SharedTrainParticipant {
pub broadcaster_user_id: types::UserId,
pub broadcaster_user_login: types::UserName,
pub broadcaster_user_name: types::DisplayName,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct HypeTrainRecord {
pub level: u64,
pub total: u64,
pub achieved_at: types::Timestamp,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
pub enum HypeTrainType {
Treasure,
GoldenKappa,
Regular,
#[serde(untagged)]
Unknown(String),
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
pub enum HypeTrainContributionType {
Bits,
Subscription,
Other,
#[serde(untagged)]
Unknown(String),
}
impl Request for GetHypeTrainStatusRequest<'_> {
type PaginationData = ();
type Response = HypeTrainStatus;
const PATH: &'static str = "hypetrain/status";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelReadHypeTrain];
}
impl RequestGet for GetHypeTrainStatusRequest<'_> {
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestGetError>
where
Self: Sized,
{
helix::parse_single_return(request, uri, response, status)
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetHypeTrainStatusRequest::new("123");
let data = br#"
{
"data": [
{
"current": {
"id": "1b0AsbInCHZW2SQFQkCzqN07Ib2",
"broadcaster_user_id": "1337",
"broadcaster_user_login": "cool_user",
"broadcaster_user_name": "Cool_User",
"level": 2,
"total": 700,
"progress": 200,
"goal": 1000,
"top_contributions": [
{
"user_id": "123",
"user_login": "pogchamp",
"user_name": "PogChamp",
"type": "bits",
"total": 50
},
{
"user_id": "456",
"user_login": "kappa",
"user_name": "Kappa",
"type": "subscription",
"total": 45
}
],
"shared_train_participants": [
{
"broadcaster_user_id": "456",
"broadcaster_user_login": "pogchamp",
"broadcaster_user_name": "PogChamp"
},
{
"broadcaster_user_id": "321",
"broadcaster_user_login": "pogchamp",
"broadcaster_user_name": "PogChamp"
}
],
"started_at": "2020-07-15T17:16:03.17106713Z",
"expires_at": "2020-07-15T17:16:11.17106713Z",
"type": "golden_kappa"
},
"all_time_high": {
"level": 6,
"total": 2850,
"achieved_at": "2020-04-24T20:12:21.003802269Z"
},
"shared_all_time_high": {
"level": 16,
"total": 23850,
"achieved_at": "2020-04-27T20:12:21.003802269Z"
}
}
]
}
"#
.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/hypetrain/status?broadcaster_id=123"
);
dbg!(GetHypeTrainStatusRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_no_shared() {
use helix::*;
let req = GetHypeTrainStatusRequest::new("123");
let data = br#"
{
"data": [
{
"current": {
"id": "1b0AsbInCHZW2SQFQkCzqN07Ib2",
"broadcaster_user_id": "1337",
"broadcaster_user_login": "cool_user",
"broadcaster_user_name": "Cool_User",
"level": 2,
"total": 700,
"progress": 200,
"goal": 1000,
"top_contributions": [
{
"user_id": "123",
"user_login": "pogchamp",
"user_name": "PogChamp",
"type": "bits",
"total": 50
},
{
"user_id": "456",
"user_login": "kappa",
"user_name": "Kappa",
"type": "subscription",
"total": 45
}
],
"started_at": "2020-07-15T17:16:03.17106713Z",
"expires_at": "2020-07-15T17:16:11.17106713Z",
"type": "golden_kappa"
},
"all_time_high": {
"level": 6,
"total": 2850,
"achieved_at": "2020-04-24T20:12:21.003802269Z"
},
"shared_all_time_high": {
"level": 16,
"total": 23850,
"achieved_at": "2020-04-27T20:12:21.003802269Z"
}
}
]
}
"#
.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/hypetrain/status?broadcaster_id=123"
);
dbg!(GetHypeTrainStatusRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_empty() {
use helix::*;
let req = GetHypeTrainStatusRequest::new("123");
let data = br#"
{
"data": [
{
"current": null,
"all_time_high": null,
"shared_all_time_high": null
}
]
}
"#
.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/hypetrain/status?broadcaster_id=123"
);
dbg!(GetHypeTrainStatusRequest::parse_response(Some(req), &uri, http_response).unwrap());
}