use std::marker::PhantomData;
use crate::helix::{parse_json, HelixRequestPatchError};
use super::*;
use helix::RequestPatch;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct EndPredictionRequest<'a> {
#[serde(skip)]
_marker: PhantomData<&'a ()>,
}
impl EndPredictionRequest<'_> {
pub fn new() -> Self { Self::default() }
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct EndPredictionBody<'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(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub id: Cow<'a, types::PredictionIdRef>,
pub status: types::PredictionStatus,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub winning_outcome_id: Option<Cow<'a, types::PredictionIdRef>>,
}
impl<'a> EndPredictionBody<'a> {
pub fn new(
broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
id: impl types::IntoCow<'a, types::PredictionIdRef> + 'a,
status: impl Into<types::PredictionStatus>,
) -> Self {
Self {
broadcaster_id: broadcaster_id.to_cow(),
id: id.to_cow(),
status: status.into(),
winning_outcome_id: None,
}
}
pub fn winning_outcome_id(
mut self,
winning_outcome_id: impl types::IntoCow<'a, types::PredictionIdRef> + 'a,
) -> Self {
self.winning_outcome_id = Some(winning_outcome_id.to_cow());
self
}
}
impl helix::private::SealedSerialize for EndPredictionBody<'_> {}
#[derive(PartialEq, Eq, 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<'a> RequestPatch for EndPredictionRequest<'a> {
type Body = EndPredictionBody<'a>;
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::new();
let body = EndPredictionBody::new(
"141981764",
"ed961efd-8a3f-4cf5-a9d0-e616c590cd2a",
types::PredictionStatus::Resolved,
);
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());
}