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 SendWhisperRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub from_user_id: Cow<'a, types::UserIdRef>,
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub to_user_id: Cow<'a, types::UserIdRef>,
}
impl<'a> SendWhisperRequest<'a> {
pub fn new(
from: impl types::IntoCow<'a, types::UserIdRef> + 'a,
to: impl types::IntoCow<'a, types::UserIdRef> + 'a,
) -> Self {
Self {
from_user_id: from.into_cow(),
to_user_id: to.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct SendWhisperBody<'a> {
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub message: Cow<'a, str>,
}
impl<'a> From<&'a str> for SendWhisperBody<'a> {
fn from(string: &'a str) -> Self { Self::new(string) }
}
impl<'a> SendWhisperBody<'a> {
pub fn new(message: impl Into<Cow<'a, str>>) -> Self {
Self {
message: message.into(),
}
}
}
impl helix::private::SealedSerialize for SendWhisperBody<'_> {}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub enum SendWhisperResponse {
Success,
}
impl Request for SendWhisperRequest<'_> {
type Response = SendWhisperResponse;
const PATH: &'static str = "whispers";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::UserManageWhispers];
}
impl<'a> RequestPost for SendWhisperRequest<'a> {
type Body = SendWhisperBody<'a>;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response_str: &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(
SendWhisperResponse::Success,
request,
)),
_ => Err(helix::HelixRequestPostError::InvalidResponse {
reason: "unexpected status",
response: response_str.to_string(),
status,
uri: uri.clone(),
}),
}
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = SendWhisperRequest::new("123", "456");
let body = SendWhisperBody::new("hello");
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"message":"hello"}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = 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/whispers?from_user_id=123&to_user_id=456"
);
dbg!(SendWhisperRequest::parse_response(Some(req), &uri, http_response).unwrap());
}