Documentation
¶
Overview ¶
Package webspotify implements Spotify's documented OAuth and Web API flows without depending on the desktop player's private services.
Index ¶
- Constants
- Variables
- func MapSDKError(kind, message string) model.Event
- func MapSDKState(state SDKState) []model.Event
- func PKCEChallenge(verifier string) string
- type API
- func (c *API) AddToQueue(ctx context.Context, deviceID, uri string) error
- func (c *API) Album(ctx context.Context, uri string) (catalog.AlbumDetails, error)
- func (c *API) Artist(ctx context.Context, uri string) (catalog.ArtistOverview, error)
- func (c *API) ContextTracks(ctx context.Context, uri string) (model.Listing, error)
- func (c *API) Home(ctx context.Context) (catalog.HomeFeed, error)
- func (c *API) Liked(ctx context.Context, uris []string) ([]bool, error)
- func (c *API) LikedTracks(ctx context.Context, offset, limit int) (catalog.SavedTracksPage, error)
- func (c *API) Next(ctx context.Context, deviceID string) error
- func (c *API) Pause(ctx context.Context, deviceID string) error
- func (c *API) PlaybackState(ctx context.Context) (model.Status, string, error)
- func (c *API) Playlists(ctx context.Context) ([]model.Playlist, error)
- func (c *API) Previous(ctx context.Context, deviceID string) error
- func (c *API) Profile(ctx context.Context) (id, displayName string, err error)
- func (c *API) RecentlyPlayed(ctx context.Context, limit int) ([]model.Played, error)
- func (c *API) Resume(ctx context.Context, deviceID string) error
- func (c *API) Search(ctx context.Context, query string, limit int) (catalog.Results, error)
- func (c *API) Seek(ctx context.Context, deviceID string, position time.Duration) error
- func (c *API) SetLiked(ctx context.Context, uris []string, liked bool) error
- func (c *API) SetRepeat(ctx context.Context, deviceID string, repeat model.Repeat) error
- func (c *API) SetShuffle(ctx context.Context, deviceID string, enabled bool) error
- func (c *API) SetVolume(ctx context.Context, deviceID string, percent int) error
- func (c *API) StartPlayback(ctx context.Context, deviceID, uri, skipTo string, likedTracks []string) error
- func (c *API) TransferPlayback(ctx context.Context, deviceID string, play bool) error
- type Auth
- type Client
- func (c *Client) Album(ctx context.Context, uri string) (catalog.AlbumDetails, error)
- func (c *Client) Artist(ctx context.Context, uri string) (catalog.ArtistOverview, error)
- func (c *Client) Configure(context.Context, model.Audio) error
- func (c *Client) ContextTracks(ctx context.Context, uri string) (model.Listing, error)
- func (c *Client) Events() <-chan model.Event
- func (c *Client) Home(ctx context.Context) (catalog.HomeFeed, error)
- func (c *Client) Liked(ctx context.Context, uris []string) ([]bool, error)
- func (c *Client) LikedTracks(ctx context.Context, offset, limit int) (catalog.SavedTracksPage, error)
- func (c *Client) Next(ctx context.Context) error
- func (c *Client) Play(ctx context.Context, uri, skipTo string) error
- func (c *Client) PlayPause(ctx context.Context) error
- func (c *Client) Playlists(ctx context.Context) ([]model.Playlist, error)
- func (c *Client) Prev(ctx context.Context) error
- func (c *Client) Queue(ctx context.Context, uri string) error
- func (c *Client) RecentlyPlayed(ctx context.Context, _ string, limit int) ([]model.Played, error)
- func (c *Client) Search(ctx context.Context, query string, limit int) (catalog.Results, error)
- func (c *Client) Seek(ctx context.Context, position time.Duration) error
- func (c *Client) SetDeviceName(ctx context.Context, name string) error
- func (c *Client) SetLiked(ctx context.Context, uris []string, liked bool) error
- func (c *Client) SetRepeat(ctx context.Context, repeat model.Repeat) error
- func (c *Client) SetShuffle(ctx context.Context, enabled bool) error
- func (c *Client) SetVolume(ctx context.Context, volume int) error
- func (c *Client) SignOut(context.Context) error
- func (c *Client) Start(ctx context.Context) error
- func (c *Client) Status(ctx context.Context) (model.Status, error)
- func (c *Client) SupportsRadio() bool
- func (c *Client) SupportsSignOut() bool
- type FetchTransport
- type HTTPError
- type HTTPTransport
- type PendingAuth
- type Request
- type Response
- type SDK
- type SDKEvent
- type SDKState
- type SessionStore
- type Token
- type Transport
Constants ¶
const ( SDKReady = "ready" SDKNotReady = "not_ready" SDKStateChanged = "player_state_changed" SDKInitError = "initialization_error" SDKAuthError = "authentication_error" SDKAccountError = "account_error" SDKPlaybackError = "playback_error" SDKAutoplayFailed = "autoplay_failed" )
const ( DefaultAPIBase = "https://api.spotify.com/v1" AuthorizeURL = "https://accounts.spotify.com/authorize" TokenURL = "https://accounts.spotify.com/api/token" )
Variables ¶
var ErrAuthState = errors.New("spotify auth: state check failed")
var ErrNoToken = errors.New("spotify auth: no refreshable token")
var Scopes = []string{
"playlist-read-private",
"playlist-read-collaborative",
"user-read-private",
"user-library-read",
"user-library-modify",
"user-read-recently-played",
"user-read-playback-state",
"user-modify-playback-state",
"streaming",
}
Scopes is the documented access needed for browsing and Connect playback.
Functions ¶
func MapSDKError ¶
MapSDKError gives SDK errors a predictable app-level kind, including Premium-account failures.
func MapSDKState ¶
MapSDKState converts a browser player state into the same events used by the desktop player. It has no DOM or syscall/js dependency.
func PKCEChallenge ¶
PKCEChallenge returns the RFC 7636 S256 challenge for verifier.
Types ¶
type API ¶
type API struct {
Auth *Auth
Transport Transport
BaseURL string
Now func() time.Time
Wait func(context.Context, time.Duration) error
Max429Retries int
}
API is an OAuth-authorized Spotify Web API client.
func (*API) Artist ¶
Artist loads artist metadata. The Web API's artist top-tracks endpoint is unavailable to current Development Mode apps, so no unofficial substitute is queried.
func (*API) ContextTracks ¶
ContextTracks reads saved tracks, album tracks, or playlist items. The playlist endpoint is the documented /items route.
func (*API) Liked ¶
Liked reports whether each track URI is saved, using the URI-based contains endpoint.
func (*API) LikedTracks ¶
LikedTracks returns one documented page of saved tracks.
func (*API) PlaybackState ¶
PlaybackState returns the current documented Web API player state.
func (*API) RecentlyPlayed ¶
RecentlyPlayed returns unique contexts ordered by their latest play, using only Spotify's documented recently-played endpoint.
func (*API) Search ¶
Search searches tracks, albums, artists, and playlists. The Web API's current Development Mode limit is ten results per type per request, so larger app requests are paged with offset.
func (*API) SetShuffle ¶
type Auth ¶
type Auth struct {
ClientID string
RedirectURI string
Transport Transport
Store SessionStore
Now func() time.Time
Random io.Reader
// contains filtered or unexported fields
}
Auth runs Authorization Code with PKCE and refreshes tokens.
func (*Auth) AccessToken ¶
AccessToken returns a current access token, refreshing it when it is about to expire.
func (*Auth) Begin ¶
Begin creates PKCE state, stores the verifier, and returns Spotify's authorization URL.
func (*Auth) ExchangeCode ¶
ExchangeCode validates the OAuth state and exchanges a one-time code.
type Client ¶
Client combines the official Web API and Web Playback SDK behind the interfaces consumed by internal/app.
func (*Client) ContextTracks ¶
func (*Client) LikedTracks ¶
func (*Client) RecentlyPlayed ¶
func (*Client) SetDeviceName ¶
func (*Client) Start ¶
Start starts the SDK event loop and connects the browser as a Connect device. It emits Ready so the app can load the account library immediately.
func (*Client) SupportsRadio ¶
Song Radio depends on an endpoint Spotify currently marks deprecated; keep the action disabled instead of using a private endpoint.
func (*Client) SupportsSignOut ¶
type FetchTransport ¶
type FetchTransport struct{}
FetchTransport is available in the js/wasm build and calls window.fetch.
type HTTPError ¶
HTTPError is a documented Web API response failure.
func (HTTPError) HTTPStatus ¶
func (HTTPError) RetryAfterValue ¶
type HTTPTransport ¶
HTTPTransport is used by native tests and API client tooling.
type PendingAuth ¶
type Request ¶
Request and Response are the small transport boundary shared by native tests and the browser fetch implementation.
type SDK ¶
type SDK interface {
Events() <-chan SDKEvent
Connect(context.Context) error
Disconnect() error
ActivateElement() error
SetName(context.Context, string) error
}
SDK is the browser-only Web Playback SDK bridge.
type SDKState ¶
type SDKState struct {
Track model.Track
ContextURI string
Position time.Duration
Duration time.Duration
Paused bool
Empty bool
}
SDKState is the DOM-free portion of Web Playback SDK state used by Smolify.
type SessionStore ¶
type SessionStore interface {
LoadToken() (Token, error)
SaveToken(Token) error
ClearToken() error
SavePending(PendingAuth) error
LoadPending() (PendingAuth, error)
ClearPending() error
}
SessionStore keeps only short-lived OAuth state and tokens.
type Token ¶
type Token struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresAt time.Time `json:"expires_at"`
}
Token is kept by a host session store; browser hosts should use sessionStorage rather than persistent storage.