use crate::helix::{parse_json, HelixRequestPatchError};
pub use super::CustomRewardRedemption;
use super::*;
use helix::RequestPatch;
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct UpdateRedemptionStatusRequest {
#[builder(setter(into))]
pub broadcaster_id: types::UserId,
#[builder(setter(into))]
pub reward_id: types::RewardId,
#[builder(setter(into))]
pub id: types::RedemptionId,
}
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct UpdateRedemptionStatusBody {
#[builder(setter(into))]
pub status: CustomRewardRedemptionStatus,
}
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
#[non_exhaustive]
pub enum UpdateRedemptionStatusInformation {
Success(CustomRewardRedemption),
}
impl Request for UpdateRedemptionStatusRequest {
type Response = UpdateRedemptionStatusInformation;
const PATH: &'static str = "channel_points/custom_rewards/redemptions";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: &'static [twitch_oauth2::Scope] =
&[twitch_oauth2::scopes::Scope::ChannelManageBroadcast];
}
impl RequestPatch for UpdateRedemptionStatusRequest {
type Body = UpdateRedemptionStatusBody;
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<CustomRewardRedemption>> =
parse_json(response, true).map_err(|e| {
HelixRequestPatchError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
UpdateRedemptionStatusInformation::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,
})
}
}
impl helix::private::SealedSerialize for UpdateRedemptionStatusBody {}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = UpdateRedemptionStatusRequest::builder()
.broadcaster_id("274637212".to_string())
.reward_id("92af127c-7326-4483-a52b-b0da0be61c01".to_string())
.id("17fa2df1-ad76-4804-bfa5-a40ef63efe63".to_string())
.build();
let body = UpdateRedemptionStatusBody::builder()
.status(CustomRewardRedemptionStatus::Unfulfilled)
.build();
dbg!(req.create_request(body, "abcd", "client").unwrap());
let data = br##"
{
"data": [
{
"broadcaster_name": "torpedo09",
"broadcaster_login": "torpedo09",
"broadcaster_id": "274637212",
"id": "17fa2df1-ad76-4804-bfa5-a40ef63efe63",
"user_id": "274637212",
"user_name": "torpedo09",
"user_login": "torpedo09",
"user_input": "",
"status": "CANCELED",
"redeemed_at": "2020-07-01T18:37:32Z",
"reward": {
"id": "92af127c-7326-4483-a52b-b0da0be61c01",
"title": "game analysis",
"prompt": "",
"cost": 50000
}
}
]
}
"##
.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/channel_points/custom_rewards/redemptions?broadcaster_id=274637212&reward_id=92af127c-7326-4483-a52b-b0da0be61c01&id=17fa2df1-ad76-4804-bfa5-a40ef63efe63"
);
dbg!(UpdateRedemptionStatusRequest::parse_response(Some(req), &uri, http_response).unwrap());
}