Skip to content

Commit b0b3570

Browse files
Add a possibility to configure the HTTP client automatically
1 parent 38da541 commit b0b3570

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

stream-android-core/src/main/java/io/getstream/android/core/api/StreamClient.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package io.getstream.android.core.api
1818
import io.getstream.android.core.annotations.StreamCoreApi
1919
import io.getstream.android.core.api.authentication.StreamTokenManager
2020
import io.getstream.android.core.api.authentication.StreamTokenProvider
21+
import io.getstream.android.core.api.http.StreamOkHttpInterceptors
2122
import io.getstream.android.core.api.log.StreamLoggerProvider
2223
import io.getstream.android.core.api.model.config.StreamClientSerializationConfig
24+
import io.getstream.android.core.api.model.config.StreamHttpConfig
2325
import io.getstream.android.core.api.model.config.StreamSocketConfig
2426
import io.getstream.android.core.api.model.connection.StreamConnectedUser
2527
import io.getstream.android.core.api.model.connection.StreamConnectionState
@@ -40,6 +42,9 @@ import io.getstream.android.core.api.socket.monitor.StreamHealthMonitor
4042
import io.getstream.android.core.api.subscribe.StreamSubscription
4143
import io.getstream.android.core.api.subscribe.StreamSubscriptionManager
4244
import io.getstream.android.core.internal.client.StreamClientImpl
45+
import io.getstream.android.core.internal.http.interceptor.StreamApiKeyInterceptor
46+
import io.getstream.android.core.internal.http.interceptor.StreamAuthInterceptor
47+
import io.getstream.android.core.internal.http.interceptor.StreamConnectionIdInterceptor
4348
import io.getstream.android.core.internal.serialization.StreamCompositeEventSerializationImpl
4449
import io.getstream.android.core.internal.serialization.StreamCompositeMoshiJsonSerialization
4550
import io.getstream.android.core.internal.serialization.StreamMoshiJsonSerializationImpl
@@ -50,6 +55,7 @@ import kotlinx.coroutines.Job
5055
import kotlinx.coroutines.SupervisorJob
5156
import kotlinx.coroutines.flow.MutableStateFlow
5257
import kotlinx.coroutines.flow.StateFlow
58+
import okhttp3.OkHttpClient
5359

5460
/**
5561
* Entry point for establishing and managing a connection to Stream services.
@@ -190,6 +196,7 @@ interface StreamClient {
190196
* @param tokenManager The token manager.
191197
* @param singleFlight The single-flight processor.
192198
* @param serialQueue The serial processing queue.
199+
* @param httpConfig The HTTP configuration.
193200
* @param retryProcessor The retry processor.
194201
* @param connectionIdHolder The connection ID holder.
195202
* @param socketFactory The WebSocket factory.
@@ -218,6 +225,8 @@ fun StreamClient(
218225
socketFactory: StreamWebSocketFactory,
219226
healthMonitor: StreamHealthMonitor,
220227
batcher: StreamBatcher<String>,
228+
// Http
229+
httpConfig: StreamHttpConfig? = null,
221230
// Serialization
222231
serializationConfig: StreamClientSerializationConfig,
223232
// Logging
@@ -248,6 +257,21 @@ fun StreamClient(
248257
StreamMoshiJsonSerializationImpl(StreamCoreMoshiProvider().builder {}.build()),
249258
serializationConfig.json,
250259
)
260+
261+
httpConfig?.apply {
262+
if (automaticInterceptors) {
263+
httpBuilder.apply {
264+
addInterceptor(StreamOkHttpInterceptors.clientInfo(clientInfoHeader))
265+
addInterceptor(StreamOkHttpInterceptors.apiKey(apiKey))
266+
addInterceptor(StreamOkHttpInterceptors.auth( "jwt", tokenManager, compositeSerialization))
267+
addInterceptor(StreamOkHttpInterceptors.connectionId(connectionIdHolder))
268+
}
269+
}
270+
configuredInterceptors.forEach {
271+
httpBuilder.addInterceptor(it)
272+
}
273+
}
274+
251275
return StreamClientImpl(
252276
userId = userId,
253277
scope = clientScope,

stream-android-core/src/main/java/io/getstream/android/core/api/http/StreamOkHttpInterceptors.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package io.getstream.android.core.api.http
1818
import io.getstream.android.core.annotations.StreamCoreApi
1919
import io.getstream.android.core.api.authentication.StreamTokenManager
2020
import io.getstream.android.core.api.model.value.StreamApiKey
21+
import io.getstream.android.core.api.model.value.StreamHttpClientInfoHeader
2122
import io.getstream.android.core.api.serialization.StreamJsonSerialization
2223
import io.getstream.android.core.api.socket.StreamConnectionIdHolder
2324
import io.getstream.android.core.internal.http.interceptor.StreamApiKeyInterceptor
2425
import io.getstream.android.core.internal.http.interceptor.StreamAuthInterceptor
26+
import io.getstream.android.core.internal.http.interceptor.StreamClientInfoInterceptor
2527
import io.getstream.android.core.internal.http.interceptor.StreamConnectionIdInterceptor
2628
import io.getstream.android.core.internal.http.interceptor.StreamEndpointErrorInterceptor
2729
import okhttp3.Interceptor
@@ -60,6 +62,15 @@ object StreamOkHttpInterceptors {
6062
fun connectionId(connectionIdHolder: StreamConnectionIdHolder): Interceptor =
6163
StreamConnectionIdInterceptor(connectionIdHolder)
6264

65+
/**
66+
* Creates an OkHttp interceptor that adds the client info header to the request.
67+
*
68+
* @param clientInfoHeader The client info header to add.
69+
* @return An OkHttp interceptor.
70+
*/
71+
fun clientInfo(clientInfoHeader: StreamHttpClientInfoHeader): Interceptor =
72+
StreamClientInfoInterceptor(clientInfoHeader)
73+
6374
/**
6475
* Creates an OkHttp interceptor that adds the Stream API key to the request as a query
6576
* parameter.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.getstream.android.core.api.model.config
2+
3+
import okhttp3.Interceptor
4+
import okhttp3.OkHttpClient
5+
6+
/**
7+
* Configuration for HTTP clients in the Stream client.
8+
*
9+
* @param httpBuilder The HTTP client builder.
10+
* @param automaticInterceptors Whether to add automatic interceptors.
11+
* @param configuredInterceptors The configured interceptors.
12+
*/
13+
data class StreamHttpConfig(
14+
val httpBuilder: OkHttpClient.Builder,
15+
val automaticInterceptors: Boolean = true,
16+
val configuredInterceptors: Set<Interceptor> = emptySet()
17+
)

0 commit comments

Comments
 (0)