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 GetVipsRequest<'a> {
#[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(default))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub user_id: types::Collection<'a, types::UserId>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub first: Option<usize>,
#[cfg_attr(feature = "typed-builder", builder(default))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub after: Option<Cow<'a, helix::CursorRef>>,
}
impl<'a> GetVipsRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
user_id: types::Collection::default(),
first: None,
after: None,
}
}
pub const fn first(mut self, first: usize) -> Self {
self.first = Some(first);
self
}
pub fn user_ids(self, user_ids: impl Into<types::Collection<'a, types::UserId>>) -> Self {
Self {
user_id: user_ids.into(),
..self
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Vip {
pub user_id: types::UserId,
pub user_name: types::DisplayName,
pub user_login: types::UserName,
}
impl Request for GetVipsRequest<'_> {
type Response = Vec<Vip>;
const PATH: &'static str = "channels/vips";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
twitch_oauth2::Scope::ChannelReadVips,
twitch_oauth2::Scope::ChannelManageVips
)];
}
impl helix::Paginated for GetVipsRequest<'_> {
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) {
self.after = cursor.map(|c| c.into_cow())
}
}
impl RequestGet for GetVipsRequest<'_> {}
#[cfg(test)]
#[test]
fn test_request_all() {
use helix::*;
let req = GetVipsRequest::broadcaster_id("123");
let data = br#"
{
"data": [
{
"user_id": "11111",
"user_name": "UserDisplayName",
"user_login": "userloginname"
}
],
"pagination": {
"cursor": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6NX19"
}
}"#
.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/vips?broadcaster_id=123"
);
dbg!(GetVipsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_request_multiple() {
use helix::*;
let ids: &[&types::UserIdRef] = &["456".into(), "678".into()];
let req = GetVipsRequest::broadcaster_id("123").user_ids(ids);
let data = br#"
{
"data": [
{
"user_id": "11111",
"user_name": "UserDisplayName",
"user_login": "userloginname"
}
],
"pagination": {
"cursor": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6NX19"
}
}"#
.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/vips?broadcaster_id=123&user_id=456&user_id=678"
);
dbg!(GetVipsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}