use super::*;
use helix::RequestGet;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct SearchChannelsRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub query: Cow<'a, str>,
#[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, setter(into)))]
pub first: Option<usize>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub live_only: Option<bool>,
}
impl<'a> SearchChannelsRequest<'a> {
pub fn query(query: impl Into<Cow<'a, str>>) -> Self {
Self {
query: query.into(),
after: None,
first: None,
live_only: None,
}
}
pub const fn live_only(mut self, live_only: bool) -> Self {
self.live_only = Some(live_only);
self
}
pub const fn first(mut self, first: usize) -> Self {
self.first = Some(first);
self
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Channel {
pub game_id: types::CategoryId,
pub game_name: String,
pub id: types::UserId,
pub display_name: types::DisplayName,
pub broadcaster_language: String,
pub broadcaster_login: types::UserName,
pub title: String,
pub thumbnail_url: String,
pub is_live: bool,
#[serde(
default,
deserialize_with = "crate::deserialize_none_from_empty_string"
)]
pub started_at: Option<types::Timestamp>,
#[deprecated(note = "use `tags` instead")]
pub tag_ids: Vec<types::TagId>,
pub tags: Vec<String>,
}
impl Request for SearchChannelsRequest<'_> {
type Response = Vec<Channel>;
const PATH: &'static str = "search/channels";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
}
impl RequestGet for SearchChannelsRequest<'_> {}
impl helix::Paginated for SearchChannelsRequest<'_> {
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 = SearchChannelsRequest::query("fort");
let data = br#"
{
"data": [
{
"broadcaster_language": "en",
"broadcaster_login": "loserfruit",
"display_name": "Loserfruit",
"game_id": "498000",
"game_name": "House Flipper",
"id": "41245072",
"is_live": false,
"tag_ids": [],
"tags": [],
"thumbnail_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/fd17325a-7dc2-46c6-8617-e90ec259501c-profile_image-300x300.png",
"title": "loserfruit",
"started_at": ""
},
{
"broadcaster_language": "en",
"broadcaster_login": "a_seagull",
"display_name": "A_Seagull",
"game_id": "506442",
"game_name": "DOOM Eternal",
"id": "19070311",
"is_live": true,
"tag_ids": [],
"tags": ["English"],
"thumbnail_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/a_seagull-profile_image-4d2d235688c7dc66-300x300.png",
"title": "a_seagull",
"started_at": "2020-03-18T17:56:00Z"
}
],
"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/search/channels?query=fort"
);
dbg!(SearchChannelsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}