mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-27 12:00:03 +03:00
Update after Ganfra's review, and kotlinification
This commit is contained in:
parent
7db1d81eb6
commit
21cff9a749
5 changed files with 44 additions and 33 deletions
|
@ -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.AuthenticationService
|
||||||
import org.matrix.android.sdk.api.auth.HomeServerHistoryService
|
import org.matrix.android.sdk.api.auth.HomeServerHistoryService
|
||||||
import org.matrix.android.sdk.api.legacy.LegacySessionImporter
|
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.network.ApiPath
|
||||||
import org.matrix.android.sdk.api.raw.RawService
|
import org.matrix.android.sdk.api.raw.RawService
|
||||||
import org.matrix.android.sdk.internal.SessionManager
|
import org.matrix.android.sdk.internal.SessionManager
|
||||||
import org.matrix.android.sdk.internal.di.DaggerMatrixComponent
|
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.network.UserAgentHolder
|
||||||
import org.matrix.android.sdk.internal.util.BackgroundDetectionObserver
|
import org.matrix.android.sdk.internal.util.BackgroundDetectionObserver
|
||||||
import org.matrix.olm.OlmManager
|
import org.matrix.olm.OlmManager
|
||||||
|
@ -76,7 +77,7 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo
|
||||||
return legacySessionImporter
|
return legacySessionImporter
|
||||||
}
|
}
|
||||||
|
|
||||||
fun registerApiInterceptorListener(path: ApiPath, listener: ApiInterceptor.Listener) {
|
fun registerApiInterceptorListener(path: ApiPath, listener: ApiInterceptorListener) {
|
||||||
apiInterceptor.addListener(path, listener)
|
apiInterceptor.addListener(path, listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
|
@ -28,7 +28,7 @@ import org.matrix.android.sdk.internal.network.interceptors.CurlLoggingIntercept
|
||||||
import org.matrix.android.sdk.internal.network.interceptors.FormattedJsonHttpLogger
|
import org.matrix.android.sdk.internal.network.interceptors.FormattedJsonHttpLogger
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
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
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
|
@ -55,13 +55,6 @@ internal object NetworkModule {
|
||||||
return CurlLoggingInterceptor()
|
return CurlLoggingInterceptor()
|
||||||
}
|
}
|
||||||
|
|
||||||
@MatrixScope
|
|
||||||
@Provides
|
|
||||||
@JvmStatic
|
|
||||||
fun providesApiInterceptor(): ApiInterceptor {
|
|
||||||
return ApiInterceptor()
|
|
||||||
}
|
|
||||||
|
|
||||||
@MatrixScope
|
@MatrixScope
|
||||||
@Provides
|
@Provides
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,29 +14,28 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.matrix.android.sdk.api.network
|
package org.matrix.android.sdk.internal.network
|
||||||
|
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Response
|
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 timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interceptor class for provided api paths.
|
* Interceptor class for provided api paths.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@MatrixScope
|
||||||
class ApiInterceptor @Inject constructor() : Interceptor {
|
internal class ApiInterceptor @Inject constructor() : Interceptor {
|
||||||
|
|
||||||
interface Listener {
|
|
||||||
fun onApiResponse(path: ApiPath, response: String)
|
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
Timber.d("ApiInterceptor.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 {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request()
|
val request = chain.request()
|
||||||
|
@ -46,9 +45,11 @@ class ApiInterceptor @Inject constructor() : Interceptor {
|
||||||
val response = chain.proceed(request)
|
val response = chain.proceed(request)
|
||||||
|
|
||||||
findApiPath(path, method)?.let { apiPath ->
|
findApiPath(path, method)?.let { apiPath ->
|
||||||
response.peekBody(Long.MAX_VALUE).string().let {
|
response.peekBody(Long.MAX_VALUE).string().let { networkResponse ->
|
||||||
apiResponseListenersMap[apiPath]?.forEach { listener ->
|
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
|
if (patternSegments.size != pathSegments.size) return false
|
||||||
|
|
||||||
for (i in patternSegments.indices) {
|
return patternSegments.indices.all { i ->
|
||||||
if (patternSegments[i] != pathSegments[i] && !patternSegments[i].startsWith("{")) {
|
patternSegments[i] == pathSegments[i] || patternSegments[i].startsWith("{")
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds listener to send intercepted api responses through.
|
* Adds listener to send intercepted api responses through.
|
||||||
*/
|
*/
|
||||||
fun addListener(path: ApiPath, listener: Listener) {
|
fun addListener(path: ApiPath, listener: ApiInterceptorListener) {
|
||||||
if (!apiResponseListenersMap.contains(path)) {
|
apiResponseListenersMap.getOrPut(path) { mutableListOf() }
|
||||||
apiResponseListenersMap[path] = mutableListOf()
|
.add(listener)
|
||||||
}
|
|
||||||
apiResponseListenersMap[path]?.add(listener)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue