Skip to content

Commit 95a6eb3

Browse files
committed
add total and gifter information to Get Broadcaster Subscriptions
1 parent 6f487eb commit 95a6eb3

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Deprecated `Vip` action in `ChatModeratorActionsReply`, replacing it with `VipAdded`
2626
* Removed some derived impls and fixed builders that assumed a default wrongly.
2727
* `HelixClient::new`, `TmiClient::new` and `TwitchClient::new` now give a more specified client.
28+
* Added total and gifter information to helix `Get Broadcaster Subscriptions`
2829

2930
### Removed
3031

src/helix/subscriptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! .build();
1717
//!
1818
//!
19-
//! println!("{:?}", &client.req_get(req, &token).await?.data.get(0));
19+
//! println!("{:?}", &client.req_get(req, &token).await?.data);
2020
//! # Ok(())
2121
//! # }
2222
//! ```

src/helix/subscriptions/get_broadcaster_subscriptions.rs

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! .build();
1515
//! ```
1616
//!
17-
//! ## Response: [BroadcasterSubscription]
17+
//! ## Response: [BroadcasterSubscriptions]
1818
//!
1919
//! Send the request to receive the response with [`HelixClient::req_get()`](helix::HelixClient::req_get).
2020
//!
@@ -29,7 +29,7 @@
2929
//! let request = get_broadcaster_subscriptions::GetBroadcasterSubscriptionsRequest::builder()
3030
//! .broadcaster_id("1234")
3131
//! .build();
32-
//! let response: Vec<get_broadcaster_subscriptions::BroadcasterSubscription> = client.req_get(request, &token).await?.data;
32+
//! let response: get_broadcaster_subscriptions::BroadcasterSubscriptions = client.req_get(request, &token).await?.data;
3333
//! # Ok(())
3434
//! # }
3535
//! ```
@@ -65,13 +65,42 @@ pub struct GetBroadcasterSubscriptionsRequest {
6565
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
6666
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
6767
#[non_exhaustive]
68+
pub struct BroadcasterSubscriptions {
69+
/// List of users subscribed to the broadcaster and the details of the subscription.
70+
pub subscriptions: Vec<BroadcasterSubscription>,
71+
/// The number of Twitch users subscribed to the broadcaster.
72+
pub total: i64,
73+
}
74+
75+
/// A subscription in a channel
76+
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
77+
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
78+
#[non_exhaustive]
6879
pub struct BroadcasterSubscription {
6980
/// User ID of the broadcaster.
7081
pub broadcaster_id: types::UserId,
7182
/// Login of the broadcaster.
7283
pub broadcaster_login: types::UserName,
7384
/// Display name of the broadcaster.
7485
pub broadcaster_name: types::DisplayName,
86+
/// User ID of the broadcaster.
87+
#[serde(
88+
default,
89+
deserialize_with = "helix::deserialize_none_from_empty_string"
90+
)]
91+
pub gifter_id: Option<types::UserId>,
92+
/// Login of the gifter.
93+
#[serde(
94+
default,
95+
deserialize_with = "helix::deserialize_none_from_empty_string"
96+
)]
97+
pub gifter_login: Option<types::UserName>,
98+
/// Display name of the gifter.
99+
#[serde(
100+
default,
101+
deserialize_with = "helix::deserialize_none_from_empty_string"
102+
)]
103+
pub gifter_name: Option<types::DisplayName>,
75104
/// Determines if the subscription is a gift subscription.
76105
pub is_gift: bool,
77106
/// Type of subscription (Tier 1, Tier 2, Tier 3). 1000 = Tier 1, 2000 = Tier 2, 3000 = Tier 3 subscriptions.
@@ -87,15 +116,49 @@ pub struct BroadcasterSubscription {
87116
}
88117

89118
impl Request for GetBroadcasterSubscriptionsRequest {
90-
type Response = Vec<BroadcasterSubscription>;
119+
type Response = BroadcasterSubscriptions;
91120

92121
const PATH: &'static str = "subscriptions";
93122
#[cfg(feature = "twitch_oauth2")]
94123
const SCOPE: &'static [twitch_oauth2::Scope] =
95124
&[twitch_oauth2::Scope::ChannelReadSubscriptions];
96125
}
97126

98-
impl RequestGet for GetBroadcasterSubscriptionsRequest {}
127+
impl RequestGet for GetBroadcasterSubscriptionsRequest {
128+
fn parse_inner_response(
129+
request: Option<Self>,
130+
uri: &http::Uri,
131+
response: &str,
132+
status: http::StatusCode,
133+
) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestGetError>
134+
where
135+
Self: Sized,
136+
{
137+
#[derive(PartialEq, Deserialize, Debug)]
138+
struct InnerResponse {
139+
data: Vec<BroadcasterSubscription>,
140+
#[serde(default)]
141+
pagination: helix::Pagination,
142+
total: i64,
143+
}
144+
let response: InnerResponse = helix::parse_json(response, true).map_err(|e| {
145+
helix::HelixRequestGetError::DeserializeError(
146+
response.to_string(),
147+
e,
148+
uri.clone(),
149+
status,
150+
)
151+
})?;
152+
Ok(helix::Response {
153+
data: BroadcasterSubscriptions {
154+
subscriptions: response.data,
155+
total: response.total,
156+
},
157+
pagination: response.pagination.cursor,
158+
request,
159+
})
160+
}
161+
}
99162

100163
impl helix::Paginated for GetBroadcasterSubscriptionsRequest {
101164
fn set_pagination(&mut self, cursor: Option<helix::Cursor>) { self.after = cursor }
@@ -109,26 +172,30 @@ fn test_request() {
109172
.broadcaster_id("123".to_string())
110173
.build();
111174

112-
// From twitch docs. Malformed example on https://dev.twitch.tv/docs/api/reference#get-broadcaster-subscriptions
175+
// From twitch docs. Example has ...
113176
let data = br#"
114-
{
115-
"data": [
116-
{
117-
"broadcaster_id": "123",
118-
"broadcaster_login": "test_user",
119-
"broadcaster_name": "test_user",
120-
"is_gift": true,
121-
"tier": "1000",
122-
"plan_name": "The Ninjas",
123-
"user_id": "123",
124-
"user_login": "snoirf",
125-
"user_name": "snoirf"
126-
}
127-
],
128-
"pagination": {
129-
"cursor": "xxxx"
130-
}
131-
}
177+
{
178+
"data": [
179+
{
180+
"broadcaster_id": "141981764",
181+
"broadcaster_login": "twitchdev",
182+
"broadcaster_name": "TwitchDev",
183+
"gifter_id": "12826",
184+
"gifter_login": "twitch",
185+
"gifter_name": "Twitch",
186+
"is_gift": true,
187+
"tier": "1000",
188+
"plan_name": "Channel Subscription (twitchdev)",
189+
"user_id": "527115020",
190+
"user_name": "twitchgaming",
191+
"user_login": "twitchgaming"
192+
}
193+
],
194+
"pagination": {
195+
"cursor": "xxxx"
196+
},
197+
"total": 13
198+
}
132199
"#
133200
.to_vec();
134201

0 commit comments

Comments
 (0)