use std::marker::PhantomData;
use super::*;
use helix::RequestPost;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[must_use]
#[non_exhaustive]
pub struct StartCommercialRequest<'a> {
#[serde(skip)]
_marker: PhantomData<&'a ()>,
}
impl StartCommercialRequest<'_> {
pub fn new() -> Self { StartCommercialRequest::default() }
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct StartCommercialBody<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
pub length: types::CommercialLength,
}
impl<'a> StartCommercialBody<'a> {
pub fn new(
broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
length: impl Into<types::CommercialLength>,
) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
length: length.into(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct StartCommercial {
pub length: types::CommercialLength,
pub message: String,
pub retry_after: u64,
}
impl Request for StartCommercialRequest<'_> {
type Response = Vec<StartCommercial>;
const PATH: &'static str = "channels/commercial";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelEditCommercial];
}
impl<'a> RequestPost for StartCommercialRequest<'a> {
type Body = StartCommercialBody<'a>;
}
impl helix::private::SealedSerialize for StartCommercialBody<'_> {}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = StartCommercialRequest::default();
let body = StartCommercialBody::new("41245072", types::CommercialLength::Length60);
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"broadcaster_id":"41245072","length":60}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": [{
"length" : 60,
"message" : "",
"retry_after" : 480
}]
}
"#
.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/channels/commercial?"
);
dbg!(StartCommercialRequest::parse_response(Some(req), &uri, http_response).unwrap());
}