use super::*;
use helix::RequestGet;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct GetBitsLeaderboardRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
pub count: Option<i32>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub period: Option<Cow<'a, str>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub started_at: Option<Cow<'a, types::TimestampRef>>,
#[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub user_id: Option<Cow<'a, types::UserIdRef>>,
}
impl<'a> GetBitsLeaderboardRequest<'a> {
pub fn count(self, count: i32) -> Self {
Self {
count: Some(count),
..self
}
}
pub fn period(self, period: impl Into<Cow<'a, str>>) -> Self {
Self {
period: Some(period.into()),
..self
}
}
pub fn started_at(self, started_at: impl types::IntoCow<'a, types::TimestampRef> + 'a) -> Self {
Self {
started_at: Some(started_at.into_cow()),
..self
}
}
pub fn user_id(self, user_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
user_id: Some(user_id.into_cow()),
..self
}
}
pub fn new() -> Self { Self::default() }
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct BitsLeaderboard {
pub leaderboard: Vec<LeaderboardUser>,
pub date_range: DateRange,
pub total: i64,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct DateRange {
pub started_at: types::Timestamp,
pub ended_at: types::Timestamp,
}
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct LeaderboardUser {
pub rank: i64,
pub score: i64,
pub user_id: types::UserId,
pub user_name: types::DisplayName,
pub user_login: types::UserName,
}
impl Request for GetBitsLeaderboardRequest<'_> {
type Response = BitsLeaderboard;
const PATH: &'static str = "bits/leaderboard";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::BitsRead];
}
impl RequestGet for GetBitsLeaderboardRequest<'_> {
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestGetError>
where
Self: Sized,
{
#[derive(PartialEq, Deserialize, Debug, Clone)]
struct InnerResponse {
data: Vec<LeaderboardUser>,
date_range: DateRange,
total: i64,
}
let response: InnerResponse = helix::parse_json(response, true).map_err(|e| {
helix::HelixRequestGetError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
Ok(helix::Response::new(
BitsLeaderboard {
leaderboard: response.data,
date_range: response.date_range,
total: response.total,
},
None,
request,
Some(response.total),
None,
))
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetBitsLeaderboardRequest::new();
let data = br##"
{
"data": [
{
"user_id": "158010205",
"user_login": "tundracowboy",
"user_name": "TundraCowboy",
"rank": 1,
"score": 12543
},
{
"user_id": "7168163",
"user_login": "topramens",
"user_name": "Topramens",
"rank": 2,
"score": 6900
}
],
"date_range": {
"started_at": "2018-02-05T08:00:00Z",
"ended_at": "2018-02-12T08:00:00Z"
},
"total": 2
}
"##
.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/bits/leaderboard?"
);
dbg!(GetBitsLeaderboardRequest::parse_response(Some(req), &uri, http_response).unwrap());
}