Skip to content

Commit

Permalink
Autowire fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sangeetds committed May 29, 2021
1 parent b9b0c00 commit 2f54b7b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/com/isdb/IsdbBackendApplication.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.isdb

import com.isdb.config.SpotifyDetails
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication

@SpringBootApplication
@EnableConfigurationProperties(SpotifyDetails::class)
class IsdbBackendApplication

fun main(args: Array<String>) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/isdb/config/SpotifyDetails.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import org.springframework.context.annotation.Configuration
@ConstructorBinding
@ConfigurationProperties("spotify")
data class SpotifyDetails(
val clientId: String,
val clientSecret: String,
val apiAuth: String,
val apiSearch: String,
val id: String,
val secret: String,
val auth: String,
val search: String,
)
6 changes: 3 additions & 3 deletions src/main/kotlin/com/isdb/service/impl/SongServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import org.springframework.stereotype.Service
import java.util.Objects

@Service
class SongServiceImpl(
class SongServiceImpl (
@Autowired val songRepository: SongRepository,
@Autowired val userService: UserService
@Autowired val userService: UserService,
@Autowired val spotifyApi: SpotifyAPI
) : SongService {

private val spotifyApi = SpotifyAPI();
private val logger = KotlinLogging.logger {}

override fun getTracks(songName: String?, userId: String): List<SongDTO> {
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/com/isdb/spotify/SpotifyAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import com.isdb.enums.Action
import com.isdb.model.SimpleTrack
import com.isdb.utils.RetrofitAdapter
import mu.KotlinLogging
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class SpotifyAPI {
class SpotifyAPI(@Autowired val retrofitAdapter: RetrofitAdapter) {

private val logger = KotlinLogging.logger {}

private fun getAuthToken(): String {
logger.info { "Auth Token request from spotify" }
val retrofitService = RetrofitAdapter.getRetrofit(action = Action.AUTH)
val retrofitService = retrofitAdapter.getRetrofit(action = Action.AUTH)
val auth = getToken(retrofitService)

return when {
Expand All @@ -33,7 +34,7 @@ class SpotifyAPI {

fun getTracksWithQuery(track: String): List<SimpleTrack> {
val token = getAuthToken()
val retrofitService = RetrofitAdapter.getRetrofit(action = Action.SEARCH, token = token)
val retrofitService = retrofitAdapter.getRetrofit(action = Action.SEARCH, token = token)

logger.info { "Tracks requested from Spotify." }
val tracks = getTracks(service = retrofitService, trackSearchValue = track, "track")
Expand All @@ -52,7 +53,7 @@ class SpotifyAPI {

fun getUserTrack(spotifyId: String): SimpleTrack {
val token = getAuthToken()
val retrofitService = RetrofitAdapter.getRetrofit(action = Action.TRACK, token = token)
val retrofitService = retrofitAdapter.getRetrofit(action = Action.TRACK, token = token)

logger.info { "Getting track info from Spotify" }
val track = getTrack(service = retrofitService, trackSearchId = spotifyId)
Expand Down
47 changes: 22 additions & 25 deletions src/main/kotlin/com/isdb/utils/RetrofitAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,35 @@ import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import okhttp3.OkHttpClient
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

class RetrofitAdapter {
@Service
class RetrofitAdapter(@Autowired val spotifyDetails: SpotifyDetails) {

companion object {
fun getRetrofit(
action: Action,
token: String? = null
): SpotifyEndpoints {
val moshi = Moshi.Builder()
.addLast(KotlinJsonAdapterFactory())
.build()

@Autowired lateinit var spotifyDetails: SpotifyDetails
val intercept =
if (action == Action.AUTH) BasicInterceptor(
spotifyDetails.id, spotifyDetails.secret
)
else BasicInterceptor(token!!)

fun getRetrofit(
action: Action,
token: String? = null
): SpotifyEndpoints {
val moshi = Moshi.Builder()
.addLast(KotlinJsonAdapterFactory())
.build()
val httpClient = OkHttpClient.Builder().addInterceptor(intercept).build()

val intercept =
if (action == Action.AUTH) BasicInterceptor(
spotifyDetails.clientId, spotifyDetails.clientSecret
)
else BasicInterceptor(token!!)
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(if (action == Action.AUTH) spotifyDetails.auth else spotifyDetails.search)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.client(httpClient)
.build()

val httpClient = OkHttpClient.Builder().addInterceptor(intercept).build()

val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(if (action == Action.AUTH) spotifyDetails.apiAuth else spotifyDetails.apiSearch)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.client(httpClient)
.build()

return retrofit.create(SpotifyEndpoints::class.java)
}
return retrofit.create(SpotifyEndpoints::class.java)
}
}

0 comments on commit 2f54b7b

Please sign in to comment.