Skip to content

Commit af46a86

Browse files
committed
make it work
1 parent b486c10 commit af46a86

File tree

8 files changed

+80
-62
lines changed

8 files changed

+80
-62
lines changed

examples/followed_streams.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
3838
.await?;
3939
let games = client
4040
.get_games_by_id(
41-
streams
41+
&streams
4242
.iter()
4343
.map(|s| &s.game_id)
44-
.collect::<Vec<_>>().into(),
44+
.collect::<Vec<_>>()
45+
.into(),
4546
&token,
4647
)
4748
.map_ok(|g| (g.id.clone(), g))
48-
.try_collect::<std::collections::HashMap<_,_>>()
49+
.try_collect::<std::collections::HashMap<_, _>>()
4950
.await?;
5051

5152
println!(

src/helix/client/client_ext.rs

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,38 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
4444
}
4545

4646
/// Get multiple [User](helix::users::User)s from user ids.
47-
pub async fn get_users_from_ids<T>(
47+
///
48+
/// # Examples
49+
///
50+
/// ```rust, no_run
51+
/// # #[tokio::main]
52+
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
53+
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
54+
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
55+
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
56+
/// use twitch_api::{helix, types};
57+
/// use futures::TryStreamExt;
58+
///
59+
/// let users: Vec<helix::users::User> = client
60+
/// .get_users_from_ids(&["1234", "4321"][..].into(), &token).try_collect().await?;
61+
/// # Ok(()) }
62+
/// ```
63+
pub fn get_users_from_ids<T>(
4864
&'client self,
49-
ids: impl AsRef<[&types::UserIdRef]> + Send,
50-
token: &T,
51-
) -> Result<Option<helix::users::User>, ClientError<C>>
65+
ids: &'client types::Collection<'client, types::UserId>,
66+
token: &'client T,
67+
) -> impl futures::Stream<Item = Result<helix::users::User, ClientError<C>>> + Send + Unpin + 'client
5268
where
5369
T: TwitchToken + Send + Sync + ?Sized,
5470
{
55-
let ids = ids.as_ref();
56-
if ids.len() > 100 {
57-
return Err(ClientRequestError::Custom("too many IDs, max 100".into()));
58-
}
59-
self.req_get(helix::users::GetUsersRequest::ids(ids), token)
60-
.await
61-
.map(|response| response.first())
71+
futures::stream::iter(ids.chunks(100).collect::<Vec<_>>())
72+
.map(move |c| {
73+
let req = helix::users::GetUsersRequest::ids(c);
74+
futures::stream::once(self.req_get(req, token)).boxed()
75+
})
76+
.flatten_unordered(None)
77+
.map_ok(|resp| futures::stream::iter(resp.data.into_iter().map(Ok)))
78+
.try_flatten_unordered(None)
6279
}
6380

6481
/// Get [ChannelInformation](helix::channels::ChannelInformation) from a broadcasters login
@@ -106,30 +123,31 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
106123
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
107124
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
108125
/// use twitch_api::{helix, types};
126+
/// use futures::TryStreamExt;
109127
///
110-
/// let ids: &[&types::UserIdRef] = &["1234".into(), "4321".into()];
111128
/// let chatters: Vec<helix::channels::ChannelInformation> = client
112-
/// .get_channels_from_ids(ids, &token).await?;
129+
/// .get_channels_from_ids(&["1234", "4321"][..].into(), &token).try_collect().await?;
113130
/// # Ok(()) }
114131
/// ```
115-
pub async fn get_channels_from_ids<'b, T>(
132+
pub fn get_channels_from_ids<T>(
116133
&'client self,
117-
ids: impl AsRef<[&types::UserIdRef]> + Send,
118-
token: &T,
119-
) -> Result<Vec<helix::channels::ChannelInformation>, ClientError<C>>
134+
ids: &'client types::Collection<'client, types::UserId>,
135+
token: &'client T,
136+
) -> impl futures::Stream<Item = Result<helix::channels::ChannelInformation, ClientError<C>>>
137+
+ Send
138+
+ Unpin
139+
+ 'client
120140
where
121141
T: TwitchToken + Send + Sync + ?Sized,
122142
{
123-
let ids = ids.as_ref();
124-
if ids.len() > 100 {
125-
return Err(ClientRequestError::Custom("too many IDs, max 100".into()));
126-
}
127-
self.req_get(
128-
helix::channels::GetChannelInformationRequest::broadcaster_ids(ids),
129-
token,
130-
)
131-
.await
132-
.map(|response| response.data)
143+
futures::stream::iter(ids.chunks(100).collect::<Vec<_>>())
144+
.map(move |c| {
145+
let req = helix::channels::GetChannelInformationRequest::broadcaster_ids(c);
146+
futures::stream::once(self.req_get(req, token)).boxed()
147+
})
148+
.flatten_unordered(None)
149+
.map_ok(|resp| futures::stream::iter(resp.data.into_iter().map(Ok)))
150+
.try_flatten_unordered(None)
133151
}
134152

135153
/// Get multiple [Stream](helix::streams::Stream)s from user ids.
@@ -145,12 +163,12 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
145163
/// use twitch_api::{types, helix};
146164
/// use futures::TryStreamExt;
147165
///
148-
/// let live: Vec<helix::streams::Stream> = client.get_streams_from_ids(&["123456", "987654"], &token).try_collect().await?;
166+
/// let live: Vec<helix::streams::Stream> = client.get_streams_from_ids(&["123456", "987654"][..].into(), &token).try_collect().await?;
149167
/// # Ok(()) }
150168
/// ```
151169
pub fn get_streams_from_ids<T>(
152170
&'client self,
153-
ids: impl Into<types::Collection<'client, types::UserId>>,
171+
ids: &'client types::Collection<'client, types::UserId>,
154172
token: &'client T,
155173
) -> impl futures::Stream<Item = Result<helix::streams::Stream, ClientError<C>>>
156174
+ Send
@@ -159,7 +177,7 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
159177
where
160178
T: TwitchToken + Send + Sync + ?Sized,
161179
{
162-
let ids = ids.into().chunks(100).collect::<Vec<_>>();
180+
let ids = ids.chunks(100).collect::<Vec<_>>();
163181
futures::stream::iter(ids.into_iter().map(move |c| {
164182
let req = helix::streams::GetStreamsRequest::user_ids(c).first(100);
165183
make_stream(req, token, self, std::collections::VecDeque::from)
@@ -180,12 +198,12 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
180198
/// use twitch_api::{types, helix};
181199
/// use futures::TryStreamExt;
182200
///
183-
/// let live: Vec<helix::streams::Stream> = client.get_streams_from_logins(&["twitchdev", "justinfan"], &token).try_collect().await?;
201+
/// let live: Vec<helix::streams::Stream> = client.get_streams_from_logins(&["twitchdev", "justinfan"][..].into(), &token).try_collect().await?;
184202
/// # Ok(()) }
185203
/// ```
186204
pub fn get_streams_from_logins<T>(
187205
&'client self,
188-
logins: impl Into<types::Collection<'client, types::UserName>>,
206+
logins: &'client types::Collection<'client, types::UserName>,
189207
token: &'client T,
190208
) -> impl futures::Stream<Item = Result<helix::streams::Stream, ClientError<C>>>
191209
+ Send
@@ -194,7 +212,7 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
194212
where
195213
T: TwitchToken + Send + Sync + ?Sized,
196214
{
197-
let logins = logins.into().chunks(100).collect::<Vec<_>>();
215+
let logins = logins.chunks(100).collect::<Vec<_>>();
198216
futures::stream::iter(logins.into_iter().map(move |c| {
199217
let req = helix::streams::GetStreamsRequest::user_logins(c).first(100);
200218
make_stream(req, token, self, std::collections::VecDeque::from)
@@ -626,18 +644,17 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
626644
/// use twitch_api::{types, helix};
627645
/// use futures::TryStreamExt;
628646
///
629-
/// let games: Vec<helix::games::Game> = client.get_games_by_id(&["509658", "32982", "27471"], &token).try_collect().await?;
647+
/// let games: Vec<helix::games::Game> = client.get_games_by_id(&["509658", "32982", "27471"][..].into(), &token).try_collect().await?;
630648
/// # Ok(()) }
631649
/// ```
632650
pub fn get_games_by_id<T>(
633651
&'client self,
634-
ids: impl Into<types::Collection<'client, types::CategoryId>>,
652+
ids: &'client types::Collection<'client, types::CategoryId>,
635653
token: &'client T,
636654
) -> impl futures::Stream<Item = Result<helix::games::Game, ClientError<C>>> + Send + Unpin + 'client
637655
where
638656
T: TwitchToken + Send + Sync + ?Sized,
639657
{
640-
let ids = ids.into();
641658
futures::stream::iter(ids.chunks(100).collect::<Vec<_>>())
642659
.map(move |c| {
643660
let req = helix::games::GetGamesRequest::ids(c);
@@ -834,16 +851,12 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
834851
/// use twitch_api::{types, helix};
835852
/// use futures::TryStreamExt;
836853
///
837-
/// let games: Vec<helix::chat::get_emote_sets::Emote> = client.get_emote_sets(&["0"], &token).try_collect().await?;
854+
/// let games: Vec<helix::chat::get_emote_sets::Emote> = client.get_emote_sets(&["0"][..].into(), &token).try_collect().await?;
838855
/// # Ok(()) }
839856
/// ```
840857
pub fn get_emote_sets<T>(
841858
&'client self,
842-
// doesn't work:
843-
emote_sets: impl Into<types::Collection<'client, types::EmoteSetId>>,
844-
// works:
845-
// emote_sets: &'client [&'client types::EmoteSetIdRef],
846-
//emote_sets: impl AsRef<types::Collection<'client, types::EmoteSetId>> + 'client,
859+
emote_sets: &'client types::Collection<'client, types::EmoteSetId>,
847860
token: &'client T,
848861
) -> impl futures::Stream<Item = Result<helix::chat::get_emote_sets::Emote, ClientError<C>>>
849862
+ Send
@@ -852,7 +865,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
852865
where
853866
T: TwitchToken + Send + Sync + ?Sized,
854867
{
855-
let emote_sets = emote_sets.into().to_owned();
856868
futures::stream::iter(emote_sets.chunks(25).collect::<Vec<_>>())
857869
.map(move |c| {
858870
let req = helix::chat::GetEmoteSetsRequest::emote_set_ids(c);
@@ -1007,12 +1019,12 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
10071019
/// use twitch_api::{types, helix};
10081020
/// use futures::TryStreamExt;
10091021
///
1010-
/// let games: Vec<helix::chat::UserChatColor> = client.get_users_chat_colors(&["1234"], &token).try_collect().await?;
1022+
/// let games: Vec<helix::chat::UserChatColor> = client.get_users_chat_colors(&["1234"][..].into(), &token).try_collect().await?;
10111023
/// # Ok(()) }
10121024
/// ```
10131025
pub fn get_users_chat_colors<T>(
10141026
&'client self,
1015-
user_ids: impl Into<types::Collection<'client, types::UserId>> + 'client,
1027+
user_ids: &'client types::Collection<'client, types::UserId>,
10161028
token: &'client T,
10171029
) -> impl futures::Stream<Item = Result<helix::chat::UserChatColor, ClientError<C>>>
10181030
+ Send
@@ -1021,7 +1033,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
10211033
where
10221034
T: TwitchToken + Send + Sync + ?Sized,
10231035
{
1024-
let user_ids = user_ids.into();
10251036
futures::stream::iter(user_ids.chunks(100))
10261037
.map(move |c| {
10271038
let req = helix::chat::GetUserChatColorRequest::user_ids(c);
@@ -1165,7 +1176,7 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
11651176
where
11661177
T: TwitchToken + Send + Sync + ?Sized,
11671178
{
1168-
self.get_custom_rewards(broadcaster_id, only_managable_rewards, &[], token)
1179+
self.get_custom_rewards(broadcaster_id, only_managable_rewards, &types::Collection::EMPTY, token)
11691180
.await
11701181
}
11711182

@@ -1186,17 +1197,17 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
11861197
/// use twitch_api::helix;
11871198
///
11881199
/// let rewards: Vec<helix::points::CustomReward> = client
1189-
/// .get_custom_rewards("1234", true, &["8969ec47-55b6-4559-a8fe-3f1fc4e6fe58".into()], &token)
1200+
/// .get_custom_rewards("1234", true, &["8969ec47-55b6-4559-a8fe-3f1fc4e6fe58"][..].into(), &token)
11901201
/// .await?;
11911202
/// # Ok(()) }
11921203
/// ```
1204+
// XXX: This function is useless as a stream, since you can never have more than 50 rewards on a channel
11931205
pub async fn get_custom_rewards<'b, T>(
11941206
&'client self,
11951207
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + Send + 'b,
11961208
only_managable_rewards: bool,
1197-
// FIXME: This should be `impl AsRef<[&'b T]> + 'b`
1198-
ids: &'b [&'b types::RewardIdRef],
1199-
token: &T,
1209+
ids: &'b types::Collection<'b, types::RewardId>,
1210+
token: &'client T,
12001211
) -> Result<Vec<helix::points::CustomReward>, ClientError<C>>
12011212
where
12021213
T: TwitchToken + Send + Sync + ?Sized,
@@ -1208,7 +1219,7 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
12081219
.req_get(
12091220
helix::points::GetCustomRewardRequest::broadcaster_id(broadcaster_id)
12101221
.only_manageable_rewards(only_managable_rewards)
1211-
.ids(ids),
1222+
.ids(ids.clone()),
12121223
token,
12131224
)
12141225
.await?

src/helix/endpoints/channels/get_channel_information.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ impl<'a> GetChannelInformationRequest<'a> {
7070
/// ids,
7171
/// );
7272
/// ```
73-
pub fn broadcaster_ids(broadcaster_ids: impl Into<types::Collection<'a, types::UserId>>) -> Self {
73+
pub fn broadcaster_ids(
74+
broadcaster_ids: impl Into<types::Collection<'a, types::UserId>>,
75+
) -> Self {
7476
Self {
7577
broadcaster_id: broadcaster_ids.into(),
7678
}

src/helix/endpoints/chat/get_emote_sets.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ pub struct GetEmoteSetsRequest<'a> {
5757

5858
impl<'a> GetEmoteSetsRequest<'a> {
5959
/// Get emotes in these sets
60-
pub fn emote_set_ids(emote_set_ids: impl Into<types::Collection<'a, types::EmoteSetId>>) -> Self {
60+
pub fn emote_set_ids(
61+
emote_set_ids: impl Into<types::Collection<'a, types::EmoteSetId>>,
62+
) -> Self {
6163
Self {
6264
emote_set_id: emote_set_ids.into(),
6365
}
@@ -106,7 +108,7 @@ impl Emote {
106108
/// # let client: helix::HelixClient<'static, client::DummyHttpClient> = helix::HelixClient::default();
107109
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
108110
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
109-
/// let emotes: Vec<_> = client.get_emote_sets(&["301590448"], &token).try_collect().await?;
111+
/// let emotes: Vec<_> = client.get_emote_sets(&["301590448"][..].into(), &token).try_collect().await?;
110112
/// assert_eq!(emotes[0].url().size_3x().dark_mode().render(), "https://static-cdn.jtvnw.net/emoticons/v2/emotesv2_dc24652ada1e4c84a5e3ceebae4de709/default/dark/3.0");
111113
/// # Ok(())
112114
/// # }

src/helix/endpoints/chat/get_user_chat_color.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
//!
1010
//! ```rust
1111
//! use twitch_api::helix::chat::get_user_chat_color;
12-
//! let request = get_user_chat_color::GetUserChatColorRequest::user_ids(&["4321"]);
12+
//! let request =
13+
//! get_user_chat_color::GetUserChatColorRequest::user_ids(&["4321"]);
1314
//! ```
1415
//!
1516
//! ## Response: [UserChatColor]

src/helix/endpoints/streams/get_streams.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ impl<'a> GetStreamsRequest<'a> {
141141
}
142142
}
143143

144-
145144
/// Return Values for [Get Streams](super::get_streams)
146145
///
147146
/// [`get-streams`](https://dev.twitch.tv/docs/api/reference#get-streams)

src/helix/endpoints/videos/delete_videos.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ pub struct DeleteVideosRequest<'a> {
5858

5959
impl<'a> DeleteVideosRequest<'a> {
6060
/// ID of the videos to be deleted
61-
pub fn ids(ids: impl Into<types::Collection<'a, types::VideoId>>) -> Self { Self { id: ids.into() } }
61+
pub fn ids(ids: impl Into<types::Collection<'a, types::VideoId>>) -> Self {
62+
Self { id: ids.into() }
63+
}
6264
}
6365
// FIXME: Should return VideoIds
6466
/// Return Values for [Delete Videos](super::delete_videos)

twitch_types

0 commit comments

Comments
 (0)