session

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: 5 Imported by: 0

Documentation

Overview

Package session provides functionality for managing authenticated sessions with the Last.fm API. It includes methods for setting session keys, verifying credentials, and making authenticated API requests.

The Session struct is the core of this package, encapsulating the API client and session key required for authenticated requests. It provides methods for sending GET and POST requests, as well as a general-purpose Request method for handling different HTTP methods.

The Client struct extends the Session functionality by embedding it and providing access to various API routes such as Album, Artist, User, and more. It serves as a central point for interacting with the Last.fm API.

Key Features:

  • Create and manage authenticated sessions with the Last.fm API.
  • Manage Last.fm user authentication and session keys.
  • Send authenticated HTTP GET and POST requests.
  • Access different API routes through the Client struct.

Usage:

  • Create a new Session or Client instance using the provided constructors.
  • Set the session key using SetSessionKey or through the Login method.
  • Use the Get, Post, or Request methods to interact with the Last.fm API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Album

type Album struct {
	*api.Album
	// contains filtered or unexported fields
}

func NewAlbum

func NewAlbum(session *Session) *Album

NewAlbum creates and returns a new Album API route.

func (Album) AddTags

func (a Album) AddTags(artist, album string, tags []string) error

AddTags adds tags to an album for the authenticated user.

func (Album) RemoveTag

func (a Album) RemoveTag(artist, album, tag string) error

RemoveTag removes a tag from an album for the authenticated user.

func (Album) SelfTags

func (a Album) SelfTags(params lastfm.AlbumSelfTagsParams) (*lastfm.AlbumTags, error)

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

func (Album) SelfTagsByMBID

func (a Album) SelfTagsByMBID(params lastfm.AlbumSelfTagsMBIDParams) (*lastfm.AlbumTags, error)

SelfTagsByMBID returns the tags of an album for the authenticated user by MBID.

type Artist

type Artist struct {
	*api.Artist
	// contains filtered or unexported fields
}

func NewArtist

func NewArtist(session *Session) *Artist

NewArtist creates and returns a new Artist API route.

func (Artist) AddTags

func (a Artist) AddTags(artist string, tags []string) error

AddTags adds tags to an artist for the authenticated user.

func (Artist) RemoveTag

func (a Artist) RemoveTag(artist string, tag string) error

RemoveTag removes a tag from an artist for the authenticated user.

func (Artist) SelfTags

func (a Artist) SelfTags(params lastfm.ArtistSelfTagsParams) (*lastfm.ArtistTags, error)

SelfTags returns the tags of an artist for the authenticated user by artist name.

func (Artist) SelfTagsByMBID

func (a Artist) SelfTagsByMBID(params lastfm.ArtistSelfTagsMBIDParams) (*lastfm.ArtistTags, error)

SelfTagsByMBID returns the tags of an artist for the authenticated user by MBID.

type Auth

type Auth struct {
	*api.Auth
}

func NewAuth

func NewAuth(session *Session) *Auth

NewAuth creates and returns a new Auth API route.

type Chart

type Chart struct {
	*api.Chart
}

func NewChart

func NewChart(session *Session) *Chart

NewChart creates and returns a new Chart API route.

type Client

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

Client is a struct that serves as a central point for making authenticated API calls. It embeds a Session and provides fields for interacting with different API routes such as Album, Artist, User, etc.

func NewClient

func NewClient(apiKey, secret string) *Client

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

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.

func (*Client) Login

func (c *Client) Login(username, password string) error

Login authenticates a user using their username and password credentials. Calls the AuthGetMobileSession method of the Last.fm API and sets the session key in the Client.

func (*Client) TokenLogin

func (c *Client) TokenLogin(token string) error

TokenLogin authenticates a user using an authorized token obtained from the AuthGetToken method of the Last.fm API. The user must have authorised the token via the URL returned by AuthTokenURL. The token is used to create a session key, which is then set in the Client. If the token cannot be used, an error is returned.

type Geo

type Geo struct {
	*api.Geo
}

func NewGeo

func NewGeo(session *Session) *Geo

NewGeo creates and returns a new Geo API route.

type Library

type Library struct {
	*api.Library
}

func NewLibrary

func NewLibrary(session *Session) *Library

NewLibrary creates and returns a new Library API route.

type Session

type Session struct {
	*api.API
	// SessionKey is the session key for the Last.fm API session. This key is
	// used to authenticate requests made to the API. Last.fm session keys have
	// infinite lifetime, so you can store it and reuse it for future requests
	// without needing to re-authenticate the user.
	SessionKey string
}

func New

func New(apiKey, secret string) *Session

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

func NewWithTimeout

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

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

func (*Session) CheckCredentials

func (s *Session) CheckCredentials(level api.RequestLevel) error

CheckCredentials verifies the authentication level required for an API request and ensures the necessary credentials are present. It checks the presence of the Last.fm API session key for requests requiring a session, the secret for requests requiring a 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 (*Session) Get

func (s *Session) Get(dest any, method api.APIMethod, params any) error

Get sends an authenticated 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 (*Session) Post

func (s *Session) Post(dest any, method api.APIMethod, params any) error

Post sends an authenticated HTTP POST request to the API with the specified method and parameters. The response is unmarshaled into the provided destination.

Parameters:

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

Returns:

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

func (*Session) Request

func (s *Session) Request(dest any, httpMethod string, method api.APIMethod, params any) error

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

Parameters:

  • dest: A pointer to the variable where the response will be unmarshaled.
  • httpMethod: The HTTP method to use for the request (e.g., "GET", "POST").
  • 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 unmarshaled.

func (*Session) SetSessionKey

func (s *Session) SetSessionKey(key string)

SetSessionKey sets the session key for the Last.fm API session. This key is used to authenticate requests made to the API. The session key is typically obtained after a user has logged in and authorized the application.

Use this method to set the session key manually if you have obtained it through other means, such as a login process or an authentication flow, or a stored session key from a previous session.

type Tag

type Tag struct {
	*api.Tag
}

func NewTag

func NewTag(session *Session) *Tag

NewTag creates and returns a new Tag API route.

type Track

type Track struct {
	*api.Track
	// contains filtered or unexported fields
}

func NewTrack

func NewTrack(session *Session) *Track

NewTrack creates and returns a new Track API route.

func (Track) AddTags

func (t Track) AddTags(artist, track string, tags []string) error

AddTags adds tags to a track for the authenticated user.

func (Track) Love

func (t Track) Love(artist, track string) error

Love marks a track as loved for the authenticated user.

func (Track) RemoveTag

func (t Track) RemoveTag(artist, track, tag string) error

RemoveTag removes a tag from a track for the authenticated user.

func (Track) Scrobble

func (t Track) Scrobble(params lastfm.ScrobbleParams) (*lastfm.ScrobbleResult, error)

Scrobble scrobbles a track for the authenticated user.

func (Track) ScrobbleMulti

func (t Track) ScrobbleMulti(
	params lastfm.ScrobbleMultiParams) (*lastfm.ScrobbleMultiResult, error)

ScrobbleMulti scrobbles multiple tracks for the authenticated user.

func (Track) SelfTags

func (t Track) SelfTags(params lastfm.TrackSelfTagsParams) (*lastfm.TrackTags, error)

SelfTags returns the tags of a track for the authenticated user by artist and track name.

func (Track) SelfTagsByMBID

func (t Track) SelfTagsByMBID(params lastfm.TrackSelfTagsMBIDParams) (*lastfm.TrackTags, error)

SelfTagsByMBID returns the tags of a track for the authenticated user by MBID.

func (Track) Unlove

func (t Track) Unlove(artist, track string) error

Unlove unmarks a track as loved for the authenticated user.

func (Track) UpdateNowPlaying

func (t Track) UpdateNowPlaying(
	params lastfm.UpdateNowPlayingParams) (*lastfm.NowPlayingUpdate, error)

UpdateNowPlaying updates the now playing track for the authenticated user.

type User

type User struct {
	*api.User
	// contains filtered or unexported fields
}

func NewUser

func NewUser(session *Session) *User

NewUser creates and returns a new User API route.

func (User) SelfInfo

func (u User) SelfInfo() (*lastfm.UserInfo, error)

Info returns the information the authenticated user.

Jump to

Keyboard shortcuts

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