About Spotify.ahk

Spotify.ahk is a (hopefully) fully featured wrapper for the Spotify API which aims to allow complete automation of Spotify

The implementation

The core of Spotify.ahk is the Spotify.Util class, which contains all code for getting user auth done, and sending actual web requests.

Spotify.Util.CustomCall

The core of the Spotify.Util class is the Spotify.Util.CustomCall method, which takes a HTTP Method, URL, array of request headers, a flag for disabling a check for if the authorization token is up to data, data to send in the body of the request, a flag to ignore non 2xx HTTP status codes, and finally, a flag passed to the HTTP cache class to decide how the request should be cached.

Because of this large number of parameters, if -1 is passed as the third parameter, the fourth parameter is treated as an object, where each key is a parameter name, mapped to the value for that parameter. This allows for fake named parameters, and cleans up API calls by a lot.

After the named parameters are dealt with, .CustomCall will check if the NoTimeOut parameter is set, and if it isn't, then an automatic check will be made that will request a new web API authorization token before making the current request. If the flag is set, this check is bypassed. This allows for requests that don't require authorization to be done with no extra overhead.

Next, .CustomCall checks if the URL passed starts with a Spotify API URL base, and if it doesn't, it prepends the API base URL to the passed URL. This allows for endpoints to be passed as URLs, and automatically converted to full URLs.

After this, .CustomCall will check if the HeaderMap parameter was passed, and if it wasn't, a default header map will be created containing the default Spotify authorization header. This allows for calls to .CustomCall to not know the Spotify API token, and keeps the token inside the Spotify.Util class only.

Finally, HTTP.Request is called, and the response to the current request is returned.

Static Classes

Classes like Spotify.Player and Spotify.Library are all static, and take parameters as raw Spotify IDs, and return AHK objects that represent the API response to whatever method was called. Objects of classes like Spotify.Track are not static, and contain info about themselves that can be found on their docs page. Methods of these classes expect objects of the similiar classes. For example,

NewPlaylist := Spotify.Playlists.CreatePlaylist("Test 123", "This is an example playlist made by Spotify.ahk")
GoodSongIMO := Spotify.Tracks.GetTrack("4A6eJ3gOmVdD7C69N3DC7K")
NewPlaylist.AddTrack(GoodSongIMO)

Since both NewPlaylist and GoodSongIMO are objects of a Spotify.* class, they take parameters that are also objects of a Spotify.* class.