use super::*;
use helix::RequestPost;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug, Default)]
#[non_exhaustive]
pub struct CreatePollRequest {}
impl CreatePollRequest {
pub fn new() -> Self { Self {} }
}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct CreatePollBody {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(setter(into))]
pub title: String,
pub duration: i64,
pub choices: Vec<NewPollChoice>,
#[builder(default, setter(into))]
pub bits_voting_enabled: Option<bool>,
#[builder(default, setter(into))]
pub bits_per_vote: Option<i64>,
#[builder(default, setter(into))]
pub channel_points_voting_enabled: Option<bool>,
#[builder(default, setter(into))]
pub channel_points_per_vote: Option<i64>,
}
impl helix::private::SealedSerialize for CreatePollBody {}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct NewPollChoice {
pub title: String,
}
impl NewPollChoice {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
}
}
}
pub type CreatePollResponse = super::Poll;
impl Request for CreatePollRequest {
type Response = CreatePollResponse;
const PATH: &'static str = "polls";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] = &[twitch_oauth2::Scope::ChannelManagePolls];
}
impl RequestPost for CreatePollRequest {
type Body = CreatePollBody;
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 = CreatePollRequest::builder().build();
let body = CreatePollBody::builder()
.broadcaster_id("141981764")
.title("Heads or Tails?")
.choices(vec![
NewPollChoice::new("Heads"),
NewPollChoice::new("Tails"),
])
.channel_points_voting_enabled(true)
.channel_points_per_vote(100)
.duration(1800)
.build();
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br##"
{
"data": [
{
"id": "ed961efd-8a3f-4cf5-a9d0-e616c590cd2a",
"broadcaster_id": "141981764",
"broadcaster_name": "TwitchDev",
"broadcaster_login": "twitchdev",
"title": "Heads or Tails?",
"choices": [
{
"id": "4c123012-1351-4f33-84b7-43856e7a0f47",
"title": "Heads",
"votes": 0,
"channel_points_votes": 0,
"bits_votes": 0
},
{
"id": "279087e3-54a7-467e-bcd0-c1393fcea4f0",
"title": "Tails",
"votes": 0,
"channel_points_votes": 0,
"bits_votes": 0
}
],
"bits_voting_enabled": false,
"bits_per_vote": 0,
"channel_points_voting_enabled": true,
"channel_points_per_vote": 100,
"status": "ACTIVE",
"duration": 1800,
"started_at": "2021-03-19T06:08:33.871278372Z"
}
]
}
"##
.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/polls?");
dbg!(CreatePollRequest::parse_response(Some(req), &uri, http_response).unwrap());
}