Skip to content

Commit e4fbf60

Browse files
committed
some touches and make more helpers take an arbitrary amount of items
1 parent 72b8b9d commit e4fbf60

File tree

3 files changed

+70
-62
lines changed

3 files changed

+70
-62
lines changed

src/helix/client/client_ext.rs

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Convenience functions for [HelixClient]
22
#![warn(clippy::future_not_send)]
3+
use futures::{StreamExt, TryStreamExt};
34
use std::borrow::Cow;
45

56
use crate::helix::{self, ClientRequestError, HelixClient};
@@ -160,7 +161,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
160161
where
161162
T: TwitchToken + Send + Sync + ?Sized,
162163
{
163-
use futures::StreamExt;
164164
futures::stream::iter(ids.chunks(100).map(move |c| {
165165
let req = helix::streams::GetStreamsRequest::user_ids(c).first(100);
166166
make_stream(req, token, self, std::collections::VecDeque::from)
@@ -197,7 +197,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
197197
where
198198
T: TwitchToken + Send + Sync + ?Sized,
199199
{
200-
use futures::StreamExt;
201200
futures::stream::iter(logins.chunks(100).map(move |c| {
202201
let req = helix::streams::GetStreamsRequest::user_logins(c).first(100);
203202
make_stream(req, token, self, std::collections::VecDeque::from)
@@ -394,8 +393,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
394393
where
395394
T: TwitchToken + Send + Sync + ?Sized,
396395
{
397-
use futures::StreamExt;
398-
399396
let user_id = match token
400397
.user_id()
401398
.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> {
437434
where
438435
T: TwitchToken + Send + Sync + ?Sized,
439436
{
440-
use futures::StreamExt;
441-
442437
let user_id = match token
443438
.user_id()
444439
.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> {
629624
Ok(resp.data.total)
630625
}
631626

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>(
634629
&'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
638633
where
639634
T: TwitchToken + Send + Sync + ?Sized,
640635
{
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)
655644
}
656645

657646
/// Block a user
@@ -830,17 +819,25 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
830819
}
831820

832821
/// Get emotes in emote set
833-
pub async fn get_emote_sets<T>(
822+
pub fn get_emote_sets<T>(
834823
&'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
838830
where
839831
T: TwitchToken + Send + Sync + ?Sized,
840832
{
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)
844841
}
845842

846843
/// Get a broadcaster's chat settings
@@ -938,24 +935,6 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
938935
Ok(self.req_delete(req, token).await?.data)
939936
}
940937

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-
959938
/// Get a users chat color
960939
pub async fn update_user_chat_color<'b, T>(
961940
&'client self,
@@ -974,22 +953,44 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
974953
Ok(self.req_put(req, helix::EmptyBody, token).await?.data)
975954
}
976955

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>(
979958
&'client self,
980-
user_ids: impl AsRef<[&types::UserIdRef]> + Send,
959+
user_id: impl Into<&types::UserIdRef> + Send,
981960
token: &T,
982-
) -> Result<Vec<helix::chat::UserChatColor>, ClientError<C>>
961+
) -> Result<Option<helix::chat::UserChatColor>, ClientError<C>>
983962
where
984963
T: TwitchToken + Send + Sync + ?Sized,
985964
{
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+
}
991973

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)
993994
}
994995

995996
/// Add a channel moderator
@@ -1132,6 +1133,10 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
11321133

11331134
/// Get specific custom rewards, see [`get_all_custom_rewards`](HelixClient::get_all_custom_rewards) to get all rewards
11341135
///
1136+
/// # Notes
1137+
///
1138+
/// Takes a max of 50 ids
1139+
///
11351140
/// # Examples
11361141
///
11371142
/// ```rust, no_run
@@ -1159,6 +1164,9 @@ impl<'client, C: crate::HttpClient + Sync + 'client> HelixClient<'client, C> {
11591164
where
11601165
T: TwitchToken + Send + Sync + ?Sized,
11611166
{
1167+
if ids.len() > 50 {
1168+
return Err(ClientRequestError::Custom("too many IDs, max 50".into()));
1169+
}
11621170
Ok(self
11631171
.req_get(
11641172
helix::points::GetCustomRewardRequest::broadcaster_id(broadcaster_id)
@@ -1335,7 +1343,6 @@ where
13351343
// I also want to keep allocations low, so std::mem::take is perfect, but that makes get_next not work optimally.
13361344
<Req as super::Request>::Response: Send + Sync + std::fmt::Debug + Clone,
13371345
{
1338-
use futures::StreamExt;
13391346
enum StateMode<Req: super::Request + super::RequestGet, Item> {
13401347
/// A request needs to be done.
13411348
Req(Option<Req>),

src/helix/endpoints/points/get_custom_reward.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a> GetCustomRewardRequest<'a> {
7272
}
7373
}
7474

75-
/// Get rewards with these ids
75+
/// Get rewards with these ids. Maximum 50
7676
pub fn ids(mut self, id: impl Into<Cow<'a, [&'a types::RewardIdRef]>>) -> Self {
7777
self.id = id.into();
7878
self

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
22
#![cfg_attr(nightly, feature(doc_cfg))]
33
#![cfg_attr(nightly, feature(doc_auto_cfg))]
4+
#![allow(clippy::needless_raw_string_hashes)]
45
#![doc(html_root_url = "https://docs.rs/twitch_api/0.7.0-rc.7")]
56
//! [![github]](https://github.com/twitch-rs/twitch_api)&ensp;[![crates-io]](https://crates.io/crates/twitch_api)&ensp;[![docs-rs-big]](https://docs.rs/twitch_api/0.7.0-rc.7/twitch_api)
67
//!

0 commit comments

Comments
 (0)