Skip to content

Commit 8313666

Browse files
committed
most people don't want pagingobjects returned
start refractoring [CursorBased]PagingObject RestActions
1 parent 18ddc8d commit 8313666

File tree

7 files changed

+89
-51
lines changed

7 files changed

+89
-51
lines changed

examples/src/main/kotlin/Examples.kt

Whitespace-only changes.

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pluginManagement {
88
}
99
}
1010
rootProject.name = 'SpotifyKotlinWrapper'
11+
include 'spotify.examples'
1112

spotify.examples/build.gradle

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
plugins {
2+
id 'java'
3+
id 'org.jetbrains.kotlin.jvm'
4+
}
5+
6+
group 'com.adamratzman'
7+
version '2.0.0'
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenCentral()
13+
maven { url 'https://jitpack.io' }
14+
}
15+
16+
dependencies {
17+
implementation 'com.github.adamint:spotify-web-api-kotlin:dev-SNAPSHOT'
18+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
19+
testCompile group: 'junit', name: 'junit', version: '4.12'
20+
}
21+
22+
compileKotlin {
23+
kotlinOptions.jvmTarget = "1.8"
24+
}
25+
compileTestKotlin {
26+
kotlinOptions.jvmTarget = "1.8"
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import com.adamratzman.spotify.main.spotifyApi
2+
import com.adamratzman.spotify.utils.Market
3+
import com.adamratzman.spotify.utils.Track
4+
5+
fun main(args:Array<String>){
6+
val api = spotifyApi {
7+
credentials {
8+
clientId = "79d455af5aea45c094c5cea04d167ac1"
9+
clientSecret = "2ed0a8e3a946471e928c5e80b83c184d"
10+
}
11+
}.buildCredentialed()
12+
13+
//api.search.searchTrack("High Hopes", 4, market = Market.US).queue { ->
14+
//items.
15+
16+
//}
17+
}

src/main/kotlin/com/adamratzman/spotify/main/SpotifyRestAction.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.util.concurrent.CompletableFuture
44
import java.util.concurrent.TimeUnit
55
import java.util.function.Supplier
66

7-
class SpotifyRestAction<T>(private val api: SpotifyAPI, private val supplier: Supplier<T>) {
7+
open class SpotifyRestAction<T>(private val api: SpotifyAPI, private val supplier: Supplier<T>) {
88
fun complete(): T {
99
return try {
1010
supplier.get()
@@ -38,4 +38,10 @@ class SpotifyRestAction<T>(private val api: SpotifyAPI, private val supplier: Su
3838
}
3939

4040
override fun toString() = complete().toString()
41-
}
41+
}
42+
/*
43+
class SpotifyRestPagingAction<Z, T: PagingObject<Z>>(val api: SpotifyAPI) {
44+
fun complete(): Z {
45+
}
46+
fun complete
47+
}*/

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

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,19 @@ import java.net.URLEncoder
1111
import java.util.*
1212
import java.util.function.Supplier
1313

14-
data class CursorBasedPagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String?, val cursors: Cursor,
15-
val total: Int, val endpoint: SpotifyEndpoint) {
16-
inline fun <reified T> getNext(): SpotifyRestAction<CursorBasedPagingObject<T>?> = endpoint.toAction(
17-
Supplier {
18-
catch {
19-
next?.let { endpoint.get(it).toCursorBasedPagingObject<T>(endpoint = endpoint) }
20-
}
21-
})
22-
23-
inline fun <reified T> getAll(): SpotifyRestAction<List<CursorBasedPagingObject<T>>> {
24-
return endpoint.toAction(Supplier {
25-
this as CursorBasedPagingObject<T>
26-
val pagingObjects = mutableListOf<CursorBasedPagingObject<T>>(this)
27-
var next = getNext<T>().complete()
28-
while (next != null) {
29-
pagingObjects.add(next)
30-
next = getNext<T>().complete()
31-
}
32-
pagingObjects.toList()
33-
})
34-
}
35-
36-
inline fun <reified T> getAllItems(): SpotifyRestAction<List<T>> {
37-
return endpoint.toAction(Supplier {
38-
getAll<T>().complete().map { it.items }.flatten()
39-
})
40-
}
41-
}
42-
4314
data class Cursor(val after: String)
4415

45-
data class PagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String? = null, val offset: Int = 0,
16+
class CursorBasedPagingObject<out T>(href: String, items: List<T>, limit: Int, next: String?,
17+
val cursors: Cursor, total: Int, endpoint: SpotifyEndpoint)
18+
: PagingObject<T>(href, items, limit, next, 0, null, total, endpoint)
19+
20+
open class PagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String? = null, val offset: Int = 0,
4621
val previous: String? = null, val total: Int, var endpoint: SpotifyEndpoint) {
4722
inline fun <reified T> getNext(): SpotifyRestAction<PagingObject<T>?> = endpoint.toAction(
4823
Supplier {
4924
catch {
50-
next?.let { endpoint.get(it).toPagingObject<T>(endpoint = endpoint) }
25+
if (this is CursorBasedPagingObject) next?.let { endpoint.get(it).toCursorBasedPagingObject<T>(endpoint = endpoint) }
26+
else next?.let { endpoint.get(it).toPagingObject<T>(endpoint = endpoint) }
5127
}
5228
})
5329

@@ -62,29 +38,40 @@ data class PagingObject<out T>(val href: String, val items: List<T>, val limit:
6238
this as PagingObject<T>
6339
return endpoint.toAction(
6440
Supplier {
65-
val pagingObjects = mutableListOf<PagingObject<T>>()
66-
var prev = previous?.let { getPrevious<T>().complete() }
67-
while (prev != null) {
68-
pagingObjects.add(prev)
69-
prev = prev.previous?.let { prev?.getPrevious<T>()?.complete() }
70-
}
71-
pagingObjects.reverse() // closer we are to current, the further we are from the start
72-
73-
pagingObjects.add(this)
74-
75-
var nxt = next?.let { getNext<T>().complete() }
76-
while (nxt != null) {
77-
pagingObjects.add(nxt)
78-
nxt = nxt.next?.let { nxt?.getNext<T>()?.complete() }
41+
if (this is CursorBasedPagingObject) {
42+
this as CursorBasedPagingObject<T>
43+
val pagingObjects = mutableListOf<CursorBasedPagingObject<T>>(this)
44+
var next = getNext<T>().complete()
45+
while (next != null) {
46+
pagingObjects.add(next as CursorBasedPagingObject<T>)
47+
next = getNext<T>().complete()
48+
}
49+
pagingObjects.toList()
50+
} else {
51+
val pagingObjects = mutableListOf<PagingObject<T>>()
52+
var prev = previous?.let { getPrevious<T>().complete() }
53+
while (prev != null) {
54+
pagingObjects.add(prev)
55+
prev = prev.previous?.let { prev?.getPrevious<T>()?.complete() }
56+
}
57+
pagingObjects.reverse() // closer we are to current, the further we are from the start
58+
59+
pagingObjects.add(this)
60+
61+
var nxt = next?.let { getNext<T>().complete() }
62+
while (nxt != null) {
63+
pagingObjects.add(nxt)
64+
nxt = nxt.next?.let { nxt?.getNext<T>()?.complete() }
65+
}
66+
// we don't need to reverse here, as it's in order
67+
pagingObjects.toList()
7968
}
80-
// we don't need to reverse here, as it's in order
81-
pagingObjects.toList()
8269
})
8370
}
8471

8572
inline fun <reified T> getAllItems(): SpotifyRestAction<List<T>> {
8673
return endpoint.toAction(Supplier {
87-
getAll<T>().complete().asSequence().map { it as PagingObject<T> }.map { it.items }.toList().flatten()
74+
getAll<T>().complete().asSequence().map { it.items }.toList().flatten()
8875
})
8976
}
9077
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,5 @@ enum class Market(val country: String) {
206206
ZM("Zambia"),
207207
ZW("Zimbabwe");
208208

209-
val code = name
209+
val code = name;
210210
}

0 commit comments

Comments
 (0)