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 GetChannelFollowersRequest<'a> {
#[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(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"))]
pub after: Option<Cow<'a, helix::CursorRef>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub first: Option<usize>,
}
impl<'a> GetChannelFollowersRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
user_id: None,
after: None,
first: None,
}
}
pub const fn first(mut self, first: usize) -> Self {
self.first = Some(first);
self
}
pub fn user_id(self, user_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
user_id: Some(user_id.into_cow()),
..self
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Follower {
pub followed_at: types::Timestamp,
pub user_id: types::UserId,
pub user_login: types::UserName,
pub user_name: types::DisplayName,
}
impl Request for GetChannelFollowersRequest<'_> {
type Response = Vec<Follower>;
const PATH: &'static str = "channels/followers";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ModeratorReadFollowers];
}
impl RequestGet for GetChannelFollowersRequest<'_> {}
impl helix::Paginated for GetChannelFollowersRequest<'_> {
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 = GetChannelFollowersRequest::broadcaster_id("123456");
let data = br#"
{
"total": 8,
"data": [
{
"user_id": "w",
"user_name": "UserDisplayName",
"user_login": "userloginname",
"followed_at": "2022-05-24T22:22:08Z"
},
{
"user_id": "11111",
"user_name": "UserDisplayName",
"user_login": "userloginname",
"followed_at": "2022-05-24T22:22:08Z"
}
],
"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/followers?broadcaster_id=123456"
);
dbg!(GetChannelFollowersRequest::parse_response(Some(req), &uri, http_response).unwrap());
}