pub struct CanvasClient { /* private fields */ }Expand description
The main client for interacting with Spotify’s private Canvas API.
Handles authentication (client-token exchange) and GraphQL queries internally.
Implementations§
Source§impl CanvasClient
impl CanvasClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new CanvasClient with default configuration and a new reqwest::Client.
Examples found in repository?
examples/simple.rs (line 28)
5async fn main() -> Result<()> {
6 // 1. Get Access Token from environment or args
7 let access_token = match env::var("SPOTIFY_TOKEN") {
8 Ok(t) => t,
9 Err(_) => {
10 eprintln!("⚠️ SPOTIFY_TOKEN environment variable not set.");
11 eprintln!(" Please set it to a valid Spotify Access Token.");
12 eprintln!(" ");
13 eprintln!(" HOW TO GET A TOKEN:");
14 eprintln!(" 1. Open https://open.spotify.com in your browser.");
15 eprintln!(" 2. Log in.");
16 eprintln!(" 3. Open Developer Tools (F12) -> Network tab.");
17 eprintln!(" 4. Filter for 'pathfinder'.");
18 eprintln!(" 5. Click any request, go to 'Headers' -> 'Request Headers'.");
19 eprintln!(" 6. Copy the 'authorization' header (starts with 'Bearer ...').");
20 eprintln!(
21 " 7. Run: $env:SPOTIFY_TOKEN='...' ; cargo run --example simple (PowerShell)"
22 );
23 return Ok(());
24 }
25 };
26
27 // 2. Initialize Client
28 let mut client = CanvasClient::new();
29
30 // 3. Define a track URI (e.g., "KORE" by Zynyx)
31 let track_uri = "spotify:track:72Xn6x8xqegX64AKeJDsZt";
32
33 println!("Fetching canvas for: {}", track_uri);
34
35 // 4. Fetch
36 match client.get_canvas(track_uri, &access_token).await {
37 Ok(canvas) => {
38 println!("✅ Canvas Found!");
39 println!(" - MP4 URL: {}", canvas.mp4_url);
40 println!(" - Canvas URI: {:?}", canvas.uri);
41 }
42 Err(e) => {
43 eprintln!("❌ Error: {}", e);
44 }
45 }
46
47 Ok(())
48}Sourcepub fn with_config(config: CanvasConfig) -> Self
pub fn with_config(config: CanvasConfig) -> Self
Create a new CanvasClient with a custom configuration.
Sourcepub fn with_client(client: Client, config: CanvasConfig) -> Self
pub fn with_client(client: Client, config: CanvasConfig) -> Self
Creates a new CanvasClient using an existing reqwest::Client.
Useful if you want to share a connection pool or proxy configuration.
Sourcepub async fn get_canvas(
&mut self,
track_uri: &str,
access_token: &str,
) -> Result<Canvas>
pub async fn get_canvas( &mut self, track_uri: &str, access_token: &str, ) -> Result<Canvas>
Fetch the Canvas video URL for a given Spotify Track URI.
§Arguments
track_uri- The Spotify Track URI (e.g., “spotify:track:…”)access_token- A valid Spotify Access Token (Bearer). Fetches the Spotify Canvas (looping video) for a given track URI.
§Arguments
track_uri- The Spotify URI of the track (e.g.,"spotify:track:...").access_token- A valid Spotify Web Player access token (starts withBearer ...).
§Errors
Returns a CanvasError if:
- The
access_tokenis invalid or expired (CanvasError::TokenExpired). - The track has no canvas (
CanvasError::NotFound). - Rate limited by Spotify (
CanvasError::RateLimited). - Network or parsing errors occur.
§Example
let canvas = client.get_canvas("spotify:track:...", "Bearer ...").await?;
println!("Canvas URL: {}", canvas.mp4_url);Examples found in repository?
examples/simple.rs (line 36)
5async fn main() -> Result<()> {
6 // 1. Get Access Token from environment or args
7 let access_token = match env::var("SPOTIFY_TOKEN") {
8 Ok(t) => t,
9 Err(_) => {
10 eprintln!("⚠️ SPOTIFY_TOKEN environment variable not set.");
11 eprintln!(" Please set it to a valid Spotify Access Token.");
12 eprintln!(" ");
13 eprintln!(" HOW TO GET A TOKEN:");
14 eprintln!(" 1. Open https://open.spotify.com in your browser.");
15 eprintln!(" 2. Log in.");
16 eprintln!(" 3. Open Developer Tools (F12) -> Network tab.");
17 eprintln!(" 4. Filter for 'pathfinder'.");
18 eprintln!(" 5. Click any request, go to 'Headers' -> 'Request Headers'.");
19 eprintln!(" 6. Copy the 'authorization' header (starts with 'Bearer ...').");
20 eprintln!(
21 " 7. Run: $env:SPOTIFY_TOKEN='...' ; cargo run --example simple (PowerShell)"
22 );
23 return Ok(());
24 }
25 };
26
27 // 2. Initialize Client
28 let mut client = CanvasClient::new();
29
30 // 3. Define a track URI (e.g., "KORE" by Zynyx)
31 let track_uri = "spotify:track:72Xn6x8xqegX64AKeJDsZt";
32
33 println!("Fetching canvas for: {}", track_uri);
34
35 // 4. Fetch
36 match client.get_canvas(track_uri, &access_token).await {
37 Ok(canvas) => {
38 println!("✅ Canvas Found!");
39 println!(" - MP4 URL: {}", canvas.mp4_url);
40 println!(" - Canvas URI: {:?}", canvas.uri);
41 }
42 Err(e) => {
43 eprintln!("❌ Error: {}", e);
44 }
45 }
46
47 Ok(())
48}Trait Implementations§
Source§impl Clone for CanvasClient
impl Clone for CanvasClient
Source§fn clone(&self) -> CanvasClient
fn clone(&self) -> CanvasClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CanvasClient
impl Debug for CanvasClient
Auto Trait Implementations§
impl Freeze for CanvasClient
impl !RefUnwindSafe for CanvasClient
impl Send for CanvasClient
impl Sync for CanvasClient
impl Unpin for CanvasClient
impl !UnwindSafe for CanvasClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more