Skip to content

hntd187/spotify

Repository files navigation

Codacy Badge Coverage Status

A Scala library for the Spotify API. The documentation on the Spotify API can be found here. It currently supports most features surrounding artists, tracks, and albums. Currently no user support is implemented.

Adding to project

libraryDependencies += "io.scarman" %% "spotify-api" % "0.3.0"

or if you are using scalaJS

libraryDependencies += "io.scarman" %%% "spotify-api" % "0.3.0"

Using the Library

There are 2 ways to make use of this library and there are some important things to keep in mind when using this library. The packages of this library are separated into request and response. Not surprisingly these are for the request portion and response portion of the API. It's important to understand this so you use the right classes.

First use

The first way uses the Spotify object directly to create requests. No matter which way you use you must have an sttp implicit backend in scope. Currently this API supports any backend implementing Backend[Future[R], _], which is AsyncHttpClientFutureBackend for JVM usage or FetchBackend if you are using JS.

import scala.concurrent.Future
import io.scarman.spotify.*
import io.scarman.spotify.{response as resp}
import com.softwaremill.sttp.asynchttpclient.future.AsyncHttpClientFutureBackend

val appId = ""
val appSecret = ""
implicit val backend = AsyncHttpClientFutureBackend()

val spotify = Spotify(appId, appSecret)

val artistId = ""
val artist: Artist = spotify.getArtist(artistId)
val response: Future[resp.Artist] = artist()
Second Way

The second way uses the Spotify object implicitly and creates the case classes directly.

import scala.concurrent.Future
import io.scarman.spotify.*
import io.scarman.spotify.{response as resp}
import com.softwaremill.sttp.asynchttpclient.future.AsyncHttpClientFutureBackend

val appId = ""
val appSecret = ""
implicit val backend = AsyncHttpClientFutureBackend()

implicit val spotify = Spotify(appId, appSecret)

val artistId = ""
val artist: Artist = Artist(artistId)
val response: Future[resp.Artist] = artist()

In these examples, we create the initial entrance point via Spotify and then create a request for an Artist. The response from this request would be the serialized result of the Future. If you've used dispatch before this should look familiar to you as the request isn't actually made until the .apply() method is called on the request object. It's the users responsibility to deal with the future at this point, but some of the response objects have methods on them which make subsequent requests using information you've already provided much easier.

import io.scarman.spotify.*

val artist = Artist("id")
val albums = artist.albums()

val track = Track("id")
val features = track.getAudioFeatures()

Currently most of the non-user related API requests are implemented. This includes artists, albums, tracks. Down the road I plan to finish off the rest of the API, but I'm not sure how the user calls will fit in down the road. I'm open to suggestions on them. Now that I have a working JS implementation it will probably make sense implement the rest of the api for more user driven JS applications to make use of.

Contact

Stephen Carman shcarman@gmail.com