-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathpagination_async.rs
More file actions
51 lines (44 loc) · 1.74 KB
/
Copy pathpagination_async.rs
File metadata and controls
51 lines (44 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! This example showcases how streams can be used for asynchronous automatic
//! pagination.
//!
//! You'll need to run `cargo add futures futures_util` to get the `pin_mut!`
//! macro and the `stream.try_next()` method.
//!
//! Asynchronous iteration is a bit uglier, since there's currently no
//! syntactic sugar for `for` loops. See this article for more information:
//!
//! https://rust-lang.github.io/async-book/05_streams/02_iteration_and_concurrency.html
use futures::stream::TryStreamExt;
use futures_util::pin_mut;
use rspotify::{prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};
#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();
// May require the `env-file` feature enabled if the environment variables
// aren't configured manually.
let creds = Credentials::from_env().unwrap();
let oauth = OAuth::from_env(scopes!("user-library-read")).unwrap();
let spotify = AuthCodeSpotify::new(creds, oauth);
// Obtaining the access token
let url = spotify.get_authorize_url(false).unwrap();
// This function requires the `cli` feature enabled.
spotify.prompt_for_token(&url).await.unwrap();
// Executing the futures sequentially
let stream = spotify.current_user_saved_tracks(None);
pin_mut!(stream);
println!("Items (blocking):");
while let Some(item) = stream.try_next().await.unwrap() {
println!("* {}", item.track.name);
}
// Executing the futures concurrently
let stream = spotify.current_user_saved_tracks(None);
println!("\nItems (concurrent):");
stream
.try_for_each_concurrent(10, |item| async move {
println!("* {}", item.track.name);
Ok(())
})
.await
.unwrap();
}