Skip to content

Improve helix::client_ext helpers & types in collections #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2024

Conversation

Emilgardis
Copy link
Member

@Emilgardis Emilgardis commented Apr 30, 2023

No description provided.

Comment on lines 140 to 189
) -> impl futures::Stream<Item = Result<helix::streams::Stream, ClientError<C>>>
+ Send
+ Unpin
+ 'client
where
T: TwitchToken + Send + Sync + ?Sized,
{
use futures::StreamExt;
futures::stream::iter(ids.chunks(100).map(move |c| {
let req = helix::streams::GetStreamsRequest::user_ids(c).first(100);
make_stream(req, token, self, std::collections::VecDeque::from)
}))
.flatten_unordered(None)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this construct, maybe we can use it for the other calls as well, to make it possible to query as many things as possible.

Should probably move away from returning a impl Stream then, and instead return a TwitchResults<T> or similar which implements it, which we can also use to capture the token better (I think?)

Copy link
Member Author

@Emilgardis Emilgardis Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, we should just hint to the user; "this is a way to query more, do it this way"

Comment on lines 136 to 176
pub fn get_streams_from_ids<T>(
&'client self,
ids: &'client [&'client types::UserIdRef],
token: &'client T,
) -> impl futures::Stream<Item = Result<helix::streams::Stream, ClientError<C>>>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate it

I want the lifetime to be more explicit, should figure out what the correct bound is (if this is not maximally permissable, which I suspect it isn't)

@Emilgardis Emilgardis changed the title add helper for getting streams Improve helix::client_ext helpers Aug 4, 2023
@Nerixyz
Copy link
Contributor

Nerixyz commented Apr 8, 2024

It's good to include more helpers, esp. for easy pagination.

Seems like we only need to adjust the example:

diff --git a/examples/followed_streams.rs b/examples/followed_streams.rs
index d702260109..b423dfa66c 100644
--- a/examples/followed_streams.rs
+++ b/examples/followed_streams.rs
@@ -1,6 +1,9 @@
+use std::collections::HashMap;
+
 use futures::TryStreamExt;
-use twitch_api::helix::HelixClient;
+use twitch_api::helix::{games::get_games::Game, HelixClient};
 use twitch_oauth2::{AccessToken, UserToken};
+use twitch_types::CategoryId;
 
 fn main() {
     use std::error::Error;
@@ -36,7 +39,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
         .get_followed_streams(&token)
         .try_collect::<Vec<_>>()
         .await?;
-    let games = client
+    let games: HashMap<CategoryId, Game> = client
         .get_games_by_id(
             &streams
                 .iter()
@@ -44,6 +47,8 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
                 .collect::<Vec<_>>(),
             &token,
         )
+        .map_ok(|game| (game.id.clone(), game))
+        .try_collect()
         .await?;
 
     println!(

@Nerixyz

This comment was marked as resolved.

@Emilgardis
Copy link
Member Author

Emilgardis commented Apr 9, 2024

I have some unpushed changes to this branch incorporating twitch-rs/twitch_types#55, I'll clean it up and push for a review

@Emilgardis Emilgardis force-pushed the helpers branch 2 times, most recently from 7ecd9cd to af46a86 Compare April 9, 2024 17:44
@Emilgardis
Copy link
Member Author

i've pushed the changes

Comment on lines 59 to 79
/// let users: Vec<helix::users::User> = client
/// .get_users_from_ids(&["1234", "4321"][..].into(), &token).try_collect().await?;
/// # Ok(()) }
/// ```
pub fn get_users_from_ids<T>(
&'client self,
ids: impl AsRef<[&types::UserIdRef]> + Send,
token: &T,
) -> Result<Option<helix::users::User>, ClientError<C>>
ids: &'client types::Collection<'client, types::UserId>,
token: &'client T,
) -> impl futures::Stream<Item = Result<helix::users::User, ClientError<C>>> + Send + Unpin + 'client
where
T: TwitchToken + Send + Sync + ?Sized,
{
let ids = ids.as_ref();
if ids.len() > 100 {
return Err(ClientRequestError::Custom("too many IDs, max 100".into()));
}
self.req_get(helix::users::GetUsersRequest::ids(ids), token)
.await
.map(|response| response.first())
futures::stream::iter(ids.chunks(100).collect::<Vec<_>>())
.map(move |c| {
let req = helix::users::GetUsersRequest::ids(c);
futures::stream::once(self.req_get(req, token)).boxed()
})
.flatten_unordered(None)
.map_ok(|resp| futures::stream::iter(resp.data.into_iter().map(Ok)))
.try_flatten_unordered(None)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like how the examples have to use [..]

I also tried to make the helper functions take impl Into<Collection<'_, T>> but it didn't work. Still going to try to make it work but I suspect we need some the changes made in 2024 edition for lifetime captures behind impls, or we go the TwitchResult route. I suspect the TwitchResult route is what is needed for doing minimal allocations also

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really only an issue with arrays, no? In real-world code, this would probably be a Vec or a slice already. But I agree that it makes the examples a bit complicated.

Copy link
Contributor

@Nerixyz Nerixyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some copy-paste artifacts.

@Emilgardis
Copy link
Member Author

final check wanted, think we can merge this

@Emilgardis Emilgardis changed the title Improve helix::client_ext helpers Improve helix::client_ext helpers & improve types in collections Apr 15, 2024
@Emilgardis Emilgardis changed the title Improve helix::client_ext helpers & improve types in collections Improve helix::client_ext helpers & types in collections Apr 15, 2024
@Emilgardis Emilgardis requested a review from Nerixyz April 15, 2024 15:40
Copy link
Contributor

@Nerixyz Nerixyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just documentation stuff. For the wrapping, I put four spaces indentation, but three spaces might look better in the docs.

@Emilgardis Emilgardis force-pushed the helpers branch 2 times, most recently from 62216d0 to 788c4ea Compare April 15, 2024 17:13
@Emilgardis Emilgardis enabled auto-merge April 15, 2024 17:13
@Emilgardis Emilgardis added this pull request to the merge queue Apr 15, 2024
Merged via the queue into twitch-rs:main with commit 5d94ce8 Apr 15, 2024
16 checks passed
@Emilgardis Emilgardis deleted the helpers branch April 15, 2024 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants