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 GetCharityCampaignRequest<'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> GetCharityCampaignRequest<'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 CharityCampaign {
pub id: types::CharityCampaignId,
pub broadcaster_id: types::UserId,
pub broadcaster_login: types::UserName,
pub broadcaster_name: types::DisplayName,
pub charity_name: String,
pub charity_description: String,
pub charity_logo: String,
pub charity_website: String,
pub current_amount: crate::extra::DonationAmount,
pub target_amount: Option<crate::extra::DonationAmount>,
}
impl Request for GetCharityCampaignRequest<'_> {
type Response = Option<CharityCampaign>;
const PATH: &'static str = "charity/campaigns";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::scopes::Scope::ChannelReadCharity];
}
impl RequestGet for GetCharityCampaignRequest<'_> {
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestGetError>
where
Self: Sized,
{
let response: helix::InnerResponse<Vec<_>> =
crate::parse_json(response, true).map_err(|e| {
helix::HelixRequestGetError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
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 = GetCharityCampaignRequest::broadcaster_id("123456");
let data = br##"
{
"data": [
{
"id": "123-abc-456-def",
"broadcaster_id": "123456",
"broadcaster_name": "SunnySideUp",
"broadcaster_login": "sunnysideup",
"charity_name": "Example name",
"charity_description": "Example description",
"charity_logo": "https://abc.cloudfront.net/ppgf/1000/100.png",
"charity_website": "https://www.example.com",
"current_amount": {
"value": 86000,
"decimal_places": 2,
"currency": "USD"
},
"target_amount": {
"value": 1500000,
"decimal_places": 2,
"currency": "USD"
}
}
]
}
"##
.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/charity/campaigns?broadcaster_id=123456"
);
dbg!(GetCharityCampaignRequest::parse_response(Some(req), &uri, http_response).unwrap());
}