Skip to content

Commit

Permalink
sync retrofit and api instance, deps updated
Browse files Browse the repository at this point in the history
  • Loading branch information
KirkBushman committed Jan 28, 2020
1 parent 7a0d321 commit 36127bf
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 47 deletions.
14 changes: 8 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion ktlint.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 1 addition & 2 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kirkbushman.zammad"/>
<manifest package="com.kirkbushman.zammad" />
93 changes: 60 additions & 33 deletions lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Expand All @@ -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? {

Expand Down
31 changes: 31 additions & 0 deletions lib/src/main/java/com/kirkbushman/zammad/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -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.*

Expand All @@ -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()
}
}
3 changes: 0 additions & 3 deletions lib/src/main/res/values/strings.xml

This file was deleted.

2 changes: 1 addition & 1 deletion sampleapp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 36127bf

Please sign in to comment.