use super::*;
use helix::RequestGet;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct GetVideosRequest<'a> {
#[cfg_attr(
feature = "typed-builder",
builder(default_code = "types::Collection::default()", setter(into))
)]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub id: types::Collection<'a, types::VideoId>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub user_id: Option<Cow<'a, types::UserIdRef>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub game_id: Option<Cow<'a, types::CategoryIdRef>>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub after: Option<Cow<'a, helix::CursorRef>>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub before: Option<Cow<'a, helix::CursorRef>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub first: Option<usize>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub language: Option<Cow<'a, str>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub period: Option<VideoPeriod>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub sort: Option<Sort>,
#[serde(rename = "type")]
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub type_: Option<VideoTypeFilter>,
}
impl<'a> GetVideosRequest<'a> {
pub fn ids(ids: impl Into<types::Collection<'a, types::VideoId>>) -> Self {
Self {
id: ids.into(),
..Self::default()
}
}
pub fn user_id(user_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
user_id: Some(user_id.into_cow()),
..Self::default()
}
}
pub fn game_id(game_id: impl types::IntoCow<'a, types::CategoryIdRef> + 'a) -> Self {
Self {
game_id: Some(game_id.into_cow()),
..Self::default()
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Video {
pub created_at: types::Timestamp,
pub description: String,
pub duration: String,
pub id: types::VideoId,
pub language: String,
#[serde(deserialize_with = "helix::deserialize_default_from_null")]
pub muted_segments: Vec<MutedSegment>,
pub published_at: types::Timestamp,
pub stream_id: Option<types::StreamId>,
pub thumbnail_url: String,
pub title: String,
#[serde(rename = "type")]
pub type_: types::VideoType,
pub url: String,
pub user_id: types::UserId,
pub user_name: types::DisplayName,
pub user_login: types::UserName,
pub view_count: i64,
pub viewable: types::VideoPrivacy,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct MutedSegment {
pub duration: i64,
pub offset: i64,
}
impl Request for GetVideosRequest<'_> {
type Response = Vec<Video>;
const PATH: &'static str = "videos";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
}
impl RequestGet for GetVideosRequest<'_> {}
impl helix::Paginated for GetVideosRequest<'_> {
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) {
self.after = cursor.map(|c| c.into_cow())
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetVideosRequest::ids(vec!["234482848"]);
let data = br#"
{
"data": [
{
"id": "335921245",
"stream_id": null,
"user_id": "141981764",
"user_login": "twitchdev",
"user_name": "TwitchDev",
"title": "Twitch Developers 101",
"description": "Welcome to Twitch development! Here is a quick overview of our products and information to help you get started.",
"created_at": "2018-11-14T21:30:18Z",
"published_at": "2018-11-14T22:04:30Z",
"url": "https://www.twitch.tv/videos/335921245",
"thumbnail_url": "https://static-cdn.jtvnw.net/cf_vods/d2nvs31859zcd8/twitchdev/335921245/ce0f3a7f-57a3-4152-bc06-0c6610189fb3/thumb/index-0000000000-%{width}x%{height}.jpg",
"viewable": "public",
"view_count": 1863062,
"language": "en",
"type": "upload",
"duration": "3m21s",
"muted_segments": [
{
"duration": 30,
"offset": 120
}
]
}
],
"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/videos?id=234482848"
);
dbg!(GetVideosRequest::parse_response(Some(req), &uri, http_response).unwrap());
}