api

package
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 14 Imported by: 1

Documentation

Overview

Package api provides a client for interacting with the Last.fm API. It includes methods for constructing API URLs, generating signatures, and making unauthenticated requests to various Last.fm API endpoints.

The package supports multiple levels of authorization, including API key, secret, and session-based authentication. It also provides utilities for parsing parameters, handling retries, and unmarshaling XML responses.

The main entry point for interacting with the API is the `Client` struct, which provides access to specific service modules such as Album, Artist, User, and more.

Key Features:

  • Build API URLs with query parameters.
  • Generate signatures for API requests using the API secret.
  • Make GET and POST requests to the API with automatic XML unmarshaling.

Usage:

  • Create a new Client with your API key and optionally, your secret.
  • Use the Client to access specific API methods
  • Handle responses and errors using the provided types and utilities.
  • Customize the client with user agent and timeout settings.
  • Use the `Request` method for general-purpose API requests.

For more information about the Last.fm API, visit: https://www.last.fm/api

Index

Constants

View Source
const (
	DefaultUserAgent      = "LastFM (https://github.com/twoscott/gobble-fm)"
	DefaultRetries   uint = 5
	DefaultTimeout        = 30
)
View Source
const (
	APIKeyMissingMessage   = "API Key is missing"
	SecretRequiredMessage  = "Method requires API secret"
	SessionRequiredMessage = "Method requires user authentication (session key)"
)

Custom error messages.

Variables

View Source
var (
	BaseEndpoint = "https://ws.audioscrobbler.com"
	Version      = "2.0"
	Endpoint     = BaseEndpoint + "/" + Version + "/"
)

Functions

func AuthURL

func AuthURL(params AuthURLParams) string

AuthURL returns the authentication URL for the Last.fm API with the specified parameters. This URL can be used to send users to Last.fm for authentication. The URL includes the API key and a callback URL if specified.

Parameters:

  • params.APIKey: The Last.fm API key.
  • params.Callback: The URL to redirect the user to and provided the authenticated token to as a query parameter.
  • params.Token: The token for the user to authenticate.

Returns:

  • The authentication URL as a string.

func BuildAPIURL

func BuildAPIURL(params url.Values) string

BuildAPIURL constructs a Last.fm API URL with the specified parameters.

func Signature

func Signature(params url.Values, secret string) string

Signature generates a Last.fm API signature for the given parameters and secret. The signature is created by concatenating the sorted parameter keys and their values, followed by the secret. The resulting string is then hashed using MD5 to produce a hexadecimal representation of the hash.

Parameters:

  • params: The parameters to include in the signature.
  • secret: The secret key to use for signing the request.

Returns:

  • A hexadecimal string representing the signature.

https://www.last.fm/api/authspec

Types

type API

type API struct {
	// APIKey is the Last.fm API key used to authenticate requests.
	APIKey string
	// Secret is the Last.fm API secret used to sign requests.
	Secret string
	// UserAgent is the user agent string sent with each request to the API.
	UserAgent string
	// Retries is the number of times to retry failed requests.
	Retries uint
	Client  HTTPClient
}

API represents the Last.fm API client. It provides methods for making requests to the Last.fm API and handling responses. The API client is initialized with an API key and can be configured with a user agent and timeout settings. It also supports retries for failed requests.

func New

func New(apiKey, secret string) *API

New returns a new instance of API with the given API key.

func NewKeyOnly added in v1.0.7

func NewKeyOnly(apiKey string) *API

NewKeyOnly returns a new instance of API with the given API key but without a Last.fm API secret. This is useful if you don't plan to use the API secret to sign requests to the API such as auth methods.

func NewWithTimeout

func NewWithTimeout(apiKey, secret string, timeout int) *API

NewWithTimeout returns a new instance of API with the given API key and timeout settings. The timeout is specified in seconds.

func (*API) AuthCallbackURL

func (a *API) AuthCallbackURL(callbackURL string) string

AuthCallbackURL returns the authorization URL for the Last.fm API with a callback URL. This URL can be used for web authentication to send users to Last.fm for authentication. The user will be redirected to the callback URL after and an authorized token query parameter will be appended to the URL.

https://www.last.fm/api/webauth

func (*API) AuthTokenURL

func (a *API) AuthTokenURL(token string) string

AuthTokenURL returns the authorization URL for the Last.fm API with a token. This URL can be used to send users to Last.fm for authentication where the provided token will be authorized.

https://www.last.fm/api/desktopauth

func (*API) AuthURL

func (a *API) AuthURL() string

AuthURL returns the authorization URL for the Last.fm API. This method should be used for web authentication if you set a callback URL when creating your API account. Otherwise, use AuthCallbackURL and provide a custom callback URL.

https://www.last.fm/api/webauth

func (*API) CheckCredentials

func (a *API) CheckCredentials(level RequestLevel) error

CheckCredentials verifies the authorization level required for an API request and ensures the necessary credentials are present. It checks the presence of the API secret for requests requiring a session or secret, and the API key for requests requiring an API key. Returns an error if required credentials are missing.

Parameters:

  • level: The RequestLevel indicating the level of authorization required.

Returns:

  • An error if the required authentication credentials are not present.

func (*API) Get

func (a *API) Get(dest any, method APIMethod, params any) error

Get sends an HTTP GET request to the API using the specified method and parameters, and decodes the response into the provided destination.

Parameters:

  • dest: A pointer to the variable where the response will be unmarshaled.
  • method: The APIMethod representing the endpoint to call.
  • params: The parameters to include in the request.

Returns:

  • An error if the request fails or the response cannot be decoded.

func (*API) GetSigned

func (a *API) GetSigned(dest any, method APIMethod, params any) error

GetSigned sends an HTTP GET request to the API with the specified method and parameters, signed with the API secret. The response is unmarshaled into the provided destination.

func (*API) GetURL

func (a *API) GetURL(dest any, url string) error

GetURL sends an HTTP GET request to the specified URL and unmarshals the response into the provided destination.

func (*API) Post

func (a *API) Post(dest any, method APIMethod, params any) error

Post sends an HTTP POST request to the API using the specified method and parameters, and decodes the response into the provided destination.

Parameters:

  • dest: A pointer to the variable where the response will be unmarshaled.
  • method: The APIMethod representing the endpoint to call.
  • params: The parameters to include in the request.

Returns:

  • An error if the request fails or the response cannot be decoded.

func (*API) PostBody

func (a *API) PostBody(dest any, url, body string) error

PostBody sends an HTTP POST request to the specified URL with the given request body and unmarshals the response into the provided destination.

func (*API) PostSigned

func (a *API) PostSigned(dest any, method APIMethod, params any) error

PostSigned sends an HTTP POST request to the API with the specified method and parameters, signed with the API secret. The response is unmarshaled into the provided destination.

func (*API) Request

func (a *API) Request(dest any, httpMethod string, method APIMethod, params any) error

Request sends an HTTP request to the API with the specified parameters and unmarshals the response into the provided destination.

Parameters:

  • dest: A pointer to the variable where the unmarshaled XML response will be stored.
  • httpMethod: The HTTP method to use for the request (e.g., "GET", "POST").
  • method: The API method to call, represented as an APIMethod type.
  • params: The parameters to include in the API request, typically a struct that can be serialized into query parameters.

Returns:

  • An error if the request fails, the response cannot be unmarshaled, or any other issue occurs.

func (*API) RequestSigned

func (a *API) RequestSigned(dest any, httpMethod string, method APIMethod, params any) error

RequestSigned sends an HTTP request to the API with the specified method and parameters, signed with the API secret. The response is unmarshaled into the provided destination.

Parameters:

  • dest: A pointer to the variable where the unmarshaled response will be stored.
  • httpMethod: The HTTP method to use for the request (e.g., "GET", "POST").
  • method: The API method to call, represented as an APIMethod type.
  • params: The parameters to include in the API request, typically a struct that can be serialized into query parameters.

Returns:

  • An error if the request fails, the response cannot be unmarshaled, or any other issue occurs.

func (*API) SetRetries

func (a *API) SetRetries(retries uint)

SetRetries sets the number of retries for failed requests.

func (*API) SetUserAgent

func (a *API) SetUserAgent(userAgent string)

SetUserAgent sets the user agent for the API client.

func (*API) Signature

func (a *API) Signature(params url.Values) string

Signature generates a signature for the given parameters using the API secret. The signature is created by concatenating the sorted parameter keys and their values, followed by the session secret. The resulting string is then hashed using MD5 to produce a hexadecimal representation of the hash.

type APIMethod

type APIMethod string

APIMethod represents a Last.fm API method parameter.

const (
	AlbumAddTagsMethod    APIMethod = "album.addTags"
	AlbumGetInfoMethod    APIMethod = "album.getInfo"
	AlbumGetTagsMethod    APIMethod = "album.getTags"
	AlbumGetTopTagsMethod APIMethod = "album.getTopTags"
	AlbumRemoveTagMethod  APIMethod = "album.removeTag"
	AlbumSearchMethod     APIMethod = "album.search"

	ArtistAddTagsMethod       APIMethod = "artist.addTags"
	ArtistGetCorrectionMethod APIMethod = "artist.getCorrection"
	ArtistGetInfoMethod       APIMethod = "artist.getInfo"
	ArtistGetSimilarMethod    APIMethod = "artist.getSimilar"
	ArtistGetTagsMethod       APIMethod = "artist.getTags"
	ArtistGetTopAlbumsMethod  APIMethod = "artist.getTopAlbums"
	ArtistGetTopTagsMethod    APIMethod = "artist.getTopTags"
	ArtistGetTopTracksMethod  APIMethod = "artist.getTopTracks"
	ArtistRemoveTagMethod     APIMethod = "artist.removeTag"
	ArtistSearchMethod        APIMethod = "artist.search"

	AuthGetMobileSessionMethod APIMethod = "auth.getMobileSession"
	AuthGetSessionMethod       APIMethod = "auth.getSession"
	AuthGetTokenMethod         APIMethod = "auth.getToken"

	ChartGetTopArtistsMethod APIMethod = "chart.getTopArtists"
	ChartGetTopTagsMethod    APIMethod = "chart.getTopTags"
	ChartGetTopTracksMethod  APIMethod = "chart.getTopTracks"

	GeoGetTopArtistsMethod APIMethod = "geo.getTopArtists"
	GeoGetTopTracksMethod  APIMethod = "geo.getTopTracks"

	LibraryGetArtistsMethod APIMethod = "library.getArtists"

	TagGetInfoMethod            APIMethod = "tag.getInfo"
	TagGetSimilarMethod         APIMethod = "tag.getSimilar"
	TagGetTopAlbumsMethod       APIMethod = "tag.getTopAlbums"
	TagGetTopArtistsMethod      APIMethod = "tag.getTopArtists"
	TagGetTopTagsMethod         APIMethod = "tag.getTopTags"
	TagGetTopTracksMethod       APIMethod = "tag.getTopTracks"
	TagGetWeeklyChartListMethod APIMethod = "tag.getWeeklyChartList"

	TrackAddTagsMethod          APIMethod = "track.addTags"
	TrackGetCorrectionMethod    APIMethod = "track.getCorrection"
	TrackGetInfoMethod          APIMethod = "track.getInfo"
	TrackGetSimilarMethod       APIMethod = "track.getSimilar"
	TrackGetTagsMethod          APIMethod = "track.getTags"
	TrackGetTopTagsMethod       APIMethod = "track.getTopTags"
	TrackLoveMethod             APIMethod = "track.love"
	TrackRemoveTagMethod        APIMethod = "track.removeTag"
	TrackScrobbleMethod         APIMethod = "track.scrobble"
	TrackSearchMethod           APIMethod = "track.search"
	TrackUnloveMethod           APIMethod = "track.unlove"
	TrackUpdateNowPlayingMethod APIMethod = "track.updateNowPlaying"

	UserGetFriendsMethod           APIMethod = "user.getFriends"
	UserGetInfoMethod              APIMethod = "user.getInfo"
	UserGetLovedTracksMethod       APIMethod = "user.getLovedTracks"
	UserGetPersonalTagsMethod      APIMethod = "user.getPersonalTags"
	UserGetRecentTracksMethod      APIMethod = "user.getRecentTracks"
	UserGetTopAlbumsMethod         APIMethod = "user.getTopAlbums"
	UserGetTopArtistsMethod        APIMethod = "user.getTopArtists"
	UserGetTopTagsMethod           APIMethod = "user.getTopTags"
	UserGetTopTracksMethod         APIMethod = "user.getTopTracks"
	UserGetWeeklyAlbumChartMethod  APIMethod = "user.getWeeklyAlbumChart"
	UserGetWeeklyArtistChartMethod APIMethod = "user.getWeeklyArtistChart"
	UserGetWeeklyChartListMethod   APIMethod = "user.getWeeklyChartList"
	UserGetWeeklyTrackChartMethod  APIMethod = "user.getWeeklyTrackChart"
)

https://www.last.fm/api

func (APIMethod) String

func (m APIMethod) String() string

String returns the string representation of the APIMethod.

type Album

type Album struct {
	// contains filtered or unexported fields
}

func NewAlbum

func NewAlbum(api *API) *Album

NewAlbum creates and returns a new Album API route.

func (Album) Info

func (a Album) Info(params lastfm.AlbumInfoParams) (*lastfm.AlbumInfo, error)

Info returns the information of an album by artist and album name.

func (Album) InfoByMBID

func (a Album) InfoByMBID(params lastfm.AlbumInfoMBIDParams) (*lastfm.AlbumInfo, error)

InfoByMBID returns the information of an album by MBID.

func (Album) Search

Search returns the results of an album search.

func (Album) TopTags

func (a Album) TopTags(params lastfm.AlbumTopTagsParams) (*lastfm.AlbumTopTags, error)

TopTags returns the top tags of an album by artist and album name.

func (Album) TopTagsByMBID deprecated

func (a Album) TopTagsByMBID(params lastfm.AlbumTopTagsMBIDParams) (*lastfm.AlbumTopTags, error)

TopTagsByMBID returns the top tags of an album by MBID.

Deprecated: Fetching top tags by MBID doesn't seem to work. Use TopTags instead.

func (Album) UserInfo added in v1.0.4

func (a Album) UserInfo(params lastfm.AlbumUserInfoParams) (*lastfm.AlbumUserInfo, error)

UserInfo returns the information of an album for user by artist and album name.

func (Album) UserInfoByMBID added in v1.0.4

func (a Album) UserInfoByMBID(
	params lastfm.AlbumUserInfoMBIDParams) (*lastfm.AlbumUserInfo, error)

UserInfoByMBID returns the information of an album for user by MBID.

func (Album) UserTags

func (a Album) UserTags(params lastfm.AlbumTagsParams) (*lastfm.AlbumTags, error)

UserTags returns the tags of an album for user by artist and album name.

func (Album) UserTagsByMBID

func (a Album) UserTagsByMBID(params lastfm.AlbumTagsMBIDParams) (*lastfm.AlbumTags, error)

UserTagsByMBID returns the tags of an album for user by MBID.

type Artist

type Artist struct {
	// contains filtered or unexported fields
}

func NewArtist

func NewArtist(api *API) *Artist

NewArtist creates and returns a new Artist API route.

func (Artist) Correction

func (a Artist) Correction(artist string) (*lastfm.ArtistCorrection, error)

Correction returns the artist name corrections of an artist.

func (Artist) Info

func (a Artist) Info(params lastfm.ArtistInfoParams) (*lastfm.ArtistInfo, error)

Info returns the information of an artist by artist name.

func (Artist) InfoByMBID

func (a Artist) InfoByMBID(params lastfm.ArtistInfoMBIDParams) (*lastfm.ArtistInfo, error)

InfoByMBID returns the information of an artist by MBID.

func (Artist) Search

Search returns the results of an album search.

func (Artist) Similar

Similar returns the similar artists of an artist by artist name.

func (Artist) SimilarByMBID

func (a Artist) SimilarByMBID(
	params lastfm.ArtistSimilarMBIDParams) (*lastfm.SimilarArtists, error)

SimilarByMBID returns the similar artists of an artist by MBID.

func (Artist) TopAlbums

TopAlbums returns the top albums of an artist by artist name.

func (Artist) TopAlbumsByMBID

func (a Artist) TopAlbumsByMBID(
	params lastfm.ArtistTopAlbumsMBIDParams) (*lastfm.ArtistTopAlbums, error)

TopAlbumsByMBID returns the top albums of an artist by MBID.

func (Artist) TopTags

TopTracks returns the top tracks of an artist by artist name.

func (Artist) TopTagsByMBID

func (a Artist) TopTagsByMBID(
	params lastfm.ArtistTopTagsMBIDParams) (*lastfm.ArtistTopTags, error)

TopTagsByMBID returns the top tracks of an artist by MBID.

func (Artist) TopTracks

TopTracks returns the top tracks of an artist by artist name.

func (Artist) TopTracksByMBID

func (a Artist) TopTracksByMBID(
	params lastfm.ArtistTopTracksMBIDParams) (*lastfm.ArtistTopTracks, error)

TopTracksByMBID returns the top tracks of an artist by MBID.

func (Artist) UserInfo added in v1.0.4

UserInfo returns the information of an artist for user by artist name.

func (Artist) UserInfoByMBID added in v1.0.4

func (a Artist) UserInfoByMBID(
	params lastfm.ArtistUserInfoMBIDParams) (*lastfm.ArtistUserInfo, error)

UserInfoByMBID returns the information of an artist for user by MBID.

func (Artist) UserTags

func (a Artist) UserTags(params lastfm.ArtistTagsParams) (*lastfm.ArtistTags, error)

UserTags returns the tags of an artist for user by artist name.

func (Artist) UserTagsByMBID

func (a Artist) UserTagsByMBID(params lastfm.ArtistTagsMBIDParams) (*lastfm.ArtistTags, error)

UserTagsByMBID returns the tags of an artist for user by MBID.

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

func NewAuth

func NewAuth(api *API) *Auth

NewAuth creates and returns a new Auth API route.

func (Auth) MobileSession

func (a Auth) MobileSession(username, password string) (*lastfm.Session, error)

MobileSession returns a session for the given mobile user credentials. This session can be used to authenticate requests to the Last.fm API. The session is obtained by calling the AuthGetMobileSession method of the Last.fm API.

func (Auth) Session

func (a Auth) Session(token string) (*lastfm.Session, error)

Session returns a session for the given token. This session can be used to authenticate requests to the Last.fm API. The session is obtained by calling the AuthGetSession method of the Last.fm API.

func (Auth) Token

func (a Auth) Token() (string, error)

Token returns a session token for the Last.fm API. This token can be used to authenticate requests to the API. The token is obtained by calling the AuthGetToken method of the Last.fm API. The token is cached in the session, so subsequent calls to this method will return the same cached token.

type AuthURLParams

type AuthURLParams struct {
	APIKey   string `url:"api_key"`
	Callback string `url:"cb,omitempty"`
	Token    string `url:"token,omitempty"`
}

type Chart

type Chart struct {
	// contains filtered or unexported fields
}

func NewChart

func NewChart(api *API) *Chart

NewChart creates and returns a new Chart API route.

func (Chart) TopArtists

func (c Chart) TopArtists() (*lastfm.ChartTopArtists, error)

TopArtists returns all the top artists of the chart. Same as TopArtistsLimit(nil).

func (Chart) TopArtistsLimit

func (c Chart) TopArtistsLimit(params *lastfm.ChartTopArtistsParams) (*lastfm.ChartTopArtists, error)

TopArtistsLimit returns the top artists of the chart.

func (Chart) TopTags

func (c Chart) TopTags() (*lastfm.ChartTopTags, error)

TopTags returns the top tags of the chart. Same as TopTagsLimit(nil).

func (Chart) TopTagsLimit

func (c Chart) TopTagsLimit(params *lastfm.ChartTopTagsParams) (*lastfm.ChartTopTags, error)

TopTagsLimit returns the top tags of the chart.

func (Chart) TopTracks

func (c Chart) TopTracks() (*lastfm.ChartTopTracks, error)

TopTracks returns all the top tracks of the chart. Same as TopTracksLimit(nil).

func (Chart) TopTracksLimit

func (c Chart) TopTracksLimit(params *lastfm.ChartTopTracksParams) (*lastfm.ChartTopTracks, error)

TopTracksLimit returns the top tracks of the chart.

type Client

type Client struct {
	*API
	Album   *Album
	Artist  *Artist
	Auth    *Auth
	Chart   *Chart
	Geo     *Geo
	Library *Library
	Tag     *Tag
	Track   *Track
	User    *User
}

Client is the main struct that provides access to various API services. It embeds the API struct and includes fields for accessing specific service modules such as Album, Artist, User, etc.

func NewClient

func NewClient(apiKey, secret string) *Client

New returns a new instance of API Client with the given API key and secret.

func NewClientKeyOnly

func NewClientKeyOnly(apiKey string) *Client

NewClientKeyOnly returns a new instance of Client with the given API key but without a Last.fm API secret. This is useful if you don't plan to use the API secret to sign requests to the API such as auth methods.

func NewClientWithTimeout

func NewClientWithTimeout(apiKey, secret string, timeout int) *Client

NewClientWithTimeout returns a new instance of Client with the given API key, secret, and timeout settings. The timeout is specified in seconds and is used to configure the HTTP client for making API requests. This allows for better control over network timeouts when interacting with the API.

type ErrorCode

type ErrorCode int
const (
	NoError ErrorCode = iota // 0 (No error)

	ErrInvalidService         // 2
	ErrInvalidMethod          // 3
	ErrAuthenticationFailed   // 4
	ErrInvalidFormat          // 5
	ErrInvalidParameters      // 6
	ErrInvalidResource        // 7
	ErrOperationFailed        // 8
	ErrInvalidSessionKey      // 9
	ErrInvalidAPIKey          // 10
	ErrServiceOffline         // 11
	ErrSubscribersOnly        // 12
	ErrInvalidMethodSignature // 13
	ErrUnauthorizedToken      // 14
	ErrItemNotStreamable      // 15
	ErrServiceUnavailable     // 16
	ErrUserNotLoggedIn        // 17
	ErrTrialExpired           // 18

	ErrNotEnoughContent    // 20
	ErrNotEnoughMembers    // 21
	ErrNotEnoughFans       // 22
	ErrNotEnoughNeighbours // 23
	ErrNoPeakRadio         // 24
	ErrRadioNotFound       // 25
	ErrAPIKeySuspended     // 26
	ErrDeprecated          // 27

	ErrRateLimitExceeded // 29
)

ErrorCode represents an error code returned by the Last.fm API.

const (
	ErrAPIKeyMissing ErrorCode = iota + 100
	ErrSecretRequired
	ErrSessionRequired
)

Custom error codes.

type Geo

type Geo struct {
	// contains filtered or unexported fields
}

func NewGeo

func NewGeo(api *API) *Geo

NewGeo creates and returns a new Geo API route.

func (Geo) TopArtists

func (g Geo) TopArtists(params lastfm.GeoTopArtistsParams) (*lastfm.GeoTopArtists, error)

TopArtists returns the top artists of a country.

func (Geo) TopTracks

func (g Geo) TopTracks(params lastfm.GeoTopTracksParams) (*lastfm.GeoTopTracks, error)

TopTracks returns the top tracks of a country.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface that defines the Do method for making HTTP requests. This allows for easier testing and mocking of HTTP requests.

type HTTPError

type HTTPError struct {
	StatusCode int
	Message    string
}

HTTPError represents an error that occurred during an HTTP request.

func NewHTTPError

func NewHTTPError(res *http.Response) *HTTPError

NewHTTPError creates a new HTTPError instance from an HTTP response.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error implements the error interface.

func (*HTTPError) Is

func (e *HTTPError) Is(target error) bool

Is checks if the error matches the target error.

type LFMWrapper

type LFMWrapper struct {
	XMLName  xml.Name `xml:"lfm"`
	Status   string   `xml:"status,attr"`
	InnerXML []byte   `xml:",innerxml"`
}

func (*LFMWrapper) Empty

func (lf *LFMWrapper) Empty() bool

Empty checks if the InnerXML is empty.

func (*LFMWrapper) StatusFailed

func (lf *LFMWrapper) StatusFailed() bool

StatusFailed checks if the status is "failed".

func (*LFMWrapper) StatusOK

func (lf *LFMWrapper) StatusOK() bool

StatusOK checks if the status is "ok".

func (*LFMWrapper) UnmarshalInnerXML

func (lf *LFMWrapper) UnmarshalInnerXML(dest any) error

UnmarshalInnerXML unmarshals the InnerXML field into the provided destination variable.

func (*LFMWrapper) UnwrapError

func (lf *LFMWrapper) UnwrapError() (*LastFMError, error)

UnwrapError attempts to extract and return a LastFMError from the LFMWrapper instance. If the status of the LFMWrapper indicates success, it returns nil for both the error and LastFMError. Otherwise, it unmarshals the InnerXML field into a LastFMError structure. If the unmarshaling succeeds and the LastFMError contains an error code, it returns the LastFMError. If no error code is present or unmarshaling fails, it returns an appropriate error.

type LastFMError

type LastFMError struct {
	Code    ErrorCode `xml:"code,attr"`
	Message string    `xml:",chardata"`
	// contains filtered or unexported fields
}

LastFMError represents an error returned by Last.fm.

func NewLastFMError

func NewLastFMError(code ErrorCode, message string) *LastFMError

NewLastFMError creates a new LastFMError instance from an error code and message.

func (*LastFMError) Error

func (e *LastFMError) Error() string

Error implements the error interface.

func (*LastFMError) HasErrorCode

func (e *LastFMError) HasErrorCode() bool

HasErrorCode checks if the error code indicated an error (not 0).

func (*LastFMError) Is

func (e *LastFMError) Is(target error) bool

Is checks if the error matches the target error.

func (*LastFMError) IsCode

func (e *LastFMError) IsCode(code ErrorCode) bool

IsCode checks if the error code matches the given code.

func (*LastFMError) ShouldRetry added in v1.0.3

func (e *LastFMError) ShouldRetry() bool

ShouldRetry returns true if the error code indicates that the request can be retried.

func (*LastFMError) Unwrap

func (e *LastFMError) Unwrap() error

Unwrap implements the error interface to return the underlying HTTP error.

func (*LastFMError) WrapHTTPError

func (e *LastFMError) WrapHTTPError(httpError *HTTPError) *LastFMError

WrapHTTPError wraps an HTTP error into the LastFMError.

func (*LastFMError) WrapResponse

func (e *LastFMError) WrapResponse(res *http.Response) *LastFMError

WrapNewHTTPError wraps a new HTTP error from a HTTP response into the LastFMError.

type Library

type Library struct {
	// contains filtered or unexported fields
}

func NewLibrary

func NewLibrary(api *API) *Library

NewLibrary creates and returns a new Library API route.

func (Library) Artists

type RequestLevel

type RequestLevel int

RequestLevel specifies the level of authorisation and authentication required for an API request.

const (
	RequestLevelNone RequestLevel = iota
	RequestLevelAPIKey
	RequestLevelSecret
	RequestLevelSession
)

type Tag

type Tag struct {
	// contains filtered or unexported fields
}

func NewTag

func NewTag(api *API) *Tag

NewTag creates and returns a new Tag API route.

func (Tag) Info

func (t Tag) Info(params lastfm.TagInfoParams) (*lastfm.TagInfo, error)

Info returns the information of a tag.

func (Tag) Similar

func (t Tag) Similar(tag string) (*lastfm.SimilarTags, error)

Similar returns tags similar to the given tag.

func (Tag) TopAlbums

func (t Tag) TopAlbums(params lastfm.TagTopAlbumsParams) (*lastfm.TagTopAlbums, error)

TopAlbums returns the top albums tagged with the given tag.

func (Tag) TopArtists

func (t Tag) TopArtists(params lastfm.TagTopArtistsParams) (*lastfm.TagTopArtists, error)

TopArtists returns the top artists tagged with the given tag.

func (Tag) TopTags

func (t Tag) TopTags() (*lastfm.TagTopTags, error)

TopTags returns the top tags on Last.fm.

func (Tag) TopTagsLimit

func (t Tag) TopTagsLimit(params *lastfm.TagTopTagsParams) (*lastfm.TagTopTags, error)

TopTagsLimit returns the top tags on Last.fm, with optional limit and offset.

func (Tag) TopTracks

func (t Tag) TopTracks(params lastfm.TagTopTracksParams) (*lastfm.TagTopTracks, error)

TopTracks returns the top tracks tagged with the given tag.

func (Tag) WeeklyChartList

func (t Tag) WeeklyChartList(tag string) (*lastfm.TagWeeklyChartList, error)

WeeklyChartList returns the weekly chart list of a tag.

type Track

type Track struct {
	// contains filtered or unexported fields
}

func NewTrack

func NewTrack(api *API) *Track

NewTrack creates and returns a new Track API route.

func (Track) Correction

func (t Track) Correction(artist, track string) (*lastfm.TrackCorrection, error)

Correction returns the track and artist name corrections of a track.

func (Track) Info

func (t Track) Info(params lastfm.TrackInfoParams) (*lastfm.TrackInfo, error)

Info returns the information of a track by artist and track name.

func (Track) InfoByMBID

func (t Track) InfoByMBID(params lastfm.TrackInfoMBIDParams) (*lastfm.TrackInfo, error)

InfoByMBID returns the information of a track by MBID.

func (Track) Search

Search searches for tracks by track name, and optionally artist name.

func (Track) Similar

func (t Track) Similar(params lastfm.TrackSimilarParams) (*lastfm.SimilarTracks, error)

Similar returns the similar tracks of a track by artist and track name.

func (Track) SimilarByMBID

func (t Track) SimilarByMBID(params lastfm.TrackSimilarMBIDParams) (*lastfm.SimilarTracks, error)

SimilarByMBID returns the similar tracks of a track by MBID.

func (Track) Tags

func (t Track) Tags(params lastfm.TrackTagsParams) (*lastfm.TrackTags, error)

Tags returns the tags of a track by artist and track name.

func (Track) TagsByMBID

func (t Track) TagsByMBID(params lastfm.TrackTagsMBIDParams) (*lastfm.TrackTags, error)

TagsByMBID returns the tags of a track by MBID.

func (Track) TopTags

func (t Track) TopTags(params lastfm.TrackTopTagsParams) (*lastfm.TrackTopTags, error)

TopTags returns the top tags of a track by artist and track name.

func (Track) TopTagsByMBID

func (t Track) TopTagsByMBID(params lastfm.TrackTopTagsMBIDParams) (*lastfm.TrackTopTags, error)

TopTagsByMBID returns the top tags of a track by MBID.

func (Track) UserInfo added in v1.0.4

func (t Track) UserInfo(params lastfm.TrackUserInfoParams) (*lastfm.TrackUserInfo, error)

UserInfo returns the information of a track for user by artist and track name.

func (Track) UserInfoByMBID added in v1.0.4

func (t Track) UserInfoByMBID(
	params lastfm.TrackUserInfoMBIDParams) (*lastfm.TrackUserInfo, error)

UserInfoByMBID returns the information of a track for user by MBID.

type User

type User struct {
	// contains filtered or unexported fields
}

func NewUser

func NewUser(api *API) *User

NewUser creates and returns a new User API route.

func (User) Friends

func (u User) Friends(params lastfm.FriendsParams) (*lastfm.Friends, error)

Friends returns the friends of a user.

func (User) Info

func (u User) Info(user string) (*lastfm.UserInfo, error)

Info returns the information of a user.

func (User) LovedTracks

func (u User) LovedTracks(params lastfm.LovedTracksParams) (*lastfm.LovedTracks, error)

LovedTracks returns the loved tracks of a user.

func (User) RecentTrack added in v1.0.6

func (u User) RecentTrack(user string) (*lastfm.RecentTrack, error)

RecentTrack returns the most recent track of a user. This is a convenience method that calls RecentTracks with limit=1.

func (User) RecentTrackExtended added in v1.0.6

func (u User) RecentTrackExtended(user string) (*lastfm.RecentTrackExtended, error)

RecentTrackExtended returns the most recent track of a user with extended information. This is a convenience method that calls RecentTracksExtended with limit=1.

func (User) RecentTracks

func (u User) RecentTracks(params lastfm.RecentTracksParams) (*lastfm.RecentTracks, error)

RecentTracks returns the recent tracks of a user.

func (User) RecentTracksExtended

func (u User) RecentTracksExtended(
	params lastfm.RecentTracksParams) (*lastfm.RecentTracksExtended, error)

RecentTracksExtended returns the recent tracks of a user with extended information.

func (User) TaggedAlbums added in v1.0.5

func (u User) TaggedAlbums(params lastfm.UserTagsParams) (*lastfm.UserAlbumTags, error)

TaggedAlbums returns the albums tagged by a user with the given tag.

func (User) TaggedArtists added in v1.0.5

func (u User) TaggedArtists(params lastfm.UserTagsParams) (*lastfm.UserArtistTags, error)

TaggedArtists returns the artists tagged by a user with the given tag.

func (User) TaggedTracks added in v1.0.5

func (u User) TaggedTracks(params lastfm.UserTagsParams) (*lastfm.UserTrackTags, error)

TaggedTracks returns the tracks tagged by a user with the given tag.

func (User) TopAlbums

func (u User) TopAlbums(params lastfm.UserTopAlbumsParams) (*lastfm.UserTopAlbums, error)

TopAlbums returns the top albums of a user.

func (User) TopArtists

func (u User) TopArtists(params lastfm.UserTopArtistsParams) (*lastfm.UserTopArtists, error)

TopArtists returns the top artists of a user.

func (User) TopTags

func (u User) TopTags(params lastfm.UserTopTagsParams) (*lastfm.UserTopTags, error)

TopTags returns the top tags of a user.

func (User) TopTracks

func (u User) TopTracks(params lastfm.UserTopTracksParams) (*lastfm.UserTopTracks, error)

TopTracks returns the top tracks of a user.

func (User) WeeklyAlbumChart

func (u User) WeeklyAlbumChart(
	params lastfm.WeeklyAlbumChartParams) (*lastfm.WeeklyAlbumChart, error)

WeeklyAlbumChart returns the weekly album chart of a user.

func (User) WeeklyArtistChart

func (u User) WeeklyArtistChart(
	params lastfm.WeeklyArtistChartParams) (*lastfm.WeeklyArtistChart, error)

WeeklyArtistChart returns the weekly artist chart of a user.

func (User) WeeklyChartList

func (u User) WeeklyChartList(user string) (*lastfm.WeeklyChartList, error)

WeeklyChartList returns the weekly chart list of a user.

func (User) WeeklyTrackChart

func (u User) WeeklyTrackChart(
	params lastfm.WeeklyTrackChartParams) (*lastfm.WeeklyTrackChart, error)

WeeklyTrackChart returns the weekly track chart of a user.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL