Skip to content

Commit

Permalink
Revert "Callback to receive raw response body from featurevisor data …
Browse files Browse the repository at this point in the history
…request" (#29)
  • Loading branch information
Tan108 authored Apr 5, 2024
1 parent 6e851dd commit 077655b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ 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, rawResponseReady, completion)
fetchDatafileContentFromUrl(url, completion)
}
}

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

fetch(request, rawResponseReady, completion)
fetch(request, completion)
} catch (throwable: IllegalArgumentException) {
completion(Result.failure(FeaturevisorError.InvalidUrl(url)))
}
Expand All @@ -45,15 +43,13 @@ 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: 0 additions & 1 deletion src/main/kotlin/com/featurevisor/sdk/Instance+Refresh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ private fun FeaturevisorInstance.refresh() {
fetchDatafileContent(
datafileUrl,
handleDatafileFetch,
rawResponseReady,
) { result ->

if (result.isSuccess) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -57,7 +56,6 @@ 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 @@ -102,7 +100,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {

datafileUrl != null -> {
datafileReader = DatafileReader(options.datafile?: emptyDatafile)
fetchDatafileContent(datafileUrl, handleDatafileFetch, rawResponseReady) { result ->
fetchDatafileContent(datafileUrl, handleDatafileFetch) { result ->
if (result.isSuccess) {
datafileReader = DatafileReader(result.getOrThrow())
statuses.ready = true
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 @@ -24,7 +23,6 @@ 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 077655b

Please sign in to comment.