Skip to content

Commit 6e851dd

Browse files
authored
Callback to receive raw response body from featurevisor data request (#26)
1 parent 3323152 commit 6e851dd

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ import java.lang.IllegalArgumentException
1313
internal fun FeaturevisorInstance.fetchDatafileContent(
1414
url: String,
1515
handleDatafileFetch: DatafileFetchHandler? = null,
16+
rawResponseReady: (ResponseBody) -> Unit,
1617
completion: (Result<DatafileContent>) -> Unit,
1718
) {
1819
handleDatafileFetch?.let { handleFetch ->
1920
val result = handleFetch(url)
2021
completion(result)
2122
} ?: run {
22-
fetchDatafileContentFromUrl(url, completion)
23+
fetchDatafileContentFromUrl(url, rawResponseReady, completion)
2324
}
2425
}
2526

2627
private fun fetchDatafileContentFromUrl(
2728
url: String,
29+
rawResponseReady: (ResponseBody) -> Unit,
2830
completion: (Result<DatafileContent>) -> Unit,
2931
) {
3032
try {
@@ -34,7 +36,7 @@ private fun fetchDatafileContentFromUrl(
3436
.addHeader("Content-Type", "application/json")
3537
.build()
3638

37-
fetch(request, completion)
39+
fetch(request, rawResponseReady, completion)
3840
} catch (throwable: IllegalArgumentException) {
3941
completion(Result.failure(FeaturevisorError.InvalidUrl(url)))
4042
}
@@ -43,13 +45,15 @@ private fun fetchDatafileContentFromUrl(
4345
const val BODY_BYTE_COUNT = 1000000L
4446
private inline fun fetch(
4547
request: Request,
48+
crossinline rawResponseReady: (ResponseBody) -> Unit,
4649
crossinline completion: (Result<DatafileContent>) -> Unit,
4750
) {
4851
val client = OkHttpClient()
4952
val call = client.newCall(request)
5053
call.enqueue(object : Callback {
5154
override fun onResponse(call: Call, response: Response) {
5255
val responseBody = response.peekBody(BODY_BYTE_COUNT)
56+
rawResponseReady(responseBody)
5357
if (response.isSuccessful) {
5458
val json = Json {
5559
ignoreUnknownKeys = true

src/main/kotlin/com/featurevisor/sdk/Instance+Refresh.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private fun FeaturevisorInstance.refresh() {
4242
fetchDatafileContent(
4343
datafileUrl,
4444
handleDatafileFetch,
45+
rawResponseReady,
4546
) { result ->
4647

4748
if (result.isSuccess) {

src/main/kotlin/com/featurevisor/sdk/Instance.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.featurevisor.types.EventName.*
99
import kotlinx.coroutines.Job
1010
import kotlinx.serialization.decodeFromString
1111
import kotlinx.serialization.json.Json
12+
import okhttp3.ResponseBody
1213

1314
typealias ConfigureBucketKey = (Feature, Context, BucketKey) -> BucketKey
1415
typealias ConfigureBucketValue = (Feature, Context, BucketValue) -> BucketValue
@@ -56,6 +57,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
5657
internal var configureBucketKey = options.configureBucketKey
5758
internal var configureBucketValue = options.configureBucketValue
5859
internal var refreshJob: Job? = null
60+
internal var rawResponseReady: (ResponseBody) -> Unit = options.rawResponseReady
5961

6062
init {
6163
with(options) {
@@ -100,7 +102,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
100102

101103
datafileUrl != null -> {
102104
datafileReader = DatafileReader(options.datafile?: emptyDatafile)
103-
fetchDatafileContent(datafileUrl, handleDatafileFetch) { result ->
105+
fetchDatafileContent(datafileUrl, handleDatafileFetch, rawResponseReady) { result ->
104106
if (result.isSuccess) {
105107
datafileReader = DatafileReader(result.getOrThrow())
106108
statuses.ready = true

src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.featurevisor.sdk
33
import com.featurevisor.types.DatafileContent
44
import com.featurevisor.types.InitialFeatures
55
import com.featurevisor.types.StickyFeatures
6+
import okhttp3.ResponseBody
67

78
typealias Listener = (Array<out Any>) -> Unit
89

@@ -23,6 +24,7 @@ data class InstanceOptions(
2324
val onError: Listener? = null,
2425
val refreshInterval: Long? = null, // seconds
2526
val stickyFeatures: StickyFeatures? = null,
27+
val rawResponseReady: (ResponseBody) -> Unit = {},
2628
) {
2729
companion object {
2830
private const val defaultBucketKeySeparator = "."

0 commit comments

Comments
 (0)