Skip to content

More about library

Kaustubh Patange edited this page Jun 6, 2020 · 1 revision

Limitation

The library does not cover all the official API, some of them are LibraryApi, PersonalizationApi, some calls of PlaylistApi, etc.

However, you can extend the API and create new methods.

Understanding

Internally the library uses GET, POST, PUT, DELETE calls to the official API, and creates a Data Object from the JSON.

The library provides certain functions like

  • executeGETMethod - for making GET request
  • executePOSTMethod - for making POST request
  • executePUTMethod - for making PUT request
  • executeDELETEMethod - for making DELETE request

We will use these functions to make API calls.

Extending Library

  • Example 1 - Creating Get a List of Browse Categories of BrowseApi.
  1. First, we need to run this API in their web-console to get a JSON response, so head over here.
  2. Now once we got the response JSON, we need to create a POJO class from it. You can either use a plugin or https://www.json2kotlin.com website. Generate the class and add it to Android Studio. Below is a Kotlin code of the class.
data class Browse(
    val categories: Categories
) {
    data class Categories(
        val href: String,
        val items: List<Item>,
        val limit: Int,
        val next: String,
        val offset: Int,
        val previous: Any?,
        val total: Int
    ) {
        data class Item(
            val href: String,
            val icons: List<Icon>,
            val id: String,
            val name: String
        ) {
            data class Icon(
                val height: Int?,
                val url: String,
                val width: Int?
            )
        }
    }
}

For Java you can use http:https://www.jsonschema2pojo.org/ this website.

  1. Now create a new class BrowseApi with primary constructor accepting Spotify client as a parameter.
class BrowseApi(
    private val client: SpotifyClient
) {
    // All codes here...
}
  1. Our endpoint URL is https://api.spotify.com/v1/browse/categories with country & locale being an optional parameter. With that being in mind, our final function will be...
    /**
     * Get a List of Browse Categories
     */
    fun getBrowseCategoriesList(
        limit: Int = 10,
        offset: Int = 5,
        country: String? = null,
        locale: String? = null,
        responseAction: ResponseAction<Browse>
    ) {
        client.commonWorkFlow { b, _, exception ->
            if (!b) {
                responseAction.onError(exception ?: client.comExp)
                return@commonWorkFlow
            }

            /** Country & locale are optional parameter **/

            var url = "https://api.spotify.com/v1/browse/categories?limit=${limit}&offset=${offset}"
            if (country != null) url += "&country=${country}"
            if (locale != null) url += "&locale=${locale}"

            client.executeGETMethod(
                url = url,
                type = Browse::class.java,
                responseAction = responseAction
            )
        }
    }
  1. That's it all you've to do is create an instance of the class and invoke the function.
val browseApi = BrowseApi(client)

browseApi.getBrowseCategoriesList(10, 5, null, null, 
    object : ResponseAction<Browse> {
        override fun onComplete(t: Browse) {
            Toast.makeText(this@MainActivity, "Name: ${t.categories.total}", Toast.LENGTH_SHORT).show()
        }
        override fun onError(e: Exception) {
            Toast.makeText(this@MainActivity, "Error: ${e.message}", Toast.LENGTH_SHORT)
                        .show()
        }
    })
Clone this wiki locally