Skip to content

Commit 02c85fa

Browse files
committed
SpotifyURIs can be regular classes, abstract
1 parent e4eed05 commit 02c85fa

File tree

5 files changed

+47
-61
lines changed

5 files changed

+47
-61
lines changed

build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ test {
6363

6464
compileKotlin {
6565
kotlinOptions.jvmTarget = "1.8"
66-
kotlinOptions {
67-
freeCompilerArgs = ["-XXLanguage:+InlineClasses"]
68-
}
6966
}
7067
compileTestKotlin {
7168
kotlinOptions.jvmTarget = "1.8"

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.adamratzman.spotify.utils
22

3-
import com.adamratzman.spotify.main.SpotifyClientAPI
43
import com.adamratzman.spotify.main.SpotifyScope
54

65
data class Token(val access_token: String, val token_type: String, val expires_in: Int, val refresh_token: String?, val scope: String?) {
@@ -17,4 +16,8 @@ data class ErrorResponse(val error: ErrorObject)
1716

1817
data class ErrorObject(val status: Int, val message: String)
1918

20-
class BadRequestException(val error: ErrorObject) : Exception("Received Status Code ${error.status}. Error cause: ${error.message}")
19+
class SpotifyUriException(message: String) : BadRequestException(message)
20+
21+
open class BadRequestException(message: String) : Exception(message) {
22+
constructor(error: ErrorObject) : this("Received Status Code ${error.status}. Error cause: ${error.message}")
23+
}

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,43 @@ private fun String.add(type: String): String {
1010
this.matchType(type)?.also {
1111
return "spotify:$type:${it.trim()}"
1212
}
13-
throw IllegalArgumentException("'$this' isn't convertible to '$type' uri")
13+
throw SpotifyUriException("Illegal Spotify ID/URI: '$this' isn't convertible to '$type' uri")
1414
}
1515

1616
private fun String.remove(type: String): String {
1717
this.matchType(type)?.also {
1818
return it.trim()
1919
}
20-
throw IllegalArgumentException("'$this' isn't convertible to '$type' id")
20+
throw SpotifyUriException("Illegal Spotify ID/URI: '$this' isn't convertible to '$type' id")
2121
}
2222

23+
abstract class SpotifyUri(tmp:String, val input: String = tmp.replace(" ", "")) {
24+
abstract val uri:String
25+
abstract val id:String
26+
}
2327

24-
inline class AlbumURI(private val input: String) {
25-
val uri: String
26-
get() = input.add("album")
27-
val id: String
28-
get() = input.remove("album")
28+
class AlbumURI(input: String):SpotifyUri(input) {
29+
override val uri: String = input.add("album")
30+
override val id: String = input.remove("album")
2931
}
3032

31-
inline class ArtistURI(private val input: String) {
32-
val uri: String
33-
get() = input.add("artist")
34-
val id: String
35-
get() = input.remove("artist")
33+
class ArtistURI(input: String):SpotifyUri(input) {
34+
override val uri: String = input.add("artist")
35+
override val id: String = input.remove("artist")
3636
}
3737

38-
inline class TrackURI(private val input: String) {
39-
val uri: String
40-
get() = input.add("track")
41-
val id: String
42-
get() = input.remove("track")
38+
class TrackURI(input: String):SpotifyUri(input) {
39+
override val uri: String= input.add("track")
40+
override val id: String= input.remove("track")
4341
}
4442

45-
inline class UserURI(private val input: String) {
46-
val uri: String
47-
get() = input.add("user")
48-
val id: String
49-
get() = input.remove("user")
43+
class UserURI(input: String):SpotifyUri(input) {
44+
override val uri: String = input.add("user")
45+
override val id: String = input.remove("user")
5046
}
5147

52-
inline class PlaylistURI(private val input: String) {
53-
val uri: String
54-
get() = input.add("playlist")
55-
val id: String
56-
get() = input.remove("playlist")
48+
class PlaylistURI(input: String) :SpotifyUri(input){
49+
override val uri: String = input.add("playlist")
50+
override val id: String= input.remove("playlist")
5751
}
5852

src/test/kotlin/com/adamratzman/spotify/public/PublicTracksAPITest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PublicTracksAPITest : Spek({
4343
assertEquals("0.0589", t.getAudioFeatures("6AH3IbS61PiabZYKVBqKAk").complete().acousticness.toString())
4444
}
4545
it("multiple tracks (all known)") {
46-
assertEquals(listOf(null, "0.0589"), t.getAudioFeatures("", "6AH3IbS61PiabZYKVBqKAk").complete().map { it?.acousticness?.toString() })
46+
assertEquals(listOf(null, "0.0589"), t.getAudioFeatures("hkiuhi", "6AH3IbS61PiabZYKVBqKAk").complete().map { it?.acousticness?.toString() })
4747
}
4848
it("mix of known and unknown tracks") {
4949
assertTrue(t.getAudioFeatures("bad track", "0o4jSZBxOQUiDKzMJSqR4x").complete().let {

src/test/kotlin/com/adamratzman/spotify/utilities/UrisTests.kt

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.adamratzman.spotify.utilities
22

3-
import com.adamratzman.spotify.utils.AlbumURI
4-
import com.adamratzman.spotify.utils.ArtistURI
5-
import com.adamratzman.spotify.utils.PlaylistURI
6-
import com.adamratzman.spotify.utils.TrackURI
7-
import com.adamratzman.spotify.utils.UserURI
3+
import com.adamratzman.spotify.utils.*
84
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
95
import org.junit.jupiter.api.Assertions.assertEquals
106
import org.junit.jupiter.api.assertThrows
@@ -16,19 +12,15 @@ class UrisTests : Spek({
1612
describe("TrackURI tests") {
1713
it("Create track with invalid input") {
1814

19-
assertDoesNotThrow {
15+
assertThrows<BadRequestException> {
2016
TrackURI("a:invalid")
2117
}
2218

23-
assertThrows<IllegalArgumentException> {
19+
assertThrows<BadRequestException> {
2420
TrackURI("a:invalid").uri
2521
}
2622

27-
assertThrows<IllegalArgumentException> {
28-
TrackURI("a:invalid").id
29-
}
30-
31-
assertThrows<IllegalArgumentException> {
23+
assertThrows<BadRequestException> {
3224
TrackURI("spotify:user:7r7uq6qxa4ymx3wnjd9mm6i83").uri
3325
}
3426
}
@@ -68,19 +60,19 @@ class UrisTests : Spek({
6860
describe("UserURI") {
6961
it("Create user with invalid input") {
7062

71-
assertDoesNotThrow {
63+
assertThrows<BadRequestException> {
7264
UserURI("a:invalid")
7365
}
7466

75-
assertThrows<IllegalArgumentException> {
67+
assertThrows<BadRequestException> {
7668
UserURI("a:invalid").uri
7769
}
7870

79-
assertThrows<IllegalArgumentException> {
71+
assertThrows<BadRequestException> {
8072
UserURI("a:invalid").id
8173
}
8274

83-
assertThrows<IllegalArgumentException> {
75+
assertThrows<BadRequestException> {
8476
UserURI("spotify:track:1Z9UVqWuRJ7zToOiVnlXRO").uri
8577
}
8678
}
@@ -134,19 +126,19 @@ class UrisTests : Spek({
134126
describe("PlaylistURI") {
135127
it("Create playlist with invalid input") {
136128

137-
assertDoesNotThrow {
129+
assertThrows<BadRequestException> {
138130
PlaylistURI("a:invalid")
139131
}
140132

141-
assertThrows<IllegalArgumentException> {
133+
assertThrows<BadRequestException> {
142134
PlaylistURI("a:invalid").uri
143135
}
144136

145-
assertThrows<IllegalArgumentException> {
137+
assertThrows<BadRequestException> {
146138
PlaylistURI("a:invalid").id
147139
}
148140

149-
assertThrows<IllegalArgumentException> {
141+
assertThrows<BadRequestException> {
150142
PlaylistURI("spotify:track:1Z9UVqWuRJ7zToOiVnlXRO").uri
151143
}
152144
}
@@ -199,19 +191,19 @@ class UrisTests : Spek({
199191
describe("AlbumURI tests") {
200192
it("Create album with invalid input") {
201193

202-
assertDoesNotThrow {
194+
assertThrows<BadRequestException> {
203195
AlbumURI("a:invalid")
204196
}
205197

206-
assertThrows<IllegalArgumentException> {
198+
assertThrows<BadRequestException> {
207199
AlbumURI("a:invalid").uri
208200
}
209201

210-
assertThrows<IllegalArgumentException> {
202+
assertThrows<BadRequestException> {
211203
AlbumURI("a:invalid").id
212204
}
213205

214-
assertThrows<IllegalArgumentException> {
206+
assertThrows<BadRequestException> {
215207
AlbumURI("spotify:user:7r7uq6qxa4ymx3wnjd9mm6i83").uri
216208
}
217209
}
@@ -251,19 +243,19 @@ class UrisTests : Spek({
251243
describe("ArtistURI tests") {
252244
it("Create artist with invalid input") {
253245

254-
assertDoesNotThrow {
246+
assertThrows<BadRequestException> {
255247
ArtistURI("a:invalid")
256248
}
257249

258-
assertThrows<IllegalArgumentException> {
250+
assertThrows<BadRequestException> {
259251
ArtistURI("a:invalid").uri
260252
}
261253

262-
assertThrows<IllegalArgumentException> {
254+
assertThrows<BadRequestException> {
263255
ArtistURI("a:invalid").id
264256
}
265257

266-
assertThrows<IllegalArgumentException> {
258+
assertThrows<BadRequestException> {
267259
ArtistURI("spotify:user:7r7uq6qxa4ymx3wnjd9mm6i83").uri
268260
}
269261
}

0 commit comments

Comments
 (0)