Skip to content

Commit a0605b0

Browse files
authored
Merge pull request #12 from adamint/develop
add options to getSavedAlbums, update kotlin version
2 parents d39187f + 3de1cd7 commit a0605b0

File tree

4 files changed

+76
-70
lines changed

4 files changed

+76
-70
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ group 'com.adamratzman'
22
version '1.0.3'
33

44
buildscript {
5-
ext.kotlin_version = '1.2.51'
5+
ext.kotlin_version = '1.2.61'
66

77
repositories {
88
mavenCentral()

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/library/ClientLibraryAPI.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2525
*
2626
* @return Paging Object of [SavedAlbum] ordered by position in library
2727
*/
28-
fun getSavedAlbums(): SpotifyRestAction<PagingObject<SavedAlbum>> {
28+
fun getSavedAlbums(limit: Int? = null, offset: Int? = null, market: Market? = null): SpotifyRestAction<PagingObject<SavedAlbum>> {
2929
return toAction(Supplier {
30-
get(EndpointBuilder("/me/albums").build()).toPagingObject<SavedAlbum>(endpoint = this)
30+
get(EndpointBuilder("/me/albums").with("limit", limit).with("offset", offset).with("market", market?.code)
31+
.build()).toPagingObject<SavedAlbum>(endpoint = this)
3132
})
3233
}
3334

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.adamratzman.spotify.utils
2+
3+
import com.adamratzman.spotify.main.SpotifyAPI
4+
import com.adamratzman.spotify.main.SpotifyClientAPI
5+
import com.adamratzman.spotify.main.base
6+
import org.json.JSONObject
7+
import org.jsoup.Connection
8+
import org.jsoup.Jsoup
9+
import java.util.function.Supplier
10+
11+
abstract class SpotifyEndpoint(val api: SpotifyAPI) {
12+
fun get(url: String): String {
13+
return execute(url)
14+
}
15+
16+
fun post(url: String, body: String? = null): String {
17+
return execute(url, body, Connection.Method.POST)
18+
}
19+
20+
fun put(url: String, body: String? = null, contentType: String? = null): String {
21+
return execute(url, body, Connection.Method.PUT, contentType = contentType)
22+
}
23+
24+
fun delete(url: String, body: String? = null, data: List<Pair<String, String>>? = null, contentType: String? = null): String {
25+
return execute(url, body, Connection.Method.DELETE, data = data, contentType = contentType)
26+
}
27+
28+
private fun execute(url: String, body: String? = null, method: Connection.Method = Connection.Method.GET,
29+
retry202: Boolean = true, contentType: String? = null, data: List<Pair<String, String>>? = null): String {
30+
if (api !is SpotifyClientAPI && System.currentTimeMillis() >= api.expireTime) {
31+
api.refreshClient()
32+
api.expireTime = System.currentTimeMillis() + api.token.expires_in * 1000
33+
}
34+
var connection = Jsoup.connect(url).ignoreContentType(true)
35+
data?.forEach { connection.data(it.first, it.second) }
36+
if (contentType != null) connection.header("Content-Type", contentType)
37+
if (body != null) {
38+
if (contentType != null) connection.requestBody(body)
39+
else
40+
connection = if (method == Connection.Method.DELETE) {
41+
val key = JSONObject(body).keySet().toList()[0]
42+
connection.data(key, JSONObject(body).getJSONArray(key).toString())
43+
} else connection.requestBody(body)
44+
}
45+
connection = connection.header("Authorization", "Bearer ${api.token.access_token}")
46+
val document = connection.method(method).ignoreHttpErrors(true).execute()
47+
if (document.statusCode() / 200 != 1 /* Check if status is 2xx */) throw BadRequestException(api.gson.fromJson(document.body(), ErrorResponse::class.java).error)
48+
else if (document.statusCode() == 202 && retry202) return execute(url, body, method, false)
49+
return document.body()
50+
}
51+
52+
fun <T> toAction(supplier: Supplier<T>) = SpotifyRestAction(api, supplier)
53+
}
54+
55+
internal class EndpointBuilder(val path: String) {
56+
val builder = StringBuilder(base)
57+
58+
init {
59+
builder.append(path)
60+
}
61+
62+
fun with(key: String, value: Any?): EndpointBuilder {
63+
if (value != null && (value !is String || value.isNotEmpty())) {
64+
if (builder.toString() == base + path) builder.append("?")
65+
else builder.append("&")
66+
builder.append(key).append("=").append(value.toString())
67+
}
68+
return this
69+
}
70+
71+
fun build() = builder.toString()
72+
}

src/main/kotlin/com/adamratzman/spotify/utils/Helpers.kt

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,13 @@
11
package com.adamratzman.spotify.utils
22

33
import com.adamratzman.spotify.main.SpotifyAPI
4-
import com.adamratzman.spotify.main.SpotifyClientAPI
5-
import com.adamratzman.spotify.main.base
64
import com.google.gson.Gson
75
import org.json.JSONObject
8-
import org.jsoup.Connection
9-
import org.jsoup.Jsoup
106
import java.io.InvalidObjectException
117
import java.net.URLEncoder
128
import java.util.*
139
import java.util.function.Supplier
1410

15-
abstract class SpotifyEndpoint(val api: SpotifyAPI) {
16-
fun get(url: String): String {
17-
return execute(url)
18-
}
19-
20-
fun post(url: String, body: String? = null): String {
21-
return execute(url, body, Connection.Method.POST)
22-
}
23-
24-
fun put(url: String, body: String? = null, contentType: String? = null): String {
25-
return execute(url, body, Connection.Method.PUT, contentType = contentType)
26-
}
27-
28-
fun delete(url: String, body: String? = null, data: List<Pair<String, String>>? = null, contentType: String? = null): String {
29-
return execute(url, body, Connection.Method.DELETE, data = data, contentType = contentType)
30-
}
31-
32-
private fun execute(url: String, body: String? = null, method: Connection.Method = Connection.Method.GET,
33-
retry202: Boolean = true, contentType: String? = null, data: List<Pair<String, String>>? = null): String {
34-
if (api !is SpotifyClientAPI && System.currentTimeMillis() >= api.expireTime) {
35-
api.refreshClient()
36-
api.expireTime = System.currentTimeMillis() + api.token.expires_in * 1000
37-
}
38-
var connection = Jsoup.connect(url).ignoreContentType(true)
39-
data?.forEach { connection.data(it.first, it.second) }
40-
if (contentType != null) connection.header("Content-Type", contentType)
41-
if (body != null) {
42-
if (contentType != null) connection.requestBody(body)
43-
else
44-
connection = if (method == Connection.Method.DELETE) {
45-
val key = JSONObject(body).keySet().toList()[0]
46-
connection.data(key, JSONObject(body).getJSONArray(key).toString())
47-
} else connection.requestBody(body)
48-
}
49-
connection = connection.header("Authorization", "Bearer ${api.token.access_token}")
50-
val document = connection.method(method).ignoreHttpErrors(true).execute()
51-
if (document.statusCode() / 200 != 1 /* Check if status is 2xx */) throw BadRequestException(api.gson.fromJson(document.body(), ErrorResponse::class.java).error)
52-
else if (document.statusCode() == 202 && retry202) return execute(url, body, method, false)
53-
return document.body()
54-
}
55-
56-
fun <T> toAction(supplier: Supplier<T>) = SpotifyRestAction(api, supplier)
57-
}
58-
59-
internal class EndpointBuilder(val path: String) {
60-
val builder = StringBuilder(base)
61-
62-
init {
63-
builder.append(path)
64-
}
65-
66-
fun with(key: String, value: Any?): EndpointBuilder {
67-
if (value != null && (value !is String || value.isNotEmpty())) {
68-
if (builder.toString() == base + path) builder.append("?")
69-
else builder.append("&")
70-
builder.append(key).append("=").append(value.toString())
71-
}
72-
return this
73-
}
74-
75-
fun build() = builder.toString()
76-
}
77-
7811
data class CursorBasedPagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String?, val cursors: Cursor,
7912
val total: Int, val endpoint: SpotifyEndpoint) {
8013
inline fun <reified T> getNext(): SpotifyRestAction<CursorBasedPagingObject<T>> = endpoint.toAction(

0 commit comments

Comments
 (0)