1
1
//! Convenience functions for [HelixClient]
2
2
#![ warn( clippy:: future_not_send) ]
3
+ use futures:: { StreamExt , TryStreamExt } ;
3
4
use std:: borrow:: Cow ;
4
5
5
6
use crate :: helix:: { self , ClientRequestError , HelixClient } ;
@@ -160,7 +161,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
160
161
where
161
162
T : TwitchToken + Send + Sync + ?Sized ,
162
163
{
163
- use futures:: StreamExt ;
164
164
futures:: stream:: iter ( ids. chunks ( 100 ) . map ( move |c| {
165
165
let req = helix:: streams:: GetStreamsRequest :: user_ids ( c) . first ( 100 ) ;
166
166
make_stream ( req, token, self , std:: collections:: VecDeque :: from)
@@ -197,7 +197,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
197
197
where
198
198
T : TwitchToken + Send + Sync + ?Sized ,
199
199
{
200
- use futures:: StreamExt ;
201
200
futures:: stream:: iter ( logins. chunks ( 100 ) . map ( move |c| {
202
201
let req = helix:: streams:: GetStreamsRequest :: user_logins ( c) . first ( 100 ) ;
203
202
make_stream ( req, token, self , std:: collections:: VecDeque :: from)
@@ -394,8 +393,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
394
393
where
395
394
T : TwitchToken + Send + Sync + ?Sized ,
396
395
{
397
- use futures:: StreamExt ;
398
-
399
396
let user_id = match token
400
397
. user_id ( )
401
398
. ok_or_else ( || ClientRequestError :: Custom ( "no user_id found on token" . into ( ) ) )
@@ -437,8 +434,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
437
434
where
438
435
T : TwitchToken + Send + Sync + ?Sized ,
439
436
{
440
- use futures:: StreamExt ;
441
-
442
437
let user_id = match token
443
438
. user_id ( )
444
439
. ok_or_else ( || ClientRequestError :: Custom ( "no user_id found on token" . into ( ) ) )
@@ -629,29 +624,23 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
629
624
Ok ( resp. data . total )
630
625
}
631
626
632
- /// Get games by ID. Can only be at max 100 ids.
633
- pub async fn get_games_by_id < T > (
627
+ /// Get games by ID.
628
+ pub fn get_games_by_id < T > (
634
629
& ' client self ,
635
- ids : impl AsRef < [ & ' client types:: CategoryIdRef ] > + Send ,
636
- token : & T ,
637
- ) -> Result < std :: collections :: HashMap < types :: CategoryId , helix:: games:: Game > , ClientError < C > >
630
+ ids : & ' client [ & ' client types:: CategoryIdRef ] ,
631
+ token : & ' client T ,
632
+ ) -> impl futures :: Stream < Item = Result < helix:: games:: Game , ClientError < C > > > + Send + Unpin + ' client
638
633
where
639
634
T : TwitchToken + Send + Sync + ?Sized ,
640
635
{
641
- let ids = ids. as_ref ( ) ;
642
- if ids. len ( ) > 100 {
643
- return Err ( ClientRequestError :: Custom ( "too many IDs, max 100" . into ( ) ) ) ;
644
- }
645
-
646
- let resp = self
647
- . req_get ( helix:: games:: GetGamesRequest :: ids ( ids) , token)
648
- . await ?;
649
-
650
- Ok ( resp
651
- . data
652
- . into_iter ( )
653
- . map ( |g : helix:: games:: Game | ( g. id . clone ( ) , g) )
654
- . collect ( ) )
636
+ futures:: stream:: iter ( ids. chunks ( 100 ) )
637
+ . map ( move |c| {
638
+ let req = helix:: games:: GetGamesRequest :: ids ( c) ;
639
+ futures:: stream:: once ( self . req_get ( req, token) ) . boxed ( )
640
+ } )
641
+ . flatten_unordered ( None )
642
+ . map_ok ( |resp| futures:: stream:: iter ( resp. data . into_iter ( ) . map ( Ok ) ) )
643
+ . try_flatten_unordered ( None )
655
644
}
656
645
657
646
/// Block a user
@@ -830,17 +819,25 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
830
819
}
831
820
832
821
/// Get emotes in emote set
833
- pub async fn get_emote_sets < T > (
822
+ pub fn get_emote_sets < T > (
834
823
& ' client self ,
835
- emote_sets : impl AsRef < [ & types:: EmoteSetIdRef ] > + Send ,
836
- token : & T ,
837
- ) -> Result < Vec < helix:: chat:: get_emote_sets:: Emote > , ClientError < C > >
824
+ emote_sets : & ' client [ & ' client types:: EmoteSetIdRef ] ,
825
+ token : & ' client T ,
826
+ ) -> impl futures:: Stream < Item = Result < helix:: chat:: get_emote_sets:: Emote , ClientError < C > > >
827
+ + Send
828
+ + Unpin
829
+ + ' client
838
830
where
839
831
T : TwitchToken + Send + Sync + ?Sized ,
840
832
{
841
- let emote_sets = emote_sets. as_ref ( ) ;
842
- let req = helix:: chat:: GetEmoteSetsRequest :: emote_set_ids ( emote_sets) ;
843
- Ok ( self . req_get ( req, token) . await ?. data )
833
+ futures:: stream:: iter ( emote_sets. chunks ( 100 ) )
834
+ . map ( move |c| {
835
+ let req = helix:: chat:: GetEmoteSetsRequest :: emote_set_ids ( c) ;
836
+ futures:: stream:: once ( self . req_get ( req, token) ) . boxed ( )
837
+ } )
838
+ . flatten_unordered ( None )
839
+ . map_ok ( |r| futures:: stream:: iter ( r. data . into_iter ( ) . map ( Ok ) ) )
840
+ . try_flatten_unordered ( None )
844
841
}
845
842
846
843
/// Get a broadcaster's chat settings
@@ -938,24 +935,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
938
935
Ok ( self . req_delete ( req, token) . await ?. data )
939
936
}
940
937
941
- /// Get a users chat color
942
- pub async fn get_user_chat_color < T > (
943
- & ' client self ,
944
- user_id : impl Into < & types:: UserIdRef > + Send ,
945
- token : & T ,
946
- ) -> Result < Option < helix:: chat:: UserChatColor > , ClientError < C > >
947
- where
948
- T : TwitchToken + Send + Sync + ?Sized ,
949
- {
950
- Ok ( self
951
- . req_get (
952
- helix:: chat:: GetUserChatColorRequest :: user_ids ( & [ user_id. into ( ) ] [ ..] ) ,
953
- token,
954
- )
955
- . await ?
956
- . first ( ) )
957
- }
958
-
959
938
/// Get a users chat color
960
939
pub async fn update_user_chat_color < ' b , T > (
961
940
& ' client self ,
@@ -974,22 +953,44 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
974
953
Ok ( self . req_put ( req, helix:: EmptyBody , token) . await ?. data )
975
954
}
976
955
977
- /// Get multiple users chat colors
978
- pub async fn get_users_chat_colors < T > (
956
+ /// Get a users chat color
957
+ pub async fn get_user_chat_color < T > (
979
958
& ' client self ,
980
- user_ids : impl AsRef < [ & types:: UserIdRef ] > + Send ,
959
+ user_id : impl Into < & types:: UserIdRef > + Send ,
981
960
token : & T ,
982
- ) -> Result < Vec < helix:: chat:: UserChatColor > , ClientError < C > >
961
+ ) -> Result < Option < helix:: chat:: UserChatColor > , ClientError < C > >
983
962
where
984
963
T : TwitchToken + Send + Sync + ?Sized ,
985
964
{
986
- let user_ids = user_ids. as_ref ( ) ;
987
- if user_ids. len ( ) > 100 {
988
- return Err ( ClientRequestError :: Custom ( "too many IDs, max 100" . into ( ) ) ) ;
989
- }
990
- let req = helix:: chat:: GetUserChatColorRequest :: user_ids ( user_ids) ;
965
+ Ok ( self
966
+ . req_get (
967
+ helix:: chat:: GetUserChatColorRequest :: user_ids ( & [ user_id. into ( ) ] [ ..] ) ,
968
+ token,
969
+ )
970
+ . await ?
971
+ . first ( ) )
972
+ }
991
973
992
- Ok ( self . req_get ( req, token) . await ?. data )
974
+ /// Get multiple users chat colors
975
+ pub fn get_users_chat_colors < T > (
976
+ & ' client self ,
977
+ user_ids : & ' client [ & ' client types:: UserIdRef ] ,
978
+ token : & ' client T ,
979
+ ) -> impl futures:: Stream < Item = Result < helix:: chat:: UserChatColor , ClientError < C > > >
980
+ + Send
981
+ + Unpin
982
+ + ' client
983
+ where
984
+ T : TwitchToken + Send + Sync + ?Sized ,
985
+ {
986
+ futures:: stream:: iter ( user_ids. chunks ( 100 ) )
987
+ . map ( move |c| {
988
+ let req = helix:: chat:: GetUserChatColorRequest :: user_ids ( c) ;
989
+ futures:: stream:: once ( self . req_get ( req, token) ) . boxed ( )
990
+ } )
991
+ . flatten_unordered ( None )
992
+ . map_ok ( move |o| futures:: stream:: iter ( o. data . into_iter ( ) . map ( Ok ) ) )
993
+ . try_flatten_unordered ( None )
993
994
}
994
995
995
996
/// Add a channel moderator
@@ -1132,6 +1133,10 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
1132
1133
1133
1134
/// Get specific custom rewards, see [`get_all_custom_rewards`](HelixClient::get_all_custom_rewards) to get all rewards
1134
1135
///
1136
+ /// # Notes
1137
+ ///
1138
+ /// Takes a max of 50 ids
1139
+ ///
1135
1140
/// # Examples
1136
1141
///
1137
1142
/// ```rust, no_run
@@ -1159,6 +1164,9 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
1159
1164
where
1160
1165
T : TwitchToken + Send + Sync + ?Sized ,
1161
1166
{
1167
+ if ids. len ( ) > 50 {
1168
+ return Err ( ClientRequestError :: Custom ( "too many IDs, max 50" . into ( ) ) ) ;
1169
+ }
1162
1170
Ok ( self
1163
1171
. req_get (
1164
1172
helix:: points:: GetCustomRewardRequest :: broadcaster_id ( broadcaster_id)
@@ -1335,7 +1343,6 @@ where
1335
1343
// I also want to keep allocations low, so std::mem::take is perfect, but that makes get_next not work optimally.
1336
1344
<Req as super :: Request >:: Response : Send + Sync + std:: fmt:: Debug + Clone ,
1337
1345
{
1338
- use futures:: StreamExt ;
1339
1346
enum StateMode < Req : super :: Request + super :: RequestGet , Item > {
1340
1347
/// A request needs to be done.
1341
1348
Req ( Option < Req > ) ,
0 commit comments