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 GetAuthorizationByUserRequest<'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 user_id: types::Collection<'a, types::UserId>,
}
impl<'a> GetAuthorizationByUserRequest<'a> {
pub fn new(ids: impl Into<types::Collection<'a, types::UserId>>) -> Self {
Self {
user_id: ids.into(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct AuthorizedUser {
pub user_id: types::UserId,
pub user_name: types::DisplayName,
pub user_login: types::UserName,
#[cfg(feature = "twitch_oauth2")]
pub scopes: Vec<twitch_oauth2::Scope>,
#[cfg(not(feature = "twitch_oauth2"))]
pub scopes: Vec<String>,
}
impl Request for GetAuthorizationByUserRequest<'_> {
type PaginationData = ();
type Response = Vec<AuthorizedUser>;
const PATH: &'static str = "authorization/users";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
}
impl RequestGet for GetAuthorizationByUserRequest<'_> {}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetAuthorizationByUserRequest::new(&["141981764", "197886470"]);
let data = br#"
{
"data": [
{
"user_id": "141981764",
"user_name": "TwitchDev",
"user_login": "twitchdev",
"scopes": [
"bits:read",
"channel:bot",
"channel:manage:predictions"
]
},
{
"user_id": "197886470",
"user_name": "TwitchRivals",
"user_login": "twitchrivals",
"scopes": [
"channel:manage:predictions"
]
}
]
}
"#
.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/authorization/users?user_id=141981764&user_id=197886470"
);
let res = GetAuthorizationByUserRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.len(), 2);
let dev = &res[0];
let rivals = &res[1];
assert_eq!(dev.user_login.as_str(), "twitchdev");
assert_eq!(dev.scopes.len(), 3);
assert_eq!(rivals.user_login.as_str(), "twitchrivals");
assert_eq!(rivals.scopes.len(), 1);
#[cfg(feature = "twitch_oauth2")]
{
assert_eq!(dev.scopes[0], twitch_oauth2::Scope::BitsRead);
assert_eq!(dev.scopes[1], twitch_oauth2::Scope::ChannelBot);
assert_eq!(
dev.scopes[2],
twitch_oauth2::Scope::ChannelManagePredictions
);
assert_eq!(
rivals.scopes[0],
twitch_oauth2::Scope::ChannelManagePredictions
);
}
#[cfg(not(feature = "twitch_oauth2"))]
{
assert_eq!(dev.scopes[0], "bits:read");
assert_eq!(dev.scopes[1], "channel:bot");
assert_eq!(dev.scopes[2], "channel:manage:predictions");
assert_eq!(rivals.scopes[0], "channel:manage:predictions");
}
assert_eq!(
res,
serde_json::from_str::<Vec<AuthorizedUser>>(&serde_json::to_string(&res).unwrap()).unwrap()
);
}