From cafba196cf1901fb7e789fa29cab13f0e968dd3f Mon Sep 17 00:00:00 2001 From: Amine Date: Wed, 14 Jun 2017 21:26:26 +0200 Subject: [PATCH] Some more code cleaning. --- .../api/selfoss/SelfossApi.kt | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index 035accc..be6e98e 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -19,7 +19,6 @@ import retrofit2.converter.gson.GsonConverterFactory import apps.amine.bou.readerforselfoss.utils.Config - // codebeat:disable[ARITY,TOO_MANY_FUNCTIONS] class SelfossApi(c: Context) { @@ -28,27 +27,31 @@ class SelfossApi(c: Context) { private val userName: String private val password: String - init { + fun Credentials.createAuthenticator(): DispatchingAuthenticator = + DispatchingAuthenticator.Builder() + .with("digest", DigestAuthenticator(this)) + .with("basic", BasicAuthenticator(this)) + .build() + fun DispatchingAuthenticator.getHttpClien(): OkHttpClient { val authCache = ConcurrentHashMap() + return OkHttpClient + .Builder() + .authenticator(CachingAuthenticatorDecorator(this, authCache)) + .addInterceptor(AuthenticationCacheInterceptor(authCache)) + .build() + } - val httpUserName = config.httpUserLogin - val httpPassword = config.httpUserPassword - val credentials = Credentials(httpUserName, httpPassword) + + init { userName = config.userLogin password = config.userPassword - val authenticator = DispatchingAuthenticator.Builder() - .with("digest", DigestAuthenticator(credentials)) - .with("basic", BasicAuthenticator(credentials)) - .build() - - val client = - OkHttpClient - .Builder() - .authenticator(CachingAuthenticatorDecorator(authenticator, authCache)) - .addInterceptor(AuthenticationCacheInterceptor(authCache)) - .build() + val authenticator = + Credentials( + config.httpUserLogin, + config.httpUserPassword + ).createAuthenticator() val gson = GsonBuilder() @@ -56,12 +59,11 @@ class SelfossApi(c: Context) { .setLenient() .create() - val retrofit = Retrofit .Builder() .baseUrl(config.baseUrl) - .client(client) + .client(authenticator.getHttpClien()) .addConverterFactory(GsonConverterFactory.create(gson)) .build() service = retrofit.create(SelfossService::class.java) @@ -119,4 +121,5 @@ class SelfossApi(c: Context) { service.createSource(title, url, spout, tags, filter, userName, password) } + // codebeat:enable[ARITY,TOO_MANY_FUNCTIONS] \ No newline at end of file