From 2f54b7b13459b01a98615f027fa04d5240d68ad9 Mon Sep 17 00:00:00 2001 From: sangeetds Date: Sat, 29 May 2021 08:12:03 +0530 Subject: [PATCH] Autowire fixes. --- .../kotlin/com/isdb/IsdbBackendApplication.kt | 3 ++ .../kotlin/com/isdb/config/SpotifyDetails.kt | 8 ++-- .../com/isdb/service/impl/SongServiceImpl.kt | 6 +-- .../kotlin/com/isdb/spotify/SpotifyAPI.kt | 9 ++-- .../kotlin/com/isdb/utils/RetrofitAdapter.kt | 47 +++++++++---------- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/com/isdb/IsdbBackendApplication.kt b/src/main/kotlin/com/isdb/IsdbBackendApplication.kt index b435759..bb02d64 100644 --- a/src/main/kotlin/com/isdb/IsdbBackendApplication.kt +++ b/src/main/kotlin/com/isdb/IsdbBackendApplication.kt @@ -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) { diff --git a/src/main/kotlin/com/isdb/config/SpotifyDetails.kt b/src/main/kotlin/com/isdb/config/SpotifyDetails.kt index 0f8bf70..c452fc6 100644 --- a/src/main/kotlin/com/isdb/config/SpotifyDetails.kt +++ b/src/main/kotlin/com/isdb/config/SpotifyDetails.kt @@ -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, ) \ No newline at end of file diff --git a/src/main/kotlin/com/isdb/service/impl/SongServiceImpl.kt b/src/main/kotlin/com/isdb/service/impl/SongServiceImpl.kt index 5237955..f7d8655 100644 --- a/src/main/kotlin/com/isdb/service/impl/SongServiceImpl.kt +++ b/src/main/kotlin/com/isdb/service/impl/SongServiceImpl.kt @@ -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 { diff --git a/src/main/kotlin/com/isdb/spotify/SpotifyAPI.kt b/src/main/kotlin/com/isdb/spotify/SpotifyAPI.kt index 57f4476..051b16a 100644 --- a/src/main/kotlin/com/isdb/spotify/SpotifyAPI.kt +++ b/src/main/kotlin/com/isdb/spotify/SpotifyAPI.kt @@ -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 { @@ -33,7 +34,7 @@ class SpotifyAPI { fun getTracksWithQuery(track: String): List { 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") @@ -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) diff --git a/src/main/kotlin/com/isdb/utils/RetrofitAdapter.kt b/src/main/kotlin/com/isdb/utils/RetrofitAdapter.kt index a4c59f5..58c68de 100644 --- a/src/main/kotlin/com/isdb/utils/RetrofitAdapter.kt +++ b/src/main/kotlin/com/isdb/utils/RetrofitAdapter.kt @@ -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) } } \ No newline at end of file