use std::marker::PhantomData;
use super::*;
use helix::RequestPost;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct CreateStreamMarkerRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(default))]
#[serde(skip)]
_marker: PhantomData<&'a ()>,
}
impl CreateStreamMarkerRequest<'_> {
pub fn new() -> Self { Self::default() }
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct CreateStreamMarkerBody<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub user_id: Cow<'a, types::UserIdRef>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub description: Option<Cow<'a, str>>,
}
impl<'a> CreateStreamMarkerBody<'a> {
pub fn new(
user_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
description: impl Into<Cow<'a, str>>,
) -> Self {
Self {
user_id: user_id.into_cow(),
description: Some(description.into()),
}
}
pub fn user_id(user_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
user_id: user_id.into_cow(),
description: None,
}
}
}
impl helix::private::SealedSerialize for CreateStreamMarkerBody<'_> {}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct CreatedStreamMarker {
pub id: types::StreamMarkerId,
pub created_at: types::Timestamp,
pub position_seconds: u64,
pub description: String,
}
impl Request for CreateStreamMarkerRequest<'_> {
type Response = CreatedStreamMarker;
const PATH: &'static str = "streams/markers";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelManageBroadcast];
}
impl<'a> RequestPost for CreateStreamMarkerRequest<'a> {
type Body = CreateStreamMarkerBody<'a>;
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,
{
helix::parse_single_return(request, uri, response, status)
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = CreateStreamMarkerRequest::new();
let body = CreateStreamMarkerBody::new("123", "hello, this is a marker!");
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"user_id":"123","description":"hello, this is a marker!"}"#
);
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br#"
{
"data": [
{
"id": "123",
"created_at": "2018-08-20T20:10:03Z",
"description": "hello, this is a marker!",
"position_seconds": 244
}
]
}
"#
.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/streams/markers?"
);
let res = CreateStreamMarkerRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.id.as_str(), "123");
assert_eq!(res.created_at.as_str(), "2018-08-20T20:10:03Z");
assert_eq!(res.description, "hello, this is a marker!");
assert_eq!(res.position_seconds, 244);
}