use super::*;
use helix::RequestPost;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug, Default)]
#[non_exhaustive]
pub struct CreatePredictionRequest {}
impl CreatePredictionRequest {
pub fn new() -> Self { Self {} }
}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct CreatePredictionBody {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(setter(into))]
pub title: String,
pub outcomes: (NewPredictionOutcome, NewPredictionOutcome),
pub prediction_window: i64,
}
impl helix::private::SealedSerialize for CreatePredictionBody {}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct NewPredictionOutcome {
pub title: String,
}
impl NewPredictionOutcome {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
}
}
pub fn new_tuple(blue: impl Into<String>, pink: impl Into<String>) -> (Self, Self) {
(Self::new(blue), Self::new(pink))
}
}
pub type CreatePredictionResponse = super::Prediction;
impl Request for CreatePredictionRequest {
type Response = CreatePredictionResponse;
const PATH: &'static str = "predictions";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ChannelManagePredictions];
}
impl RequestPost for CreatePredictionRequest {
type Body = CreatePredictionBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response_str: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPostError>
where
Self: Sized,
{
let response: helix::InnerResponse<Vec<Self::Response>> =
helix::parse_json(response_str, true).map_err(|e| {
helix::HelixRequestPostError::DeserializeError(
response_str.to_string(),
e,
uri.clone(),
status,
)
})?;
let data = response.data.into_iter().next().ok_or_else(|| {
helix::HelixRequestPostError::InvalidResponse {
reason: "response included no data",
response: response_str.to_string(),
status,
uri: uri.clone(),
}
})?;
Ok(helix::Response {
data,
pagination: response.pagination.cursor,
request,
total: None,
other: None,
})
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = CreatePredictionRequest::builder().build();
let body = CreatePredictionBody::builder()
.broadcaster_id("141981764")
.title("Any leeks in the stream?")
.outcomes(NewPredictionOutcome::new_tuple(
"Yes, give it time.",
"Definitely not.",
))
.prediction_window(120)
.build();
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br##"
{
"data": [
{
"id": "bc637af0-7766-4525-9308-4112f4cbf178",
"broadcaster_id": "141981764",
"broadcaster_name": "TwitchDev",
"broadcaster_login": "twitchdev",
"title": "Any leeks in the stream?",
"winning_outcome_id": null,
"outcomes": [
{
"id": "73085848-a94d-4040-9d21-2cb7a89374b7",
"title": "Yes, give it time.",
"users": 0,
"channel_points": 0,
"top_predictors": null,
"color": "BLUE"
},
{
"id": "906b70ba-1f12-47ea-9e95-e5f93d20e9cc",
"title": "Definitely not.",
"users": 0,
"channel_points": 0,
"top_predictors": null,
"color": "PINK"
}
],
"prediction_window": 120,
"status": "ACTIVE",
"created_at": "2021-04-28T17:11:22.595914172Z",
"ended_at": null,
"locked_at": null
}
]
}
"##
.to_vec();
let http_response = http::Response::builder().status(200).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(uri.to_string(), "https://api.twitch.tv/helix/predictions?");
dbg!(CreatePredictionRequest::parse_response(Some(req), &uri, http_response).unwrap());
}