use super::*;
use helix::RequestPost;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct CreateChannelStreamScheduleSegmentRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
}
impl<'a> CreateChannelStreamScheduleSegmentRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct CreateChannelStreamScheduleSegmentBody<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub start_time: Cow<'a, types::TimestampRef>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub timezone: Cow<'a, str>,
pub is_recurring: bool,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub duration: Option<Cow<'a, str>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub category_id: Option<Cow<'a, types::CategoryIdRef>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub title: Option<Cow<'a, str>>,
}
impl<'a> CreateChannelStreamScheduleSegmentBody<'a> {
pub fn new(
start_time: impl types::IntoCow<'a, types::TimestampRef> + 'a,
timezone: impl Into<Cow<'a, str>>,
is_recurring: bool,
) -> Self {
Self {
start_time: start_time.into_cow(),
timezone: timezone.into(),
is_recurring,
duration: Default::default(),
category_id: Default::default(),
title: Default::default(),
}
}
}
impl helix::private::SealedSerialize for CreateChannelStreamScheduleSegmentBody<'_> {}
pub type CreateChannelStreamScheduleSegmentResponse = ScheduledBroadcasts;
impl Request for CreateChannelStreamScheduleSegmentRequest<'_> {
type Response = CreateChannelStreamScheduleSegmentResponse;
const PATH: &'static str = "schedule/segment";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelManageSchedule];
}
impl<'a> RequestPost for CreateChannelStreamScheduleSegmentRequest<'a> {
type Body = CreateChannelStreamScheduleSegmentBody<'a>;
}
#[cfg(test)]
#[test]
fn test_request() {
use std::convert::TryFrom;
use helix::*;
let req = CreateChannelStreamScheduleSegmentRequest::broadcaster_id("141981764");
let ts = types::Timestamp::try_from("2021-07-01T18:00:00Z").unwrap();
let body = CreateChannelStreamScheduleSegmentBody {
duration: Some("60".into()),
category_id: Some(types::IntoCow::into_cow("509670")),
title: Some("TwitchDev Monthly Update // July 1, 2021".into()),
..CreateChannelStreamScheduleSegmentBody::new(&*ts, "America/New_York", false)
};
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"start_time":"2021-07-01T18:00:00Z","timezone":"America/New_York","is_recurring":false,"duration":"60","category_id":"509670","title":"TwitchDev Monthly Update // July 1, 2021"}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": {
"segments": [
{
"id": "eyJzZWdtZW50SUQiOiJlNGFjYzcyNC0zNzFmLTQwMmMtODFjYS0yM2FkYTc5NzU5ZDQiLCJpc29ZZWFyIjoyMDIxLCJpc29XZWVrIjoyNn0=",
"start_time": "2021-07-01T18:00:00Z",
"end_time": "2021-07-01T19:00:00Z",
"title": "TwitchDev Monthly Update // July 1, 2021",
"canceled_until": null,
"category": {
"id": "509670",
"name": "Science & Technology"
},
"is_recurring": false
}
],
"broadcaster_id": "141981764",
"broadcaster_name": "TwitchDev",
"broadcaster_login": "twitchdev",
"vacation": null
}
}
"#.to_vec();
let http_response = http::Response::builder().status(204).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/schedule/segment?broadcaster_id=141981764"
);
dbg!(
CreateChannelStreamScheduleSegmentRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
);
}