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 GetAdScheduleRequest<'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> GetAdScheduleRequest<'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 AdSchedule {
pub snooze_count: i32,
pub snooze_refresh_at: types::Timestamp,
#[serde(
default,
deserialize_with = "helix::deserialize_none_from_empty_or_zero_string"
)]
pub next_ad_at: Option<types::Timestamp>,
pub duration: i32,
pub last_ad_at: types::Timestamp,
#[serde(
default,
deserialize_with = "helix::deserialize_none_from_empty_or_zero_string"
)]
pub preroll_free_time: Option<i32>,
}
impl Request for GetAdScheduleRequest<'_> {
type Response = Option<AdSchedule>;
const PATH: &'static str = "channels/ads";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
twitch_oauth2::Scope::ChannelReadAds,
twitch_oauth2::Scope::ChannelManageAds
)];
}
impl RequestGet for GetAdScheduleRequest<'_> {
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
str_response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestGetError>
where
Self: Sized,
{
let response: Result<helix::InnerResponse<Vec<_>>, _> =
crate::parse_json(str_response, true).map_err(|e| {
helix::HelixRequestGetError::DeserializeError(
str_response.to_string(),
e,
uri.clone(),
status,
)
});
let response = match response {
Ok(resp) => resp,
Err(error) => {
#[derive(PartialEq, Eq, Deserialize, Default)]
struct IsWrong {
snooze_count: i32,
snooze_refresh_at: i32,
next_ad_at: i32,
duration: i32,
last_ad_at: i32,
preroll_free_time: i32,
}
let fake: Result<helix::InnerResponse<Vec<IsWrong>>, _> =
crate::parse_json(str_response, true);
let empty = match fake {
Ok(fake) if fake.data.first() == Some(&IsWrong::default()) => fake,
_ => {
return Err(error);
}
};
helix::InnerResponse {
data: vec![],
pagination: empty.pagination,
total: empty.total,
other: empty.other,
}
}
};
Ok(helix::Response::new(
response.data.into_iter().next(),
response.pagination.cursor,
request,
response.total,
response.other,
))
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetAdScheduleRequest::broadcaster_id("123");
let data = br#"
{
"data": [
{
"next_ad_at" : "2023-08-01T23:08:18+00:00",
"last_ad_at" : "2023-08-01T23:08:18+00:00",
"duration" : 60,
"preroll_free_time" : 90,
"snooze_count" : 1,
"snooze_refresh_at" : "2023-08-01T23:08:18+00:00"
}
]
}
"#
.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/channels/ads?broadcaster_id=123"
);
dbg!(GetAdScheduleRequest::parse_response(Some(req), &uri, http_response).unwrap());
}
#[cfg(test)]
#[test]
fn test_request_empty_wrong() {
use helix::*;
let req = GetAdScheduleRequest::broadcaster_id("123");
let data = br#"{
"data": [
{
"duration": 0,
"last_ad_at": 0,
"next_ad_at": 0,
"preroll_free_time": 0,
"snooze_count": 0,
"snooze_refresh_at": 0
}
]
}"#
.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/channels/ads?broadcaster_id=123"
);
assert_eq!(
GetAdScheduleRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data,
None
);
}