use super::*;
use helix::RequestGet;
pub use types::{PollChoice, PollStatus};
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct GetPollsRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(default, setter(into))]
pub id: Vec<types::PollId>,
#[builder(default, setter(into))]
pub after: Option<helix::Cursor>,
#[builder(default, setter(into))]
pub first: Option<usize>,
}
#[derive(PartialEq, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Poll {
pub id: types::PollId,
pub broadcaster_id: types::UserId,
pub broadcaster_name: types::UserName,
pub broadcaster_login: types::DisplayName,
pub title: String,
pub choices: Vec<PollChoice>,
pub bits_voting_enabled: bool,
pub bits_per_vote: i64,
pub channel_points_voting_enabled: bool,
pub channel_points_per_vote: i64,
pub status: PollStatus,
pub duration: i64,
pub started_at: types::Timestamp,
pub ended_at: Option<types::Timestamp>,
}
impl Request for GetPollsRequest {
type Response = Vec<Poll>;
const PATH: &'static str = "polls";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] = &[twitch_oauth2::Scope::ChannelReadPolls];
}
impl RequestGet for GetPollsRequest {}
impl helix::Paginated for GetPollsRequest {
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) { self.after = cursor; }
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetPollsRequest::builder()
.broadcaster_id("141981764")
.id(vec!["ed961efd-8a3f-4cf5-a9d0-e616c590cd2a".into()])
.build();
let data = br#"
{
"data": [
{
"id": "ed961efd-8a3f-4cf5-a9d0-e616c590cd2a",
"broadcaster_id": "55696719",
"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": false,
"channel_points_per_vote": 0,
"status": "ACTIVE",
"duration": 1800,
"started_at": "2021-03-19T06:08:33.871278372Z"
}
],
"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/polls?broadcaster_id=141981764&id=ed961efd-8a3f-4cf5-a9d0-e616c590cd2a"
);
dbg!(GetPollsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}