Skip to content

Commit

Permalink
handle bad request
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Volkmann committed Feb 14, 2021
1 parent 45af881 commit 276a9e4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
11 changes: 5 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.20'
id 'org.jetbrains.kotlin.jvm' version '1.4.30'
id "com.github.ben-manes.versions" version "0.36.0"
id 'org.springframework.boot' version '2.4.0'
id 'org.springframework.boot' version '2.4.2'
}

ext {
Expand Down Expand Up @@ -40,8 +40,8 @@ dependencies {
compile 'com.apurebase:arkenv:3.1.0'
compile 'org.apache.commons:commons-text:1.9'

testImplementation "io.strikt:strikt-core:0.28.1"
testImplementation "io.mockk:mockk:1.10.3"
testImplementation "io.strikt:strikt-core:0.28.2"
testImplementation "io.mockk:mockk:1.10.6"
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junit_version
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junit_version"
}
Expand All @@ -58,9 +58,8 @@ test {
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "11"
kotlinOptions.useIR = true

}
compileTestKotlin {
kotlinOptions.jvmTarget = "13"
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/me/avo/spottit/data/TrackResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.avo.spottit.data

import com.wrapper.spotify.model_objects.specification.Track
import me.avo.spottit.model.RedditTrack

/**
* Result of the find track operation.
* @param redditTrack the reddit track to look for.
* @param chosenTrack the chosen track.
* @param totalNumberResults the total number of results found.
*/
data class TrackResult(
val redditTrack: RedditTrack,
val chosenTrack: Track?,
val totalNumberResults: Int,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.avo.spottit.service.spotify

import com.wrapper.spotify.model_objects.specification.Track
import me.avo.spottit.data.TrackResult
import me.avo.spottit.model.RedditTrack
import me.avo.spottit.util.SpotifyQueryTools
import me.avo.spottit.util.TrackFilter
Expand All @@ -13,26 +14,33 @@ class ElectronicSearchAlgorithm(

override fun searchForTracks(tracks: List<RedditTrack>): List<Track> = tracks
.asSequence()
.map(::findTrack)
.map(::tryFindTrack)
.mapNotNull { (reddit, chosenTrack, total) ->
logger.info("Found $total results for: $reddit | ${chosenTrack?.artists?.joinToString { it.name }} - ${chosenTrack?.name}")
chosenTrack
}
.onEach { trackFilter.timeout() }
.toList()

private fun findTrack(track: RedditTrack): Triple<RedditTrack, Track?, Int> = when {
private fun tryFindTrack(track: RedditTrack): TrackResult = try {
findTrack(track)
} catch (ex: Exception) {
logger.warn("Exception finding track $track", ex)
TrackResult(track, null, 0)
}

private fun findTrack(track: RedditTrack): TrackResult = when {
track.isSpotifyTrack -> getTrack(track)
else -> searchForTrack(track)
}

private fun getTrack(track: RedditTrack): Triple<RedditTrack, Track?, Int> {
private fun getTrack(track: RedditTrack): TrackResult {
val id = track.url!!.toString().substringAfter("/track/").substringBefore("?")
val spotifyTrack = spotifyApi.getTrack(id)
return Triple(track, spotifyTrack, if (spotifyTrack != null) 1 else 0)
return TrackResult(track, spotifyTrack, if (spotifyTrack != null) 1 else 0)
}

private fun searchForTrack(track: RedditTrack): Triple<RedditTrack, Track?, Int> =
private fun searchForTrack(track: RedditTrack): TrackResult =
SpotifyQueryTools.initialQuery(track, useTags = true)
.let(::searchQuery)
.let { (total, items) ->
Expand All @@ -42,7 +50,7 @@ class ElectronicSearchAlgorithm(
if (trackFilter.checkTrackAgeByAlbum(album)) it else null
} else it
}
Triple(track, chosenTrack, total)
TrackResult(track, chosenTrack, total)
}

private fun searchQuery(query: String): Pair<Int, Array<Track>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import com.github.salomonbrys.kotson.jsonArray
import com.github.salomonbrys.kotson.jsonObject
import com.github.salomonbrys.kotson.toJsonArray
import com.wrapper.spotify.SpotifyApi
import com.wrapper.spotify.exceptions.detailed.BadGatewayException
import com.wrapper.spotify.exceptions.detailed.NotFoundException
import com.wrapper.spotify.exceptions.detailed.ServiceUnavailableException
import com.wrapper.spotify.exceptions.detailed.TooManyRequestsException
import com.wrapper.spotify.exceptions.detailed.*
import com.wrapper.spotify.model_objects.special.SnapshotResult
import com.wrapper.spotify.model_objects.specification.Album
import com.wrapper.spotify.model_objects.specification.Paging
import com.wrapper.spotify.model_objects.specification.PlaylistTrack
import com.wrapper.spotify.model_objects.specification.Track
import me.avo.spottit.model.TrackInPlaylist
import me.avo.spottit.util.RetrySupport
Expand All @@ -24,6 +20,8 @@ class SpotifyApiServiceImpl(private val spotifyApi: SpotifyApi) : SpotifyApiServ
spotifyApi.getTrack(id).execute()
} catch (ex: NotFoundException) {
null
} catch (ex: BadRequestException) {
null
}

override fun getAlbum(id: String): Album = spotifyApi.getAlbum(id).execute()
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/me/avo/spottit/util/RetrySupport.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.avo.spottit.util

import com.wrapper.spotify.exceptions.detailed.BadRequestException
import com.wrapper.spotify.exceptions.detailed.NotFoundException
import com.wrapper.spotify.requests.IRequest
import org.slf4j.Logger
Expand All @@ -26,7 +27,7 @@ inline fun <T> RetrySupport.retry(block: () -> T): T {
return block()
}
catch (ex: Exception) {
if (ex is NotFoundException) {
if (ex is NotFoundException || ex is BadRequestException) {
throw ex // should be handled by call site
}
logger.error("Exception encountered in retry code.", ex)
Expand Down

0 comments on commit 276a9e4

Please sign in to comment.