Skip to content

Commit 17b58d7

Browse files
Spotless
1 parent 13a4625 commit 17b58d7

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ fun StreamClient(
260260
addInterceptor(StreamOkHttpInterceptors.clientInfo(clientInfoHeader))
261261
addInterceptor(StreamOkHttpInterceptors.apiKey(apiKey))
262262
addInterceptor(StreamOkHttpInterceptors.connectionId(connectionIdHolder))
263-
addInterceptor(StreamOkHttpInterceptors.auth( "jwt", tokenManager, compositeSerialization))
263+
addInterceptor(
264+
StreamOkHttpInterceptors.auth("jwt", tokenManager, compositeSerialization)
265+
)
264266
addInterceptor(StreamOkHttpInterceptors.error(compositeSerialization))
265267
}
266268
}
267-
configuredInterceptors.forEach {
268-
httpBuilder.addInterceptor(it)
269-
}
269+
configuredInterceptors.forEach { httpBuilder.addInterceptor(it) }
270270
}
271271

272272
return StreamClientImpl(

stream-android-core/src/main/java/io/getstream/android/core/api/model/config/StreamClientSerializationConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package io.getstream.android.core.api.model.config
1717

1818
import io.getstream.android.core.annotations.StreamCoreApi
1919
import io.getstream.android.core.api.model.event.StreamClientWsEvent
20-
import io.getstream.android.core.api.serialization.StreamJsonSerialization
2120
import io.getstream.android.core.api.serialization.StreamEventSerialization
21+
import io.getstream.android.core.api.serialization.StreamJsonSerialization
2222

2323
/**
2424
* Configuration for serialization and deserialization in the Stream client.

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ interface StreamEventSerialization<T> {
4949
fun deserialize(raw: String): Result<T>
5050
}
5151

52-
53-
5452
/**
55-
* Creates a new [StreamEventSerialization] instance that can serialize [io.getstream.android.core.api.model.event.StreamClientWsEvent] objects.
53+
* Creates a new [StreamEventSerialization] instance that can serialize
54+
* [io.getstream.android.core.api.model.event.StreamClientWsEvent] objects.
5655
*
5756
* @param jsonParser The [StreamJsonSerialization] to use for JSON serialization and
5857
* deserialization.

stream-android-core/src/main/java/io/getstream/android/core/internal/http/interceptor/StreamAuthInterceptor.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ internal class StreamAuthInterceptor(
6161
}
6262

6363
override fun intercept(chain: Interceptor.Chain): Response {
64-
val token = runBlocking { tokenManager.loadIfAbsent() }.getOrEndpointException("Failed to load token.")
64+
val token =
65+
runBlocking { tokenManager.loadIfAbsent() }
66+
.getOrEndpointException("Failed to load token.")
6567
val original = chain.request()
6668
val authed = original.withAuthHeaders(authType, token.rawValue)
6769

6870
val first = chain.proceed(authed)
69-
if (first.isSuccessful) return first
71+
if (first.isSuccessful) {
72+
return first
73+
}
7074

7175
// Peek only; do NOT consume
7276
val peeked = first.peekBody(PEEK_ERROR_BYTES_MAX).string()
@@ -82,11 +86,16 @@ internal class StreamAuthInterceptor(
8286
// refresh & retry once
8387
first.close()
8488
tokenManager.invalidate().getOrEndpointException("Failed to invalidate token")
85-
val refreshed = runBlocking { tokenManager.refresh() }
86-
.getOrEndpointException("Failed to refresh token")
89+
val refreshed =
90+
runBlocking { tokenManager.refresh() }
91+
.getOrEndpointException("Failed to refresh token")
8792

88-
val retried = original.withAuthHeaders(authType, refreshed.rawValue)
89-
.newBuilder().header(HEADER_RETRIED_ON_AUTH, "present").build()
93+
val retried =
94+
original
95+
.withAuthHeaders(authType, refreshed.rawValue)
96+
.newBuilder()
97+
.header(HEADER_RETRIED_ON_AUTH, "present")
98+
.build()
9099

91100
return chain.proceed(retried) // pass result (ok or error) downstream
92101
}
@@ -100,7 +109,6 @@ internal class StreamAuthInterceptor(
100109
}
101110
}
102111

103-
104112
private fun Request.withAuthHeaders(authType: String, bearer: String): Request =
105113
newBuilder()
106114
.addHeader(HEADER_STREAM_AUTH_TYPE, authType)

stream-android-core/src/test/java/io/getstream/android/core/internal/http/interceptor/StreamAuthInterceptorTest.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package io.getstream.android.core.internal.http.interceptor
1717

1818
import io.getstream.android.core.api.authentication.StreamTokenManager
1919
import io.getstream.android.core.api.model.exceptions.StreamEndpointErrorData
20-
import io.getstream.android.core.api.model.exceptions.StreamEndpointException
2120
import io.getstream.android.core.api.model.value.StreamToken
2221
import io.getstream.android.core.api.serialization.StreamJsonSerialization
2322
import io.mockk.MockKAnnotations
@@ -28,7 +27,6 @@ import io.mockk.impl.annotations.MockK
2827
import io.mockk.verify
2928
import java.util.concurrent.TimeUnit
3029
import kotlin.test.assertEquals
31-
import kotlin.test.assertFailsWith
3230
import kotlin.test.assertTrue
3331
import okhttp3.Interceptor
3432
import okhttp3.OkHttpClient
@@ -196,7 +194,7 @@ class StreamAuthInterceptorTest {
196194
// Proper token error code handled by this interceptor
197195
val tokenError = tokenErrorData(40)
198196
every { json.fromJson(any(), StreamEndpointErrorData::class.java) } returns
199-
Result.success(tokenError)
197+
Result.success(tokenError)
200198

201199
val interceptor = StreamAuthInterceptor(tokenManager, json, authType = "jwt")
202200
val client = client(interceptor)
@@ -205,15 +203,18 @@ class StreamAuthInterceptorTest {
205203

206204
val url = server.url("/v1/protected")
207205

208-
client.newCall(
209-
Request.Builder()
210-
.url(url)
211-
.header("x-stream-retried-on-auth", "present") // simulate already retried
212-
.build()
213-
).execute().use { resp ->
214-
assertFalse(resp.isSuccessful) // pass-through, no exception here
215-
assertEquals(401, resp.code)
216-
}
206+
client
207+
.newCall(
208+
Request.Builder()
209+
.url(url)
210+
.header("x-stream-retried-on-auth", "present") // simulate already retried
211+
.build()
212+
)
213+
.execute()
214+
.use { resp ->
215+
assertFalse(resp.isSuccessful) // pass-through, no exception here
216+
assertEquals(401, resp.code)
217+
}
217218

218219
val first = server.takeRequest(2, TimeUnit.SECONDS)
219220
kotlin.test.assertNotNull(first)
@@ -225,9 +226,7 @@ class StreamAuthInterceptorTest {
225226
coVerify(exactly = 0) { tokenManager.refresh() }
226227
}
227228

228-
/**
229-
* Non-token error codes are NOT handled here; pass response through without retry.
230-
*/
229+
/** Non-token error codes are NOT handled here; pass response through without retry. */
231230
@Test
232231
fun `non-token error passes through without retry`() {
233232
val token = streamToken("t1")
@@ -236,7 +235,7 @@ class StreamAuthInterceptorTest {
236235
// e.g., business error code that is not 40/41/42
237236
val nonTokenError = tokenErrorData(13)
238237
every { json.fromJson(any(), StreamEndpointErrorData::class.java) } returns
239-
Result.success(nonTokenError)
238+
Result.success(nonTokenError)
240239

241240
val interceptor = StreamAuthInterceptor(tokenManager, json, authType = "jwt")
242241
val client = client(interceptor)
@@ -258,16 +257,14 @@ class StreamAuthInterceptorTest {
258257
coVerify(exactly = 0) { tokenManager.refresh() }
259258
}
260259

261-
/**
262-
* If the error body cannot be parsed into StreamEndpointErrorData, pass through.
263-
*/
260+
/** If the error body cannot be parsed into StreamEndpointErrorData, pass through. */
264261
@Test
265262
fun `unparsable error body passes through without retry`() {
266263
val token = streamToken("t1")
267264
coEvery { tokenManager.loadIfAbsent() } returns Result.success(token)
268265

269266
every { json.fromJson(any(), StreamEndpointErrorData::class.java) } returns
270-
Result.failure(IllegalStateException("bad json"))
267+
Result.failure(IllegalStateException("bad json"))
271268

272269
val interceptor = StreamAuthInterceptor(tokenManager, json, authType = "jwt")
273270
val client = client(interceptor)
@@ -291,7 +288,6 @@ class StreamAuthInterceptorTest {
291288
coVerify(exactly = 0) { tokenManager.refresh() }
292289
}
293290

294-
295291
// ----------------- Helpers -----------------
296292

297293
private fun client(interceptor: Interceptor): OkHttpClient =

0 commit comments

Comments
 (0)