Skip to content

Commit 78458cd

Browse files
Merge branch 'main' into test-ready-when-polling-disabled
2 parents 7ef97e8 + 06cc4d8 commit 78458cd

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

unleashandroidsdk/src/main/java/io/getunleash/android/UnleashConfig.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.getunleash.android
22

33
import io.getunleash.android.backup.LocalStorageConfig
44
import io.getunleash.android.data.DataStrategy
5+
import okhttp3.OkHttpClient
56
import java.util.UUID
67

78
/**
@@ -11,6 +12,7 @@ import java.util.UUID
1112
* @property appName: name of the underlying application. Will be used as default in the [io.getunleash.android.data.UnleashContext] call (Required).
1213
* @property pollingStrategy How to poll for features. (Optional - Defaults to [io.getunleash.android.data.DataStrategy] with poll interval set to 60 seconds).
1314
* @property metricsStrategy How to poll for metrics. (Optional - Defaults to [io.getunleash.android.data.DataStrategy] with poll interval set to 60 seconds).
15+
* @property httpClient Custom http client to be used. (Optional - Use if you want to pass in custom SSL certificates).
1416
*/
1517
data class UnleashConfig(
1618
val proxyUrl: String?,
@@ -24,7 +26,8 @@ data class UnleashConfig(
2426
pauseOnBackground = true,
2527
),
2628
val delayedInitialization: Boolean = true,
27-
val forceImpressionData: Boolean = false
29+
val forceImpressionData: Boolean = false,
30+
val httpClient: OkHttpClient? = null,
2831
) {
2932
companion object {
3033
val instanceId: String = UUID.randomUUID().toString()
@@ -62,6 +65,7 @@ data class UnleashConfig(
6265
.newBuilder(parent = this)
6366
val localStorageConfig: LocalStorageConfig.Builder = LocalStorageConfig()
6467
.newBuilder(parent = this)
68+
var httpClient: OkHttpClient? = null
6569
fun build(): UnleashConfig {
6670
if ((proxyUrl == null || clientKey == null) && (pollingStrategy.enabled || metricsStrategy.enabled)) {
6771
throw IllegalStateException("You must either set proxyUrl and clientKey or disable both polling and metrics.")
@@ -74,7 +78,8 @@ data class UnleashConfig(
7478
metricsStrategy = metricsStrategy.build(),
7579
delayedInitialization = delayedInitialization,
7680
forceImpressionData = forceImpressionData,
77-
localStorageConfig = localStorageConfig.build()
81+
localStorageConfig = localStorageConfig.build(),
82+
httpClient = httpClient
7883
)
7984
}
8085

@@ -86,5 +91,9 @@ data class UnleashConfig(
8691

8792
fun forceImpressionData(forceImpressionData: Boolean) =
8893
apply { this.forceImpressionData = forceImpressionData }
94+
95+
fun httpClient(httpClient: OkHttpClient) = apply {
96+
this.httpClient = httpClient
97+
}
8998
}
9099
}

unleashandroidsdk/src/main/java/io/getunleash/android/data/DataStrategy.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.getunleash.android.data
22

33
import io.getunleash.android.UnleashConfig
4+
import okhttp3.OkHttpClient
45

56
/**
67
* @property enabled Whether the strategy is enabled or not. (Optional - Defaults to true)
78
* @property interval How often to perform the operation in milliseconds. (Optional - Defaults to 60000)
89
* @property delay How long to wait before starting the operation in milliseconds. (Optional - Defaults to 0)
910
* @property pauseOnBackground Whether the operation should pause when the app is in background. (Optional - Defaults to true)
1011
* @property httpReadTimeout How long to wait for HTTP reads in milliseconds. (Optional - Defaults to 5000)
12+
* @property httpWriteTimeout How long to wait for HTTP writes in milliseconds. (Optional - Defaults to 5000)
1113
* @property httpConnectionTimeout How long to wait for HTTP connection in milliseconds. (Optional - Defaults to 2000)
1214
* @property httpCacheSize Disk space (in bytes) set aside for http cache. (Optional - Defaults to 10MB)
1315
* @property httpCustomHeaders Enables users to override httpCustomHeaders. (Optional - Defaults to empty)
@@ -19,6 +21,7 @@ data class DataStrategy(
1921
val pauseOnBackground: Boolean = true,
2022
val httpConnectionTimeout: Long = 2000,
2123
val httpReadTimeout: Long = 5000,
24+
val httpWriteTimeout: Long = 5000,
2225
val httpCacheSize: Long = 1024 * 1024 * 10,
2326
val httpCustomHeaders: Map<String, String> = emptyMap(),
2427
) {

unleashandroidsdk/src/main/java/io/getunleash/android/http/ClientBuilder.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ class ClientBuilder(private val unleashConfig: UnleashConfig, private val androi
1313
clientName: String,
1414
strategy: DataStrategy
1515
): OkHttpClient {
16-
val builder = OkHttpClient.Builder()
16+
// either use the provided client or create a new one
17+
val clientBuilder = unleashConfig.httpClient?.newBuilder() ?: OkHttpClient.Builder()
18+
val builder = clientBuilder
1719
.readTimeout(strategy.httpReadTimeout, TimeUnit.MILLISECONDS)
20+
.writeTimeout(strategy.httpWriteTimeout, TimeUnit.MILLISECONDS)
1821
.connectTimeout(strategy.httpConnectionTimeout, TimeUnit.MILLISECONDS)
1922
if (unleashConfig.localStorageConfig.enabled) {
2023
builder.cache(

unleashandroidsdk/src/test/java/io/getunleash/android/http/ClientBuilderTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ package io.getunleash.android.http
33
import android.content.Context
44
import io.getunleash.android.BaseTest
55
import io.getunleash.android.UnleashConfig
6+
import okhttp3.OkHttpClient
67
import org.assertj.core.api.Assertions.assertThat
78
import org.junit.Test
9+
import org.mockito.ArgumentMatchers.any
10+
import org.mockito.ArgumentMatchers.anyLong
11+
import org.mockito.ArgumentMatchers.eq
812
import org.mockito.Mockito.mock
13+
import org.mockito.Mockito.spy
14+
import org.mockito.Mockito.verify
15+
import org.mockito.Mockito.`when`
16+
import java.util.concurrent.TimeUnit
917
import kotlin.io.path.createTempDirectory
1018

1119
class ClientBuilderTest : BaseTest() {
@@ -38,4 +46,24 @@ class ClientBuilderTest : BaseTest() {
3846

3947
assertThat(client.cache).isNull()
4048
}
49+
50+
@Test
51+
fun `when http client is provided by customer we should use it`() {
52+
val builder = spy(OkHttpClient.Builder())
53+
val customHttpClient = mock(OkHttpClient::class.java)
54+
`when`(customHttpClient.newBuilder()).thenReturn(builder)
55+
val config =
56+
UnleashConfig.newBuilder("my-app")
57+
.proxyUrl("https://localhost:4242/proxy")
58+
.httpClient(customHttpClient)
59+
.localStorageConfig.enabled(false)
60+
.clientKey("some-key").build()
61+
62+
val clientBuilder = ClientBuilder(config, mock(Context::class.java))
63+
clientBuilder.build("clientName", config.pollingStrategy)
64+
65+
verify(builder).readTimeout(5000, TimeUnit.MILLISECONDS)
66+
verify(builder).writeTimeout(5000, TimeUnit.MILLISECONDS)
67+
verify(builder).connectTimeout(2000, TimeUnit.MILLISECONDS)
68+
}
4169
}

0 commit comments

Comments
 (0)