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 SnoozeNextAdRequest<'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> SnoozeNextAdRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
SnoozeNextAdRequest {
broadcaster_id: broadcaster_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[non_exhaustive]
pub struct SnoozedAdSchedule {
pub snooze_count: i32,
pub snooze_refresh_at: types::Timestamp,
pub next_ad_at: types::Timestamp,
}
impl Request for SnoozeNextAdRequest<'_> {
type Response = SnoozedAdSchedule;
const PATH: &'static str = "channels/ads/schedule/snooze";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelManageAds];
}
impl RequestPost for SnoozeNextAdRequest<'_> {
type Body = helix::EmptyBody;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestPostError>
where
Self: Sized,
{
let resp: helix::InnerResponse<Vec<_>> =
crate::parse_json(response, true).map_err(|e| {
helix::HelixRequestPostError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
Ok(helix::Response::new(
resp.data
.into_iter()
.next()
.ok_or(helix::HelixRequestPostError::InvalidResponse {
reason: "expected an entry in `data`",
response: response.to_string(),
status,
uri: uri.clone(),
})?,
resp.pagination.cursor,
request,
resp.total,
resp.other,
))
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = SnoozeNextAdRequest::broadcaster_id("123");
let data = r#"
{
"data": [
{
"snooze_count" : 1,
"snooze_refresh_at" : "2023-08-01T23:08:18+00:00",
"next_ad_at" : "2023-08-01T23:08:18+00:00"
}
]
}
"#;
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/channels/ads/schedule/snooze?broadcaster_id=123"
);
dbg!(SnoozeNextAdRequest::parse_response(Some(req), &uri, http_response).unwrap());
}