pub struct LastFmClient { /* private fields */ }Expand description
Main Last.fm API client
This is the entry point for interacting with the Last.fm API using the new v2.0 API.
§Example
use lastfm_client::LastFmClient;
use std::time::Duration;
// Create client with custom configuration
let client = LastFmClient::builder()
.api_key("your_api_key")
.timeout(Duration::from_secs(60))
.max_concurrent_requests(10)
.build()
.unwrap();
// Use client.recent_tracks() to fetch dataImplementations§
Source§impl LastFmClient
impl LastFmClient
Sourcepub fn builder() -> ConfigBuilder
pub fn builder() -> ConfigBuilder
Create a new configuration builder
This is the recommended way to create a LastFmClient.
§Example
use lastfm_client::LastFmClient;
let client = LastFmClient::builder()
.api_key("your_api_key")
.build()?;Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new LastFmClient with default configuration
This will automatically try to load the API key from the LAST_FM_API_KEY
environment variable. All other settings use sensible defaults.
§Example
use lastfm_client::LastFmClient;
let client = LastFmClient::new()?;§Errors
Returns an error if the API key is not set and cannot be loaded from environment
Sourcepub fn from_config(config: Config) -> Self
pub fn from_config(config: Config) -> Self
Create a new LastFmClient from a configuration
This automatically sets up retry logic and rate limiting based on the configuration.
Most users should use builder() instead.
Sourcepub fn with_http(config: Config, http: Arc<dyn HttpClient>) -> Self
pub fn with_http(config: Config, http: Arc<dyn HttpClient>) -> Self
Create a new LastFmClient with a custom HTTP client
This is primarily useful for testing with a mock HTTP client.
§Example
use lastfm_client::{LastFmClient, Config, ConfigBuilder};
use lastfm_client::client::MockClient;
use std::sync::Arc;
let config = ConfigBuilder::new()
.api_key("test_key")
.build()?;
let mock = MockClient::new();
let client = LastFmClient::with_http(config, Arc::new(mock));Sourcepub fn recent_tracks(
&self,
username: impl Into<String>,
) -> RecentTracksRequestBuilder
pub fn recent_tracks( &self, username: impl Into<String>, ) -> RecentTracksRequestBuilder
Get a builder for recent tracks requests
§Example
let tracks = client
.recent_tracks("username")
.limit(100)
.fetch()
.await?;Sourcepub fn loved_tracks(
&self,
username: impl Into<String>,
) -> LovedTracksRequestBuilder
pub fn loved_tracks( &self, username: impl Into<String>, ) -> LovedTracksRequestBuilder
Get a builder for loved tracks requests
§Example
let tracks = client
.loved_tracks("username")
.limit(100)
.fetch()
.await?;Sourcepub fn top_tracks(&self, username: impl Into<String>) -> TopTracksRequestBuilder
pub fn top_tracks(&self, username: impl Into<String>) -> TopTracksRequestBuilder
Get a builder for top tracks requests
§Example
let tracks = client
.top_tracks("username")
.limit(100)
.fetch()
.await?;Sourcepub async fn user_exists(&self, username: impl Into<String>) -> Result<bool>
pub async fn user_exists(&self, username: impl Into<String>) -> Result<bool>
Check if a Last.fm user exists
§Arguments
username- The Last.fm username to check
§Returns
Ok(true)- User existsOk(false)- User does not existErr- Network error or other API error
§Example
if client.user_exists("rj").await? {
println!("User exists!");
} else {
println!("User not found");
}§Errors
Returns an error if the request fails due to network issues or other API errors
(not including “user not found” which returns Ok(false))