use super::*;
use helix::RequestPost;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct BanUserRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(setter(into))]
pub moderator_id: types::UserId,
}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct BanUserBody {
pub duration: Option<u32>,
pub reason: String,
pub user_id: types::UserId,
}
impl BanUserBody {
pub fn new(
user_id: impl Into<types::UserId>,
reason: String,
duration: impl Into<Option<u32>>,
) -> Self {
Self {
duration: duration.into(),
reason,
user_id: user_id.into(),
}
}
}
impl helix::HelixRequestBody for BanUserBody {
fn try_to_body(&self) -> Result<hyper::body::Bytes, helix::BodyError> {
#[derive(Serialize)]
struct InnerBody<'a> {
data: &'a Vec<&'a BanUserBody>,
}
let v = vec![self];
serde_json::to_vec(&InnerBody { data: &v })
.map_err(Into::into)
.map(Into::into)
}
}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct BanUser {
pub broadcaster_id: types::UserId,
pub end_time: Option<types::Timestamp>,
pub moderator_id: types::UserId,
pub user_id: types::UserId,
}
impl Request for BanUserRequest {
type Response = BanUser;
const PATH: &'static str = "moderation/bans";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ModeratorManageBannedUsers];
}
impl RequestPost for BanUserRequest {
type Body = BanUserBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPostError>
where
Self: Sized,
{
#[derive(PartialEq, Deserialize, Debug, Clone)]
struct InnerResponse {
data: Vec<BanUser>,
}
let InnerResponse { data } = helix::parse_json(response, true).map_err(|e| {
helix::HelixRequestPostError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
Ok(helix::Response {
data: data.into_iter().next().ok_or_else(|| {
helix::HelixRequestPostError::InvalidResponse {
reason: "missing response data",
response: response.to_string(),
status,
uri: uri.clone(),
}
})?,
pagination: None,
request,
total: None,
other: None,
})
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = BanUserRequest::builder()
.broadcaster_id("1234")
.moderator_id("5678")
.build();
let body = BanUserBody::new("9876", "no reason".to_string(), 300);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": [
{
"broadcaster_id": "1234",
"moderator_id": "5678",
"user_id": "9876",
"end_time": "2021-09-28T19:22:31Z"
}
]
}
"#
.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/bans?broadcaster_id=1234&moderator_id=5678"
);
dbg!(BanUserRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_request_error() {
use helix::*;
let req = BanUserRequest::builder()
.broadcaster_id("1234")
.moderator_id("5678")
.build();
let body = BanUserBody::new("9876", "no reason".to_string(), 300);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"error": "Bad Request",
"status": 400,
"message": "user is already banned"
}
"#
.to_vec();
let http_response = http::Response::builder().status(400).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/moderation/bans?broadcaster_id=1234&moderator_id=5678"
);
dbg!(BanUserRequest::parse_response(Some(req), &uri, http_response).unwrap_err());
}