Skip to main content

LastFmClient

Struct LastFmClient 

Source
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 data

Implementations§

Source§

impl LastFmClient

Source

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()?;
Source

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

Source

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.

Source

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));
Source

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?;
Source

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?;
Source

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?;
Source

pub fn top_artists( &self, username: impl Into<String>, ) -> TopArtistsRequestBuilder

Get a builder for top artists requests

§Example
let artists = client
    .top_artists("username")
    .limit(100)
    .fetch()
    .await?;
Source

pub fn top_albums(&self, username: impl Into<String>) -> TopAlbumsRequestBuilder

Get a builder for top albums requests

§Example
let albums = client
    .top_albums("username")
    .limit(100)
    .fetch()
    .await?;
Source

pub fn top_tags(&self, username: impl Into<String>) -> TopTagsRequestBuilder

Get a builder for top tags requests

§Example
let tags = client
    .top_tags("username")
    .limit(20)
    .fetch()
    .await?;
Source

pub fn weekly_chart_list( &self, username: impl Into<String>, ) -> WeeklyChartListRequestBuilder

Get a builder for user.getWeeklyChartList requests

§Example
let ranges = client.weekly_chart_list("username").fetch().await?;
Source

pub fn weekly_track_chart( &self, username: impl Into<String>, ) -> WeeklyTrackChartRequestBuilder

Get a builder for user.getWeeklyTrackChart requests

§Example
let ranges = client.weekly_chart_list("username").fetch().await?;
if let Some(range) = ranges.first() {
    let tracks = client.weekly_track_chart("username").range(range).fetch().await?;
}
Source

pub fn weekly_artist_chart( &self, username: impl Into<String>, ) -> WeeklyArtistChartRequestBuilder

Get a builder for user.getWeeklyArtistChart requests

Source

pub fn weekly_album_chart( &self, username: impl Into<String>, ) -> WeeklyAlbumChartRequestBuilder

Get a builder for user.getWeeklyAlbumChart requests

Source

pub fn friends(&self, username: impl Into<String>) -> FriendsRequestBuilder

Get a builder for friends requests

§Example
let friends = client.friends("rj").fetch_all().await?;
for friend in &friends {
    println!("{}", friend.name);
}
Source

pub fn personal_tags( &self, username: impl Into<String>, tag: impl Into<String>, ) -> PersonalTagsRequestBuilder

Get a builder for personal tags requests

Returns tracks, artists, or albums that the user has tagged with the given tag.

§Example
let page = client.personal_tags("rj", "rock").fetch_tracks().await?;
for track in &page.tracks {
    println!("{} - {}", track.artist_name, track.name);
}
Source

pub fn user_info(&self, username: impl Into<String>) -> UserInfoRequestBuilder

Get a builder for user profile requests

§Example
let info = client.user_info("rj").fetch().await?;
println!("{} has {} scrobbles", info.name, info.play_count);
Source

pub async fn user_exists(&self, username: impl Into<String>) -> Result<bool>

Check if a Last.fm user exists

§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))

Source

pub fn config(&self) -> &Config

Get a reference to the configuration

Trait Implementations§

Source§

impl Debug for LastFmClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more