use super::*;
use helix::RequestGet;
#[derive(PartialEq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct GetStreamsRequest<'a> {
#[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_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 game_id: types::Collection<'a, types::CategoryId>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub language: Option<Cow<'a, str>>,
#[cfg_attr(
feature = "typed-builder",
builder(default_code = "types::Collection::default()", setter(into))
)]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub user_id: types::Collection<'a, types::UserId>,
#[cfg_attr(
feature = "typed-builder",
builder(default_code = "types::Collection::default()", setter(into))
)]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub user_login: types::Collection<'a, types::UserName>,
}
impl<'a> GetStreamsRequest<'a> {
pub fn user_ids(user_ids: impl Into<types::Collection<'a, types::UserId>>) -> Self {
Self {
user_id: user_ids.into(),
..Self::default()
}
}
pub fn user_logins(user_logins: impl Into<types::Collection<'a, types::UserName>>) -> Self {
Self {
user_login: user_logins.into(),
..Self::default()
}
}
pub const fn first(mut self, first: usize) -> Self {
self.first = Some(first);
self
}
pub fn game_ids(game_ids: impl Into<types::Collection<'a, types::CategoryId>>) -> Self {
Self {
game_id: game_ids.into(),
..Self::default()
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Stream {
pub game_id: types::CategoryId,
pub game_name: String,
pub id: types::StreamId,
pub language: String,
pub is_mature: bool,
pub started_at: types::Timestamp,
#[serde(deserialize_with = "helix::deserialize_default_from_null", default)]
#[deprecated(note = "use `tags` instead")]
pub tag_ids: Vec<types::TagId>,
#[serde(deserialize_with = "helix::deserialize_default_from_null")]
pub tags: Vec<String>,
pub thumbnail_url: String,
pub title: String,
#[serde(rename = "type")]
pub type_: StreamType,
pub user_id: types::UserId,
pub user_name: types::DisplayName,
pub user_login: types::UserName,
pub viewer_count: usize,
}
impl Request for GetStreamsRequest<'_> {
type Response = Vec<Stream>;
const PATH: &'static str = "streams";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
}
impl RequestGet for GetStreamsRequest<'_> {}
impl helix::Paginated for GetStreamsRequest<'_> {
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 = GetStreamsRequest::default();
let data = r#"
{
"data": [
{
"id": "123456789",
"user_id": "98765",
"user_login": "sandysanderman",
"user_name": "SandySanderman",
"game_id": "494131",
"game_name": "Little Nightmares",
"type": "live",
"title": "hablamos y le damos a Little Nightmares 1",
"tags": ["Español"],
"viewer_count": 78365,
"started_at": "2021-03-10T15:04:21Z",
"language": "es",
"thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_auronplay-{width}x{height}.jpg",
"tag_ids": [],
"is_mature": false
},
{
"id": "123456789",
"user_id": "98765",
"user_login": "sandysanderman",
"user_name": "SandySanderman",
"game_id": "494131",
"game_name": "Little Nightmares",
"type": "live",
"title": "hablamos y le damos a Little Nightmares 1",
"tags": ["Español"],
"viewer_count": 78365,
"started_at": "2021-03-10T15:04:21Z",
"language": "es",
"thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_auronplay-{width}x{height}.jpg",
"tag_ids": [],
"is_mature": false
}
],
"pagination": {
"cursor": "eyJiIjp7IkN1cnNvciI6ImV5SnpJam8zT0RNMk5TNDBORFF4TlRjMU1UY3hOU3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqb3hOVGs0TkM0MU56RXhNekExTVRZNU1ESXNJbVFpT21aaGJITmxMQ0owSWpwMGNuVmxmUT09In19"
}
}
"#
.as_bytes().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/streams?");
dbg!(GetStreamsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_request_null_tags_issue184() {
use helix::*;
let req = GetStreamsRequest::default();
let data = br#"
{
"data": [
{
"id": "123456789",
"user_id": "98765",
"user_login": "sandysanderman",
"user_name": "SandySanderman",
"game_id": "494131",
"game_name": "Little Nightmares",
"type": "live",
"title": "hablamos y le damos a Little Nightmares 1",
"tags": null,
"viewer_count": 78365,
"started_at": "2021-03-10T15:04:21Z",
"language": "es",
"thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_auronplay-{width}x{height}.jpg",
"tag_ids": null,
"is_mature": false
}
],
"pagination": {
"cursor": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ=="
}
}
"#
.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/streams?");
dbg!(GetStreamsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}