Some more code cleaning.

This commit is contained in:
Amine 2017-06-14 21:26:26 +02:00
parent 493b1b12b3
commit cafba196cf

View File

@ -19,7 +19,6 @@ import retrofit2.converter.gson.GsonConverterFactory
import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.Config
// codebeat:disable[ARITY,TOO_MANY_FUNCTIONS] // codebeat:disable[ARITY,TOO_MANY_FUNCTIONS]
class SelfossApi(c: Context) { class SelfossApi(c: Context) {
@ -28,27 +27,31 @@ class SelfossApi(c: Context) {
private val userName: String private val userName: String
private val password: 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<String, CachingAuthenticator>() val authCache = ConcurrentHashMap<String, CachingAuthenticator>()
return OkHttpClient
.Builder()
.authenticator(CachingAuthenticatorDecorator(this, authCache))
.addInterceptor(AuthenticationCacheInterceptor(authCache))
.build()
}
val httpUserName = config.httpUserLogin
val httpPassword = config.httpUserPassword init {
val credentials = Credentials(httpUserName, httpPassword)
userName = config.userLogin userName = config.userLogin
password = config.userPassword password = config.userPassword
val authenticator = DispatchingAuthenticator.Builder() val authenticator =
.with("digest", DigestAuthenticator(credentials)) Credentials(
.with("basic", BasicAuthenticator(credentials)) config.httpUserLogin,
.build() config.httpUserPassword
).createAuthenticator()
val client =
OkHttpClient
.Builder()
.authenticator(CachingAuthenticatorDecorator(authenticator, authCache))
.addInterceptor(AuthenticationCacheInterceptor(authCache))
.build()
val gson = val gson =
GsonBuilder() GsonBuilder()
@ -56,12 +59,11 @@ class SelfossApi(c: Context) {
.setLenient() .setLenient()
.create() .create()
val retrofit = val retrofit =
Retrofit Retrofit
.Builder() .Builder()
.baseUrl(config.baseUrl) .baseUrl(config.baseUrl)
.client(client) .client(authenticator.getHttpClien())
.addConverterFactory(GsonConverterFactory.create(gson)) .addConverterFactory(GsonConverterFactory.create(gson))
.build() .build()
service = retrofit.create(SelfossService::class.java) service = retrofit.create(SelfossService::class.java)
@ -119,4 +121,5 @@ class SelfossApi(c: Context) {
service.createSource(title, url, spout, tags, filter, userName, password) service.createSource(title, url, spout, tags, filter, userName, password)
} }
// codebeat:enable[ARITY,TOO_MANY_FUNCTIONS] // codebeat:enable[ARITY,TOO_MANY_FUNCTIONS]