Skip to content

Commit

Permalink
Stop creating new http client for each request
Browse files Browse the repository at this point in the history
  • Loading branch information
stevesoltys committed Sep 8, 2020
1 parent 809fcac commit 3b63eb2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/main/kotlin/com/stevesoltys/applemusic/AppleMusic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ class AppleMusic(
AppleMusicConfiguration(teamId, privateKey, keyId, storefront)
}

private val retrofitBuilder: AppleMusicRetrofitBuilder by lazy {
AppleMusicRetrofitBuilder()
}

private var currentToken: AppleMusicAuthToken? = null

private val appleMusicService: AppleMusicService
get() {
return AppleMusicRetrofitBuilder()
return retrofitBuilder
.build(configuration, token().toString())
.create(AppleMusicService::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import retrofit2.converter.jackson.JacksonConverterFactory
*/
class AppleMusicRetrofitBuilder {

fun build(configuration: AppleMusicConfiguration, token: String): Retrofit {
val objectMapper = AppleMusicObjectMapper()
private val objectMapper = AppleMusicObjectMapper()

val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val newRequest: Request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer $token")
.build()
private val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val newRequest: Request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer $token")
.build()

chain.proceed(newRequest)
}
.build()
chain.proceed(newRequest)
}
.build()

private lateinit var token: String

fun build(configuration: AppleMusicConfiguration, token: String): Retrofit {
this.token = token

return Retrofit.Builder()
.baseUrl("https://api.music.apple.com/v1/catalog/${configuration.storefront}/")
Expand Down

0 comments on commit 3b63eb2

Please sign in to comment.