use super::*;
use helix::RequestGet;
pub use types::{PredictionOutcome, PredictionOutcomeId, PredictionStatus};
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct GetPredictionsRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(default, setter(into))]
pub id: Vec<types::PredictionId>,
#[builder(default, setter(into))]
pub after: Option<helix::Cursor>,
#[builder(default, setter(into))]
pub first: Option<usize>,
}
#[derive(PartialEq, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Prediction {
pub id: types::PredictionId,
pub broadcaster_id: types::UserId,
pub broadcaster_name: types::DisplayName,
pub broadcaster_login: types::UserName,
pub title: String,
pub winning_outcome_id: Option<PredictionOutcomeId>,
pub outcomes: Vec<PredictionOutcome>,
pub prediction_window: i64,
pub status: PredictionStatus,
pub created_at: types::Timestamp,
pub ended_at: Option<types::Timestamp>,
pub locked_at: Option<types::Timestamp>,
}
impl Request for GetPredictionsRequest {
type Response = Vec<Prediction>;
const PATH: &'static str = "predictions";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] = &[twitch_oauth2::Scope::ChannelReadPredictions];
}
impl RequestGet for GetPredictionsRequest {}
impl helix::Paginated for GetPredictionsRequest {
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) { self.after = cursor; }
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetPredictionsRequest::builder()
.broadcaster_id("55696719")
.id(vec!["d6676d5c-c86e-44d2-bfc4-100fb48f0656".into()])
.build();
let data = br#"
{
"data": [
{
"id": "d6676d5c-c86e-44d2-bfc4-100fb48f0656",
"broadcaster_id": "55696719",
"broadcaster_name": "TwitchDev",
"broadcaster_login": "twitchdev",
"title": "Will there be any leaks today?",
"winning_outcome_id": null,
"outcomes": [
{
"id": "021e9234-5893-49b4-982e-cfe9a0aaddd9",
"title": "Yes",
"users": 0,
"channel_points": 0,
"top_predictors": null,
"color": "BLUE"
},
{
"id": "ded84c26-13cb-4b48-8cb5-5bae3ec3a66e",
"title": "No",
"users": 0,
"channel_points": 0,
"top_predictors": null,
"color": "PINK"
}
],
"prediction_window": 600,
"status": "ACTIVE",
"created_at": "2021-04-28T16:03:06.320848689Z",
"ended_at": null,
"locked_at": null
}
],
"pagination": {}
}
"#
.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/predictions?broadcaster_id=55696719&id=d6676d5c-c86e-44d2-bfc4-100fb48f0656"
);
dbg!(GetPredictionsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}