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 GetSharedChatSessionRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
}
impl<'a> GetSharedChatSessionRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct SharedChatSession {
pub session_id: types::SharedChatSessionId,
pub host_broadcaster_id: types::UserId,
pub participants: Vec<SharedChatParticipant>,
pub created_at: types::Timestamp,
pub updated_at: types::Timestamp,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct SharedChatParticipant {
pub broadcaster_id: types::UserId,
}
impl Request for GetSharedChatSessionRequest<'_> {
type Response = Option<SharedChatSession>;
const PATH: &'static str = "shared_chat/session";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
}
impl RequestGet for GetSharedChatSessionRequest<'_> {
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<helix::request::ZeroOrOne<SharedChatSession>> =
helix::parse_json(response, true).map_err(|e| {
helix::HelixRequestGetError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
resp.data.0
}
_ => {
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() {
use helix::*;
let req = GetSharedChatSessionRequest::broadcaster_id("198704263");
let data = br#"
{
"data": [
{
"session_id": "359bce59-fa4e-41a5-bd6f-9bc0c8360485",
"host_broadcaster_id": "198704263",
"participants": [{
"broadcaster_id": "198704263"
}, {
"broadcaster_id": "487263401"
}],
"created_at": "2024-09-29T19:45:37Z",
"updated_at": "2024-09-29T19:50:01Z"
}
]
}
"#
.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/shared_chat/session?broadcaster_id=198704263"
);
let res = GetSharedChatSessionRequest::parse_response(Some(req), &uri, http_response).unwrap();
let res = res.data.unwrap();
assert_eq!(
res.session_id.as_str(),
"359bce59-fa4e-41a5-bd6f-9bc0c8360485"
);
assert_eq!(res.host_broadcaster_id.as_str(), "198704263");
assert_eq!(res.participants.len(), 2);
assert_eq!(res.participants[0].broadcaster_id.as_str(), "198704263");
assert_eq!(res.participants[1].broadcaster_id.as_str(), "487263401");
assert_eq!(res.created_at.as_str(), "2024-09-29T19:45:37Z");
assert_eq!(res.updated_at.as_str(), "2024-09-29T19:50:01Z");
}
#[cfg(test)]
#[test]
fn test_request_empty() {
use helix::*;
let req = GetSharedChatSessionRequest::broadcaster_id("198704263");
let data = br#"{ "data": [] }"#.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/shared_chat/session?broadcaster_id=198704263"
);
let res = GetSharedChatSessionRequest::parse_response(Some(req), &uri, http_response).unwrap();
assert!(res.data.is_none());
}