Update after Ganfra's review, and kotlinification

This commit is contained in:
Benoit Marty 2021-03-22 16:36:44 +01:00
parent 7db1d81eb6
commit 21cff9a749
5 changed files with 44 additions and 33 deletions

View File

@ -25,11 +25,12 @@ import org.matrix.android.sdk.BuildConfig
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.HomeServerHistoryService
import org.matrix.android.sdk.api.legacy.LegacySessionImporter
import org.matrix.android.sdk.api.network.ApiInterceptor
import org.matrix.android.sdk.api.network.ApiInterceptorListener
import org.matrix.android.sdk.api.network.ApiPath
import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.internal.SessionManager
import org.matrix.android.sdk.internal.di.DaggerMatrixComponent
import org.matrix.android.sdk.internal.network.ApiInterceptor
import org.matrix.android.sdk.internal.network.UserAgentHolder
import org.matrix.android.sdk.internal.util.BackgroundDetectionObserver
import org.matrix.olm.OlmManager
@ -76,7 +77,7 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo
return legacySessionImporter
}
fun registerApiInterceptorListener(path: ApiPath, listener: ApiInterceptor.Listener) {
fun registerApiInterceptorListener(path: ApiPath, listener: ApiInterceptorListener) {
apiInterceptor.addListener(path, listener)
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.api.network
interface ApiInterceptorListener {
fun onApiResponse(path: ApiPath, response: String)
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -28,7 +28,7 @@ import org.matrix.android.sdk.internal.network.interceptors.CurlLoggingIntercept
import org.matrix.android.sdk.internal.network.interceptors.FormattedJsonHttpLogger
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.matrix.android.sdk.api.network.ApiInterceptor
import org.matrix.android.sdk.internal.network.ApiInterceptor
import java.util.concurrent.TimeUnit
@Module
@ -55,13 +55,6 @@ internal object NetworkModule {
return CurlLoggingInterceptor()
}
@MatrixScope
@Provides
@JvmStatic
fun providesApiInterceptor(): ApiInterceptor {
return ApiInterceptor()
}
@MatrixScope
@Provides
@JvmStatic

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,29 +14,28 @@
* limitations under the License.
*/
package org.matrix.android.sdk.api.network
package org.matrix.android.sdk.internal.network
import okhttp3.Interceptor
import okhttp3.Response
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.network.ApiInterceptorListener
import org.matrix.android.sdk.api.network.ApiPath
import org.matrix.android.sdk.internal.di.MatrixScope
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
/**
* Interceptor class for provided api paths.
*/
@Singleton
class ApiInterceptor @Inject constructor() : Interceptor {
interface Listener {
fun onApiResponse(path: ApiPath, response: String)
}
@MatrixScope
internal class ApiInterceptor @Inject constructor() : Interceptor {
init {
Timber.d("ApiInterceptor.init")
}
private val apiResponseListenersMap = mutableMapOf<ApiPath, MutableList<Listener>>()
private val apiResponseListenersMap = mutableMapOf<ApiPath, MutableList<ApiInterceptorListener>>()
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
@ -46,9 +45,11 @@ class ApiInterceptor @Inject constructor() : Interceptor {
val response = chain.proceed(request)
findApiPath(path, method)?.let { apiPath ->
response.peekBody(Long.MAX_VALUE).string().let {
response.peekBody(Long.MAX_VALUE).string().let { networkResponse ->
apiResponseListenersMap[apiPath]?.forEach { listener ->
listener.onApiResponse(apiPath, it)
tryOrNull("Error in the implementation") {
listener.onApiResponse(apiPath, networkResponse)
}
}
}
}
@ -70,21 +71,16 @@ class ApiInterceptor @Inject constructor() : Interceptor {
if (patternSegments.size != pathSegments.size) return false
for (i in patternSegments.indices) {
if (patternSegments[i] != pathSegments[i] && !patternSegments[i].startsWith("{")) {
return false
}
return patternSegments.indices.all { i ->
patternSegments[i] == pathSegments[i] || patternSegments[i].startsWith("{")
}
return true
}
/**
* Adds listener to send intercepted api responses through.
*/
fun addListener(path: ApiPath, listener: Listener) {
if (!apiResponseListenersMap.contains(path)) {
apiResponseListenersMap[path] = mutableListOf()
}
apiResponseListenersMap[path]?.add(listener)
fun addListener(path: ApiPath, listener: ApiInterceptorListener) {
apiResponseListenersMap.getOrPut(path) { mutableListOf() }
.add(listener)
}
}