use super::*;
use helix::RequestGet;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct GetChatSettingsRequest<'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(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub moderator_id: Option<Cow<'a, types::UserIdRef>>,
}
impl<'a> GetChatSettingsRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
moderator_id: None,
}
}
pub fn moderator_id(
mut self,
moderator_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
) -> Self {
self.moderator_id = Some(moderator_id.into_cow());
self
}
}
impl Request for GetChatSettingsRequest<'_> {
type Response = ChatSettings;
#[cfg(feature = "twitch_oauth2")]
const OPT_SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ModeratorReadChatSettings];
const PATH: &'static str = "chat/settings";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
}
impl RequestGet for GetChatSettingsRequest<'_> {
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestGetError>
where
Self: Sized,
{
let resp = match status {
http::StatusCode::OK => {
let resp: helix::InnerResponse<[ChatSettings; 1]> =
helix::parse_json(response, true).map_err(|e| {
helix::HelixRequestGetError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
let [s] = resp.data;
s
}
_ => {
return Err(helix::HelixRequestGetError::InvalidResponse {
reason: "unexpected status code",
response: response.to_string(),
status,
uri: uri.clone(),
})
}
};
Ok(helix::Response::with_data(resp, request))
}
}
#[cfg(test)]
#[test]
fn test_request_as_mod() {
use helix::*;
let req = GetChatSettingsRequest::broadcaster_id("1234").moderator_id("713936733");
let data = br#"
{
"data": [
{
"broadcaster_id": "713936733",
"slow_mode": false,
"slow_mode_wait_time": null,
"follower_mode": true,
"follower_mode_duration": 0,
"subscriber_mode": false,
"emote_mode": false,
"unique_chat_mode": false,
"non_moderator_chat_delay": true,
"non_moderator_chat_delay_duration": 4
}
]
}
"#
.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/chat/settings?broadcaster_id=1234&moderator_id=713936733"
);
dbg!(GetChatSettingsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_request_as_user() {
use helix::*;
let req = GetChatSettingsRequest::broadcaster_id("11148817");
let data = br#"
{
"data": [
{
"broadcaster_id": "11148817",
"emote_mode": false,
"follower_mode": false,
"follower_mode_duration": null,
"slow_mode": false,
"slow_mode_wait_time": null,
"subscriber_mode": false,
"unique_chat_mode": false
}
]
}
"#
.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/chat/settings?broadcaster_id=11148817"
);
dbg!(GetChatSettingsRequest::parse_response(Some(req), &uri, http_response).unwrap());
}