use super::*;
use helix::RequestPut;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct UpdateUserRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub description: Option<Cow<'a, str>>,
}
impl<'a> UpdateUserRequest<'a> {
pub const fn empty() -> Self { Self { description: None } }
pub fn description(description: impl Into<Cow<'a, str>>) -> Self {
Self {
description: Some(description.into()),
}
}
}
impl Request for UpdateUserRequest<'_> {
type Response = User;
const PATH: &'static str = "users";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::UserEdit];
}
impl RequestPut for UpdateUserRequest<'_> {
type Body = helix::EmptyBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPutError>
where
Self: Sized,
{
helix::parse_single_return(request, uri, response, status)
}
}
#[cfg(test)]
#[test]
fn test_request() {
let req = UpdateUserRequest::description("my description");
let body = helix::EmptyBody;
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data":[{
"id": "44322889",
"login": "dallas",
"display_name": "dallas",
"type": "staff",
"broadcaster_type": "affiliate",
"description": "my description",
"profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/4d1f36cbf1f0072d-profile_image-300x300.png",
"offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-channel_offline_image-2e82c1df2a464df7-1920x1080.jpeg",
"view_count": 6995,
"email": "not-real@email.com",
"created_at": "2013-06-03T19:12:02.580593Z"
}]
}
"#.to_vec();
let http_response = http::Response::builder().status(200).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/users?description=my+description"
);
let res = UpdateUserRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.id.as_str(), "44322889");
assert_eq!(res.type_, Some(types::UserType::Staff));
assert_eq!(
res.broadcaster_type,
Some(types::BroadcasterType::Affiliate)
);
assert_eq!(res.login.as_str(), "dallas");
assert_eq!(res.description.unwrap(), "my description");
}
#[cfg(test)]
#[test]
fn test_request_empty() {
let req = UpdateUserRequest::empty();
let body = helix::EmptyBody;
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": [
{
"broadcaster_type": "",
"created_at": "2016-07-14T16:13:40Z",
"description": "hi",
"display_name": "nerixyz",
"id": "129546453",
"login": "nerixyz",
"offline_image_url": "",
"profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/e065218b-49df-459d-afd3-c6557870f551-profile_image-300x300.png",
"type": "",
"view_count": 0
}
]
}
"#.to_vec();
let http_response = http::Response::builder().status(200).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(uri.to_string(), "https://api.twitch.tv/helix/users?");
let res = UpdateUserRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.id.as_str(), "129546453");
assert_eq!(res.type_, Some(types::UserType::None));
assert_eq!(res.broadcaster_type, Some(types::BroadcasterType::None));
}