use super::*;
use helix::RequestPost;
#[derive(PartialEq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct CreateClipFromVodRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub editor_id: Cow<'a, types::UserIdRef>,
#[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 vod_id: Cow<'a, types::VideoIdRef>,
pub vod_offset: u64,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub duration: Option<f32>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub title: Cow<'a, str>,
}
impl<'a> CreateClipFromVodRequest<'a> {
pub fn new(
editor_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
vod_id: impl types::IntoCow<'a, types::VideoIdRef> + 'a,
vod_offset: u64,
title: impl Into<Cow<'a, str>>,
) -> Self {
Self {
editor_id: editor_id.into_cow(),
broadcaster_id: broadcaster_id.into_cow(),
vod_id: vod_id.into_cow(),
vod_offset,
duration: None,
title: title.into(),
}
}
pub const fn duration(mut self, duration: f32) -> Self {
self.duration = Some(duration);
self
}
}
pub type CreatedClip = super::CreatedClip;
impl Request for CreateClipFromVodRequest<'_> {
type PaginationData = ();
type Response = CreatedClip;
const PATH: &'static str = "videos/clips";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
twitch_oauth2::Scope::EditorManageClips,
twitch_oauth2::Scope::ChannelManageClips
)];
}
impl RequestPost for CreateClipFromVodRequest<'_> {
type Body = helix::EmptyBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestPostError>
where
Self: Sized,
{
helix::parse_single_return(request, uri, response, status)
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = CreateClipFromVodRequest::new("12345", "67890", "abcde", 64, "title");
let data = br#"
{
"data":
[{
"id": "FiveWordsForClipSlug",
"edit_url": "https://www.twitch.tv/twitchdev/clip/FiveWordsForClipSlug"
}]
}
"#
.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/videos/clips?editor_id=12345&broadcaster_id=67890&vod_id=abcde&vod_offset=64&title=title"
);
dbg!(CreateClipFromVodRequest::parse_response(Some(req), &uri, http_response).unwrap());
}