use std::marker::PhantomData;
use super::*;
use helix::RequestPost;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct ManageHeldAutoModMessagesRequest<'a> {
#[serde(skip)]
_marker: PhantomData<&'a ()>,
}
impl ManageHeldAutoModMessagesRequest<'_> {
pub fn new() -> Self { Self::default() }
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct ManageHeldAutoModMessagesBody<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub user_id: Cow<'a, types::UserIdRef>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub msg_id: Cow<'a, types::MsgIdRef>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
pub action: AutoModAction,
}
impl<'a> ManageHeldAutoModMessagesBody<'a> {
pub fn new(
user_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
msg_id: impl types::IntoCow<'a, types::MsgIdRef> + 'a,
action: impl Into<AutoModAction>,
) -> Self {
Self {
user_id: user_id.into_cow(),
msg_id: msg_id.into_cow(),
action: action.into(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Copy, Clone, Debug)]
#[serde(rename_all = "UPPERCASE")]
#[non_exhaustive]
pub enum AutoModAction {
Allow,
Deny,
}
impl From<bool> for AutoModAction {
fn from(b: bool) -> Self {
match b {
true => Self::Allow,
false => Self::Deny,
}
}
}
impl helix::private::SealedSerialize for ManageHeldAutoModMessagesBody<'_> {}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub enum ManageHeldAutoModMessages {
Success,
}
impl Request for ManageHeldAutoModMessagesRequest<'_> {
type Response = ManageHeldAutoModMessages;
const PATH: &'static str = "moderation/automod/message";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ModeratorManageAutoMod];
}
impl<'a> RequestPost for ManageHeldAutoModMessagesRequest<'a> {
type Body = ManageHeldAutoModMessagesBody<'a>;
fn parse_inner_response<'d>(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPostError>
where
Self: Sized,
{
match status {
http::StatusCode::NO_CONTENT => Ok(helix::Response::with_data(
ManageHeldAutoModMessages::Success,
request,
)),
_ => Err(helix::HelixRequestPostError::InvalidResponse {
reason: "unexpected status",
response: response.to_string(),
status,
uri: uri.clone(),
}),
}
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = ManageHeldAutoModMessagesRequest::new();
let body = ManageHeldAutoModMessagesBody::new("9327994", "836013710", true);
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"user_id":"9327994","msg_id":"836013710","action":"ALLOW"}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#""#.to_vec();
let http_response = http::Response::builder().status(204).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/moderation/automod/message?"
);
dbg!(ManageHeldAutoModMessagesRequest::parse_response(Some(req), &uri, http_response).unwrap());
}