Skip to content

Commit

Permalink
Callback to receive raw response body from featurevisor data request (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
klauudiaa authored Apr 5, 2024
1 parent 3323152 commit 6e851dd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ import java.lang.IllegalArgumentException
internal fun FeaturevisorInstance.fetchDatafileContent(
url: String,
handleDatafileFetch: DatafileFetchHandler? = null,
rawResponseReady: (ResponseBody) -> Unit,
completion: (Result<DatafileContent>) -> Unit,
) {
handleDatafileFetch?.let { handleFetch ->
val result = handleFetch(url)
completion(result)
} ?: run {
fetchDatafileContentFromUrl(url, completion)
fetchDatafileContentFromUrl(url, rawResponseReady, completion)
}
}

private fun fetchDatafileContentFromUrl(
url: String,
rawResponseReady: (ResponseBody) -> Unit,
completion: (Result<DatafileContent>) -> Unit,
) {
try {
Expand All @@ -34,7 +36,7 @@ private fun fetchDatafileContentFromUrl(
.addHeader("Content-Type", "application/json")
.build()

fetch(request, completion)
fetch(request, rawResponseReady, completion)
} catch (throwable: IllegalArgumentException) {
completion(Result.failure(FeaturevisorError.InvalidUrl(url)))
}
Expand All @@ -43,13 +45,15 @@ private fun fetchDatafileContentFromUrl(
const val BODY_BYTE_COUNT = 1000000L
private inline fun fetch(
request: Request,
crossinline rawResponseReady: (ResponseBody) -> Unit,
crossinline completion: (Result<DatafileContent>) -> Unit,
) {
val client = OkHttpClient()
val call = client.newCall(request)
call.enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
val responseBody = response.peekBody(BODY_BYTE_COUNT)
rawResponseReady(responseBody)
if (response.isSuccessful) {
val json = Json {
ignoreUnknownKeys = true
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Refresh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private fun FeaturevisorInstance.refresh() {
fetchDatafileContent(
datafileUrl,
handleDatafileFetch,
rawResponseReady,
) { result ->

if (result.isSuccess) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.featurevisor.types.EventName.*
import kotlinx.coroutines.Job
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import okhttp3.ResponseBody

typealias ConfigureBucketKey = (Feature, Context, BucketKey) -> BucketKey
typealias ConfigureBucketValue = (Feature, Context, BucketValue) -> BucketValue
Expand Down Expand Up @@ -56,6 +57,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
internal var configureBucketKey = options.configureBucketKey
internal var configureBucketValue = options.configureBucketValue
internal var refreshJob: Job? = null
internal var rawResponseReady: (ResponseBody) -> Unit = options.rawResponseReady

init {
with(options) {
Expand Down Expand Up @@ -100,7 +102,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {

datafileUrl != null -> {
datafileReader = DatafileReader(options.datafile?: emptyDatafile)
fetchDatafileContent(datafileUrl, handleDatafileFetch) { result ->
fetchDatafileContent(datafileUrl, handleDatafileFetch, rawResponseReady) { result ->
if (result.isSuccess) {
datafileReader = DatafileReader(result.getOrThrow())
statuses.ready = true
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.featurevisor.sdk
import com.featurevisor.types.DatafileContent
import com.featurevisor.types.InitialFeatures
import com.featurevisor.types.StickyFeatures
import okhttp3.ResponseBody

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

Expand All @@ -23,6 +24,7 @@ data class InstanceOptions(
val onError: Listener? = null,
val refreshInterval: Long? = null, // seconds
val stickyFeatures: StickyFeatures? = null,
val rawResponseReady: (ResponseBody) -> Unit = {},
) {
companion object {
private const val defaultBucketKeySeparator = "."
Expand Down

0 comments on commit 6e851dd

Please sign in to comment.