Add optional logging to SubsonicAPI.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-07-22 22:49:14 +02:00
parent 24d68fcf3c
commit 20d95ce19d
3 changed files with 14 additions and 3 deletions

View File

@ -30,11 +30,12 @@ ext.androidSupport = [
] ]
ext.other = [ ext.other = [
kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib-common:$versions.kotlin", kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib-common:$versions.kotlin",
retrofit : "com.squareup.retrofit2:retrofit:$versions.retrofit", retrofit : "com.squareup.retrofit2:retrofit:$versions.retrofit",
gsonConverter : "com.squareup.retrofit2:converter-gson:$versions.retrofit", gsonConverter : "com.squareup.retrofit2:converter-gson:$versions.retrofit",
jacksonConverter : "com.squareup.retrofit2:converter-jackson:$versions.retrofit", jacksonConverter : "com.squareup.retrofit2:converter-jackson:$versions.retrofit",
jacksonKotlin : "com.fasterxml.jackson.module:jackson-module-kotlin:$versions.jackson", jacksonKotlin : "com.fasterxml.jackson.module:jackson-module-kotlin:$versions.jackson",
okhttpLogging : "com.squareup.okhttp3:logging-interceptor:$versions.okhttp",
] ]
ext.testing = [ ext.testing = [

View File

@ -12,6 +12,7 @@ dependencies {
compile other.retrofit compile other.retrofit
compile other.jacksonConverter compile other.jacksonConverter
compile other.jacksonKotlin compile other.jacksonKotlin
compile other.okhttpLogging
testCompile testing.junit testCompile testing.junit
testCompile testing.kotlinJunit testCompile testing.kotlinJunit

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.KotlinModule
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory import retrofit2.converter.jackson.JacksonConverterFactory
import java.math.BigInteger import java.math.BigInteger
@ -17,7 +18,8 @@ class SubsonicAPI(baseUrl: String,
username: String, username: String,
private val password: String, private val password: String,
clientProtocolVersion: SubsonicAPIVersions, clientProtocolVersion: SubsonicAPIVersions,
clientID: String) { clientID: String,
debug: Boolean = false) {
private val okHttpClient = OkHttpClient.Builder() private val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain -> .addInterceptor { chain ->
// Adds default request params // Adds default request params
@ -30,6 +32,13 @@ class SubsonicAPI(baseUrl: String,
.addQueryParameter("f", "json") .addQueryParameter("f", "json")
.build() .build()
chain.proceed(originalRequest.newBuilder().url(newUrl).build()) chain.proceed(originalRequest.newBuilder().url(newUrl).build())
}
.apply {
if (debug) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
this.addInterceptor(loggingInterceptor)
}
}.build() }.build()
private val jacksonMapper = ObjectMapper() private val jacksonMapper = ObjectMapper()
@ -49,7 +58,7 @@ class SubsonicAPI(baseUrl: String,
* *
* @return initialized API instance * @return initialized API instance
*/ */
fun getApi() = subsonicAPI fun getApi(): SubsonicAPIDefinition = subsonicAPI
private fun passwordHex() = "enc:${password.toHexBytes()}" private fun passwordHex() = "enc:${password.toHexBytes()}"