use super::*;
use helix::RequestPost;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct AddBlockedTermRequest<'a> {
#[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(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub moderator_id: Cow<'a, types::UserIdRef>,
}
impl<'a> AddBlockedTermRequest<'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 AddBlockedTermBody<'a> {
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub text: Cow<'a, str>,
}
impl<'a> AddBlockedTermBody<'a> {
pub fn new(text: impl Into<Cow<'a, str>>) -> Self { Self { text: text.into() } }
}
impl helix::private::SealedSerialize for AddBlockedTermBody<'_> {}
impl helix::HelixRequestBody for [AddBlockedTermBody<'_>] {
fn try_to_body(&self) -> Result<hyper::body::Bytes, helix::BodyError> {
#[derive(Serialize)]
struct InnerBody<'a> {
data: &'a [AddBlockedTermBody<'a>],
}
serde_json::to_vec(&InnerBody { data: self })
.map_err(Into::into)
.map(Into::into)
}
}
pub type AddBlockedTermResponse = BlockedTerm;
impl Request for AddBlockedTermRequest<'_> {
type Response = Vec<BlockedTerm>;
const PATH: &'static str = "moderation/blocked_terms";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ModeratorManageBlockedTerms];
}
impl<'a> RequestPost for AddBlockedTermRequest<'a> {
type Body = AddBlockedTermBody<'a>;
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = AddBlockedTermRequest::new("1234", "5678");
let body = AddBlockedTermBody::new("A phrase I’m not fond of");
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"text":"A phrase I’m not fond of"}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": [
{
"broadcaster_id": "713936733",
"moderator_id": "713936733",
"id": "3bb6e5d3-afb1-416c-ad4e-21af970ccfe7",
"text": "A phrase I'm not fond of",
"created_at": "2021-09-29T15:36:45Z",
"updated_at": "2021-09-29T15:36:45Z",
"expires_at": null
}
]
}
"#
.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/moderation/blocked_terms?broadcaster_id=1234&moderator_id=5678"
);
dbg!(AddBlockedTermRequest::parse_response(Some(req), &uri, http_response).unwrap());
}