use super::*;
use helix::RequestGet;
pub use types::{PredictionOutcome, PredictionOutcomeId, PredictionStatus};
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct GetPredictionsRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub id: types::Collection<'a, types::PredictionId>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub after: Option<Cow<'a, helix::CursorRef>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub first: Option<usize>,
}
impl<'a> GetPredictionsRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
id: types::Collection::default(),
after: Default::default(),
first: Default::default(),
}
}
pub fn ids(mut self, ids: impl Into<types::Collection<'a, types::PredictionId>>) -> Self {
self.id = ids.into();
self
}
}
#[derive(PartialEq, Eq, 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: twitch_oauth2::Validator =
twitch_oauth2::validator![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.map(|c| c.into_cow())
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetPredictionsRequest::broadcaster_id("55696719")
.ids(vec!["d6676d5c-c86e-44d2-bfc4-100fb48f0656"]);
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());
}