From 36127bffff7c2f4c8e7d084393fb6d9a129bc4ec Mon Sep 17 00:00:00 2001 From: luca Date: Tue, 28 Jan 2020 17:10:27 +0100 Subject: [PATCH] sync retrofit and api instance, deps updated --- build.gradle | 14 +-- ktlint.gradle | 2 +- lib/build.gradle | 2 +- lib/src/main/AndroidManifest.xml | 3 +- .../com/kirkbushman/zammad/ZammadClient.kt | 93 ++++++++++++------- .../com/kirkbushman/zammad/utils/Utils.kt | 31 +++++++ lib/src/main/res/values/strings.xml | 3 - sampleapp/build.gradle | 2 +- 8 files changed, 103 insertions(+), 47 deletions(-) delete mode 100644 lib/src/main/res/values/strings.xml diff --git a/build.gradle b/build.gradle index ad232e9..8e005cb 100644 --- a/build.gradle +++ b/build.gradle @@ -3,15 +3,17 @@ buildscript { ext { - android_gradle_ver = '3.5.2' + android_gradle_ver = '3.5.3' kotlin_ver = '1.3.61' - retrofit_ver = '2.6.2' + ktlint_ver = '0.36.0' + retrofit_ver = '2.7.1' moshi_ver = '1.9.2' - okhttp_ver = '4.2.2' - mdc_ver = '1.1.0-beta02' - epoxy_ver = '3.8.0' - leak_ver = '2.0-beta-5' + okhttp_ver = '4.3.1' + mdc_ver = '1.1.0-rc02' + epoxy_ver = '3.9.0' + junit_ver = '4.13' + leak_ver = '2.0' } repositories { google() diff --git a/ktlint.gradle b/ktlint.gradle index 0d26977..2c481c7 100644 --- a/ktlint.gradle +++ b/ktlint.gradle @@ -7,7 +7,7 @@ configurations { } dependencies { - ktlint 'com.pinterest:ktlint:0.35.0' + ktlint "com.pinterest:ktlint:$ktlint_ver" // additional 3rd party ruleset(s) can be specified here // just add them to the classpath (ktlint 'groupId:artifactId:version') and // ktlint will pick them up diff --git a/lib/build.gradle b/lib/build.gradle index 543bef4..670da11 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -36,7 +36,7 @@ kapt { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - testImplementation 'junit:junit:4.12' + testImplementation 'junit:junit:4.13' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_ver" diff --git a/lib/src/main/AndroidManifest.xml b/lib/src/main/AndroidManifest.xml index 09e5ba6..3c7b817 100644 --- a/lib/src/main/AndroidManifest.xml +++ b/lib/src/main/AndroidManifest.xml @@ -1,2 +1 @@ - + diff --git a/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt b/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt index 57af97d..e9fecb1 100644 --- a/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt +++ b/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt @@ -5,11 +5,8 @@ import android.util.Log import com.kirkbushman.zammad.models.* import com.kirkbushman.zammad.models.compat.TicketArticleCompat import com.kirkbushman.zammad.models.compat.TicketCompat -import com.squareup.moshi.Moshi -import okhttp3.OkHttpClient -import okhttp3.logging.HttpLoggingInterceptor +import com.kirkbushman.zammad.utils.Utils.buildRetrofit import retrofit2.Retrofit -import retrofit2.converter.moshi.MoshiConverterFactory class ZammadClient( @@ -24,51 +21,81 @@ class ZammadClient( companion object { - fun me(baseUrl: String, username: String, password: String, logging: Boolean): User? { + @Volatile + private var retrofit: Retrofit? = null + @Volatile + private var api: ZammadApi? = null - val auth = "$username:$password" - val authMap = hashMapOf("Authorization" to "Basic ".plus(String(Base64.encode(auth.toByteArray(), Base64.NO_WRAP)))) - val retrofit = getRetrofit(baseUrl, logging) - val api = retrofit.create(ZammadApi::class.java) - val req = api.me(authMap) - val res = req.execute() + @Synchronized + fun getRetrofit(baseUrl: String, logging: Boolean): Retrofit { + synchronized(this) { - if (!res.isSuccessful) { - return null + if (retrofit == null) { + retrofit = buildRetrofit(baseUrl, logging) + } + + return retrofit!! } + } - return res.body() + @Synchronized + fun getRetrofit(): Retrofit { + synchronized(this) { + + if (retrofit == null) { + throw IllegalStateException("Retrofit instance is null, have you called `getRetrofit(baseUrl, logging)`?") + } + + return retrofit!! + } + } + + @Synchronized + fun getApi(baseUrl: String, logging: Boolean): ZammadApi { + synchronized(this) { + + if (api == null) { + api = getRetrofit(baseUrl, logging).create(ZammadApi::class.java) + } + + return api!! + } } - private fun getRetrofit(baseUrl: String, logging: Boolean): Retrofit { + @Synchronized + fun getApi(): ZammadApi { + synchronized(this) { + + if (api == null) { - val moshi = Moshi.Builder().build() - val moshiFactory = MoshiConverterFactory.create(moshi) + if (retrofit == null) { + throw IllegalStateException("Retrofit instance is null, have you called `getRetrofit(baseUrl, logging)`?") + } - val httpClient = if (logging) { + api = getRetrofit().create(ZammadApi::class.java) + } - val logger = HttpLoggingInterceptor() - logger.level = HttpLoggingInterceptor.Level.BODY + return api!! + } + } - OkHttpClient.Builder() - .addInterceptor(logger) - .build() - } else { + fun me(baseUrl: String, username: String, password: String, logging: Boolean): User? { - OkHttpClient.Builder() - .build() + val auth = "$username:$password" + val authMap = hashMapOf("Authorization" to "Basic ".plus(String(Base64.encode(auth.toByteArray(), Base64.NO_WRAP)))) + val api = getApi(baseUrl, logging) + val req = api.me(authMap) + val res = req.execute() + + if (!res.isSuccessful) { + return null } - return Retrofit.Builder() - .baseUrl(baseUrl) - .addConverterFactory(moshiFactory) - .client(httpClient) - .build() + return res.body() } } - private val retrofit = getRetrofit(baseUrl, logging) - private val api = retrofit.create(ZammadApi::class.java) + private val api = getApi(baseUrl, logging) fun me(): User? { diff --git a/lib/src/main/java/com/kirkbushman/zammad/utils/Utils.kt b/lib/src/main/java/com/kirkbushman/zammad/utils/Utils.kt index ca5e0d0..bedeacd 100644 --- a/lib/src/main/java/com/kirkbushman/zammad/utils/Utils.kt +++ b/lib/src/main/java/com/kirkbushman/zammad/utils/Utils.kt @@ -1,5 +1,10 @@ package com.kirkbushman.zammad.utils +import com.squareup.moshi.Moshi +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import retrofit2.Retrofit +import retrofit2.converter.moshi.MoshiConverterFactory import java.text.SimpleDateFormat import java.util.* @@ -9,4 +14,30 @@ object Utils { val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) return format.parse(dateStr.replace('T', ' ').replace('Z', ' ')) } + + fun buildRetrofit(baseUrl: String, logging: Boolean): Retrofit { + + val moshi = Moshi.Builder().build() + val moshiFactory = MoshiConverterFactory.create(moshi) + + val httpClient = if (logging) { + + val logger = HttpLoggingInterceptor() + logger.level = HttpLoggingInterceptor.Level.BODY + + OkHttpClient.Builder() + .addInterceptor(logger) + .build() + } else { + + OkHttpClient.Builder() + .build() + } + + return Retrofit.Builder() + .baseUrl(baseUrl) + .addConverterFactory(moshiFactory) + .client(httpClient) + .build() + } } diff --git a/lib/src/main/res/values/strings.xml b/lib/src/main/res/values/strings.xml deleted file mode 100644 index d8b1ac9..0000000 --- a/lib/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - lib - diff --git a/sampleapp/build.gradle b/sampleapp/build.gradle index 0d3a4f5..3c2fe8c 100644 --- a/sampleapp/build.gradle +++ b/sampleapp/build.gradle @@ -43,7 +43,7 @@ dependencies { implementation project(":lib") - testImplementation 'junit:junit:4.12' + testImplementation "junit:junit:$junit_ver" androidTestImplementation 'androidx.test:runner:1.2.0' debugImplementation "com.squareup.leakcanary:leakcanary-android:$leak_ver"