use super::*;
use helix::RequestPatch;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct ModifyChannelInformationRequest<'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> ModifyChannelInformationRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
ModifyChannelInformationRequest {
broadcaster_id: broadcaster_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct ModifyChannelInformationBody<'a> {
#[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 game_id: Option<Cow<'a, types::CategoryIdRef>>,
#[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 title: Option<Cow<'a, str>>,
#[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 broadcaster_language: Option<Cow<'a, str>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[serde(skip_serializing_if = "Option::is_none")]
pub delay: Option<i32>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
#[cfg_attr(not(feature = "deser_borrow"), serde(bound(deserialize = "'de: 'a")))]
pub tags: Option<Cow<'a, [&'a str]>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub content_classification_labels: Option<Cow<'a, [ContentClassificationLabel]>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_branded_content: Option<bool>,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct ContentClassificationLabel {
pub is_enabled: bool,
pub id: types::ContentClassificationId,
}
impl ContentClassificationLabel {
pub const fn new(is_enabled: bool, id: types::ContentClassificationId) -> Self {
Self { is_enabled, id }
}
}
impl From<(bool, types::ContentClassificationId)> for ContentClassificationLabel {
fn from(tup: (bool, types::ContentClassificationId)) -> Self {
Self {
is_enabled: tup.0,
id: tup.1,
}
}
}
impl<'a> ModifyChannelInformationBody<'a> {
pub fn new() -> Self { Default::default() }
pub fn game_id(
&mut self,
game_id: impl types::IntoCow<'a, types::CategoryIdRef> + 'a,
) -> &mut Self {
self.game_id = Some(game_id.into_cow());
self
}
pub fn broadcaster_language(
&mut self,
broadcaster_language: impl Into<Cow<'a, str>>,
) -> &mut Self {
self.broadcaster_language = Some(broadcaster_language.into());
self
}
pub fn title(&mut self, title: impl Into<Cow<'a, str>>) -> &mut Self {
self.title = Some(title.into());
self
}
pub fn delay(&mut self, delay: i32) -> &mut Self {
self.delay = Some(delay);
self
}
pub fn tags(&mut self, tags: &'a [&str]) -> &mut Self {
self.tags = Some(tags.into());
self
}
pub fn content_classification_labels(
&mut self,
content_classification_labels: impl Into<Cow<'a, [ContentClassificationLabel]>>,
) -> &mut Self {
self.content_classification_labels = Some(content_classification_labels.into());
self
}
pub fn is_branded_content(&mut self, is_branded_content: bool) -> &mut Self {
self.is_branded_content = Some(is_branded_content);
self
}
}
#[test]
fn t() {
let mut body = ModifyChannelInformationBody::new();
body.content_classification_labels(vec![
(true, types::ContentClassificationId::SexualThemes).into()
]);
dbg!(body);
}
impl helix::private::SealedSerialize for ModifyChannelInformationBody<'_> {}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[non_exhaustive]
pub enum ModifyChannelInformation {
Success,
}
impl Request for ModifyChannelInformationRequest<'_> {
type Response = ModifyChannelInformation;
const PATH: &'static str = "channels";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelManageBroadcast];
}
impl<'a> RequestPatch for ModifyChannelInformationRequest<'a> {
type Body = ModifyChannelInformationBody<'a>;
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPatchError>
where
Self: Sized,
{
match status {
http::StatusCode::NO_CONTENT | http::StatusCode::OK => Ok(helix::Response::with_data(
ModifyChannelInformation::Success,
request,
)),
_ => Err(helix::HelixRequestPatchError::InvalidResponse {
reason: "unexpected status code",
response: response.to_string(),
status,
uri: uri.clone(),
}),
}
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = ModifyChannelInformationRequest::broadcaster_id("41245072");
let mut body = ModifyChannelInformationBody::new();
body.game_id("33214");
body.title("there are helicopters in the game? REASON TO PLAY FORTNITE found");
body.broadcaster_language("en");
body.tags(&["LevelingUp"]);
assert_eq!(
std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
r#"{"game_id":"33214","title":"there are helicopters in the game? REASON TO PLAY FORTNITE found","broadcaster_language":"en","tags":["LevelingUp"]}"#
);
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/channels?broadcaster_id=41245072"
);
dbg!(ModifyChannelInformationRequest::parse_response(Some(req), &uri, http_response).unwrap());
}