use crate::helix::{parse_json, HelixRequestPatchError};
use super::*;
use helix::RequestPatch;
pub use types::{PredictionId, PredictionStatus};
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug, Default)]
#[non_exhaustive]
pub struct EndPredictionRequest {}
impl EndPredictionRequest {
pub fn new() -> Self { Self {} }
}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct EndPredictionBody {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(setter(into))]
pub id: PredictionId,
pub status: PredictionStatus,
#[builder(default, setter(into))]
pub winning_outcome_id: Option<PredictionId>,
}
impl helix::private::SealedSerialize for EndPredictionBody {}
#[derive(PartialEq, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub enum EndPrediction {
Success(Prediction),
MissingQuery,
AuthFailed,
}
impl Request for EndPredictionRequest {
type Response = EndPrediction;
const PATH: &'static str = "predictions";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ChannelManagePredictions];
}
impl RequestPatch for EndPredictionRequest {
type Body = EndPredictionBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPatchError>
where
Self: Sized,
{
let resp = match status {
http::StatusCode::OK => {
let resp: helix::InnerResponse<Vec<Prediction>> = parse_json(response, true)
.map_err(|e| {
HelixRequestPatchError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
EndPrediction::Success(resp.data.into_iter().next().ok_or(
helix::HelixRequestPatchError::InvalidResponse {
reason: "expected at least one element in data",
response: response.to_string(),
status,
uri: uri.clone(),
},
)?)
}
http::StatusCode::BAD_REQUEST => EndPrediction::MissingQuery,
http::StatusCode::UNAUTHORIZED => EndPrediction::AuthFailed,
_ => {
return Err(helix::HelixRequestPatchError::InvalidResponse {
reason: "unexpected status code",
response: response.to_string(),
status,
uri: uri.clone(),
})
}
};
Ok(helix::Response {
data: resp,
pagination: None,
request,
total: None,
other: None,
})
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = EndPredictionRequest::builder().build();
let body = EndPredictionBody::builder()
.broadcaster_id("141981764")
.id("ed961efd-8a3f-4cf5-a9d0-e616c590cd2a")
.status(PredictionStatus::Resolved)
.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": "Will we win all the games?",
"winning_outcome_id": "73085848-a94d-4040-9d21-2cb7a89374b7",
"outcomes": [
{
"id": "73085848-a94d-4040-9d21-2cb7a89374b7",
"title": "yes",
"users": 0,
"channel_points": 0,
"top_predictors": null,
"color": "BLUE"
},
{
"id": "86010b2e-9764-4136-9359-fd1c9c5a8033",
"title": "no",
"users": 0,
"channel_points": 0,
"top_predictors": null,
"color": "PINK"
}
],
"prediction_window": 120,
"status": "RESOLVED",
"created_at": "2021-04-28T21:48:19.480371331Z",
"ended_at": "2021-04-28T21:54:24.026833954Z",
"locked_at": "2021-04-28T21:48:34.636685705Z"
}
]
}
"##
.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!(EndPredictionRequest::parse_response(Some(req), &uri, http_response).unwrap());
}