use super::*;
use helix::RequestPut;
pub use super::ShieldModeStatus;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct UpdateShieldModeStatusRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub moderator_id: Cow<'a, types::UserIdRef>,
}
impl<'a> UpdateShieldModeStatusRequest<'a> {
pub fn new(
broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
moderator_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
moderator_id: moderator_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct UpdateShieldModeStatusBody<'a> {
pub is_active: bool,
#[serde(skip)]
_ph: std::marker::PhantomData<&'a ()>,
}
impl helix::private::SealedSerialize for UpdateShieldModeStatusBody<'_> {}
impl UpdateShieldModeStatusBody<'_> {
pub const fn is_active(is_active: bool) -> Self {
Self {
is_active,
_ph: std::marker::PhantomData,
}
}
}
impl Request for UpdateShieldModeStatusRequest<'_> {
type Response = super::ShieldModeStatus;
const PATH: &'static str = "moderation/shield_mode";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ModeratorManageShieldMode];
}
impl<'a> RequestPut for UpdateShieldModeStatusRequest<'a> {
type Body = UpdateShieldModeStatusBody<'a>;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestPutError>
where
Self: Sized,
{
let inner_response: helix::InnerResponse<Vec<_>> = crate::parse_json(response, true)
.map_err(|e| {
helix::HelixRequestPutError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
Ok(helix::Response::new(
inner_response.data.into_iter().next().ok_or(
helix::HelixRequestPutError::InvalidResponse {
reason: "expected an entry in `data`",
response: response.to_string(),
status,
uri: uri.clone(),
},
)?,
inner_response.pagination.cursor,
request,
inner_response.total,
inner_response.other,
))
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = UpdateShieldModeStatusRequest::new("12345", "98765");
let body = UpdateShieldModeStatusBody::is_active(false);
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"is_active":false}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": [
{
"is_active": false,
"moderator_id": "98765",
"moderator_name": "SimplySimple",
"moderator_login": "simplysimple",
"last_activated_at": "2022-07-26T17:16:03.123Z"
}
]
}
"#
.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/moderation/shield_mode?broadcaster_id=12345&moderator_id=98765"
);
dbg!(UpdateShieldModeStatusRequest::parse_response(Some(req), &uri, http_response).unwrap());
}