use crate::helix::{parse_json, HelixRequestPatchError};
use super::*;
use helix::RequestPatch;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct UpdateCustomRewardRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(setter(into))]
pub id: types::RewardId,
}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct UpdateCustomRewardBody {
#[builder(default, setter(into))]
pub title: Option<String>,
#[builder(default, setter(into))]
pub prompt: Option<String>,
#[builder(default, setter(into))]
pub cost: Option<usize>,
#[builder(default, setter(into))]
pub background_color: Option<String>,
#[builder(default, setter(into))]
pub is_enabled: Option<bool>,
#[builder(default, setter(into))]
pub is_user_input_required: Option<bool>,
#[builder(default, setter(into))]
pub is_max_per_stream_enabled: Option<bool>,
#[builder(default, setter(into))]
pub max_per_stream: Option<usize>,
#[builder(default, setter(into))]
pub is_max_per_user_per_stream_enabled: Option<bool>,
#[builder(default, setter(into))]
pub max_per_user_per_stream: Option<usize>,
#[builder(default, setter(into))]
pub is_global_cooldown_enabled: Option<bool>,
#[builder(default, setter(into))]
pub global_cooldown_seconds: Option<usize>,
#[builder(default, setter(into))]
pub is_paused: Option<bool>,
#[builder(default, setter(into))]
pub should_redemptions_skip_request_queue: Option<bool>,
}
impl helix::private::SealedSerialize for UpdateCustomRewardBody {}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub enum UpdateCustomReward {
Success(CustomReward),
}
impl Request for UpdateCustomRewardRequest {
type Response = UpdateCustomReward;
const PATH: &'static str = "channel_points/custom_rewards";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::Scope::ChannelManageRedemptions];
}
impl RequestPatch for UpdateCustomRewardRequest {
type Body = UpdateCustomRewardBody;
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,
{
let resp = match status {
http::StatusCode::OK => {
let resp: helix::InnerResponse<Vec<CustomReward>> = parse_json(response, true)
.map_err(|e| {
HelixRequestPatchError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
UpdateCustomReward::Success(resp.data.into_iter().next().ok_or(
helix::HelixRequestPatchError::InvalidResponse {
reason: "expected at least one element in data",
response: response.to_string(),
status,
uri: uri.clone(),
},
)?)
}
_ => {
return Err(helix::HelixRequestPatchError::InvalidResponse {
reason: "unexpected status code",
response: response.to_string(),
status,
uri: uri.clone(),
})
}
};
Ok(helix::Response {
data: resp,
pagination: None,
request,
total: None,
other: None,
})
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = UpdateCustomRewardRequest::builder()
.broadcaster_id("274637212")
.id("92af127c-7326-4483-a52b-b0da0be61c01")
.build();
let body = UpdateCustomRewardBody::builder().is_enabled(false).build();
dbg!(req.create_request(body, "token", "clientid").unwrap());
let data = br##"
{
"data": [
{
"broadcaster_name": "torpedo09",
"broadcaster_login": "torpedo09",
"broadcaster_id": "274637212",
"id": "92af127c-7326-4483-a52b-b0da0be61c01",
"image": null,
"background_color": "#00E5CB",
"is_enabled": false,
"cost": 30000,
"title": "game analysis 2v2",
"prompt": "",
"is_user_input_required": false,
"max_per_stream_setting": {
"is_enabled": true,
"max_per_stream": 60
},
"max_per_user_per_stream_setting": {
"is_enabled": false,
"max_per_user_per_stream": 0
},
"global_cooldown_setting": {
"is_enabled": false,
"global_cooldown_seconds": 0
},
"is_paused": false,
"is_in_stock": false,
"default_image": {
"url_1x": "https://static-cdn.jtvnw.net/custom-reward-images/default-1.png",
"url_2x": "https://static-cdn.jtvnw.net/custom-reward-images/default-2.png",
"url_4x": "https://static-cdn.jtvnw.net/custom-reward-images/default-4.png"
},
"should_redemptions_skip_request_queue": true,
"redemptions_redeemed_current_stream": 60,
"cooldown_expires_at": null
}
]
}
"##
.to_vec();
let http_response = http::Response::builder().status(200).body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=274637212&id=92af127c-7326-4483-a52b-b0da0be61c01"
);
dbg!(UpdateCustomRewardRequest::parse_response(Some(req), &uri, http_response).unwrap());
}