Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ maven.install(
"org.jetbrains.dokka:dokka-cli:1.9.10",

# Library dependencies
"com.michael-bull.kotlin-result:kotlin-result-jvm:1.1.18",
"com.google.code.gson:gson:2.10.1",
"com.squareup.okhttp3:okhttp:{}".format(OKHTTP_VERSION),
"androidx.startup:startup-runtime:1.2.0",
Expand Down
1 change: 0 additions & 1 deletion examples/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ _maven_deps = [
artifact("androidx.appcompat:appcompat"),
artifact("androidx.activity:activity-compose"),
artifact("androidx.compose.material:material"),
artifact("com.michael-bull.kotlin-result:kotlin-result-jvm"),
artifact("com.google.code.gson:gson"),
artifact("com.google.flatbuffers:flatbuffers-java"),
artifact("androidx.annotation:annotation-jvm"),
Expand Down
17 changes: 7 additions & 10 deletions examples/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.testTag
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import io.bitdrift.capture.Capture.Logger
import io.bitdrift.capture.LogLevel
import io.bitdrift.capture.common.ErrorHandler
Expand All @@ -47,6 +45,7 @@ import java.io.IOException
import kotlin.system.exitProcess
import kotlin.time.DurationUnit
import kotlin.time.toDuration
import io.bitdrift.capture.CaptureResult

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -178,15 +177,13 @@ class MainActivity : ComponentActivity() {
}

fun generateTmpDeviceCode(view: View) {
Logger.createTemporaryDeviceCode(completion = { result ->
result.onSuccess {
updateDeviceCodeValue(it)
Logger.createTemporaryDeviceCode { result ->
when (result) {
is CaptureResult.Success -> updateDeviceCodeValue(result.value)
is CaptureResult.Failure -> updateDeviceCodeValue(result.error.message)
}
result.onFailure {
updateDeviceCodeValue(it.message)
}
})
}
}
}

private fun updateDeviceCodeValue(deviceCode: String) {
deviceCodeTextView.text = deviceCode
Expand Down
2 changes: 0 additions & 2 deletions platform/jvm/capture/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ bitdrift_kt_android_library(
artifact("androidx.core:core"),
artifact("com.google.guava:listenablefuture"),
artifact("com.google.flatbuffers:flatbuffers-java"),
artifact("com.michael-bull.kotlin-result:kotlin-result-jvm"),
artifact("com.squareup.okhttp3:okhttp"),
],
deps = [
Expand All @@ -44,7 +43,6 @@ bitdrift_kt_android_library(
artifact("androidx.startup:startup-runtime"),
artifact("androidx.core:core"),
artifact("com.google.guava:listenablefuture"),
artifact("com.michael-bull.kotlin-result:kotlin-result-jvm"),
artifact("com.squareup.okhttp3:okhttp"),
artifact("com.google.code.findbugs:jsr305"),
artifact("com.google.code.gson:gson"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package io.bitdrift.capture

import android.content.Context
import android.util.Log
import com.github.michaelbull.result.Err
import io.bitdrift.capture.common.IBackgroundThreadHandler
import io.bitdrift.capture.common.MainThreadHandler
import io.bitdrift.capture.events.span.Span
Expand Down Expand Up @@ -288,7 +287,7 @@ object Capture {
mainThreadHandler.run { completion(it) }
}
} ?: run {
mainThreadHandler.run { completion(Err(SdkNotStartedError)) }
mainThreadHandler.run { completion(CaptureResult.Failure(SdkNotStartedError)) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package io.bitdrift.capture

import com.github.michaelbull.result.map
import com.google.gson.annotations.SerializedName
import io.bitdrift.capture.network.okhttp.HttpApiEndpoint
import io.bitdrift.capture.network.okhttp.OkHttpApiClient
Expand All @@ -21,8 +20,16 @@ internal class DeviceCodeService(
) {
val typedRequest = DeviceCodeRequest(deviceId)

apiClient.perform<DeviceCodeRequest, DeviceCodeResponse>(HttpApiEndpoint.GetTemporaryDeviceCode, typedRequest) { result ->
completion(result.map { it.code })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so it looks like all things being equal this is the only nice method we lost (map), which doesn't sound like the end of the world to me

apiClient.perform<DeviceCodeRequest, DeviceCodeResponse>(
HttpApiEndpoint.GetTemporaryDeviceCode,
typedRequest,
) { result ->
val mappedResult =
when (result) {
is CaptureResult.Success -> CaptureResult.Success(result.value.code)
is CaptureResult.Failure -> CaptureResult.Failure(result.error)
}
completion(mappedResult)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.content.Context
import android.system.Os
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.ProcessLifecycleOwner
import com.github.michaelbull.result.Err
import io.bitdrift.capture.attributes.ClientAttributes
import io.bitdrift.capture.attributes.DeviceAttributes
import io.bitdrift.capture.attributes.NetworkAttributes
Expand Down Expand Up @@ -300,7 +299,7 @@ internal class LoggerImpl(
* `SharedPreferences`.
*/
deviceCodeService.createTemporaryDeviceCode(deviceId, completion)
} ?: completion(Err(SdkNotStartedError))
} ?: completion(CaptureResult.Failure(SdkNotStartedError))
}

private fun appExitSaveCurrentSessionId(sessionId: String? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,26 @@ package io.bitdrift.capture
/**
* A monad for modeling success or failure operations in the Capture SDK.
*/
typealias CaptureResult<V> = com.github.michaelbull.result.Result<V, Error>
sealed class CaptureResult<out V> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since refactoring this will touch all existing references, why CaptureResult instead of just Result since this is already in the capture package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i thought about it, but wanted to minimize noise to existing customer that rely on CaptureResult

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I didn't realize this class/alias was public

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should take a look at the library we used, it can be helpful as a reference implementation for things like this:

The Result type is modelled as an inline value class. This achieves zero object allocations on the happy path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying we have to do it that way but it's a neat property of the library

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup very neat indeed

/**
* Represents a successful operation result within the Capture SDK.
*
* @param V the type of value returned upon success.
* @property value the result value produced by the operation.
*/
data class Success<V>(
val value: V,
) : CaptureResult<V>()

/**
* Represents a failed operation result within the Capture SDK.
*
* @property error the [Error] describing the reason for the failure.
*/
data class Failure(
val error: Error,
) : CaptureResult<Nothing>()
}

/**
* Represents a failed operation in the Capture SDK.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
package io.bitdrift.capture.error

import android.util.Log
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import com.google.gson.annotations.SerializedName
import io.bitdrift.capture.ApiError
import io.bitdrift.capture.CaptureResult
import io.bitdrift.capture.network.okhttp.HttpApiEndpoint
import io.bitdrift.capture.network.okhttp.OkHttpApiClient
import io.bitdrift.capture.providers.FieldProvider
Expand Down Expand Up @@ -46,19 +45,29 @@ internal class ErrorReporterService(
putAll(fields)
}

apiClient.perform<ErrorReportRequest, Unit>(HttpApiEndpoint.ReportSdkError, typedRequest, allFields) { result ->
result.onSuccess {
Log.i("capture", "Successfully reported error to bitdrift service")
}
result.onFailure { error ->
when (error) {
is ApiError.ServerError -> {
Log.w("capture", "Failed to report error to bitdrift service, got ${error.statusCode} response")
}
else -> {
Log.e("capture", "Failed to report error to bitdrift service: ${error.message}")
}
apiClient.perform<ErrorReportRequest, Unit>(
HttpApiEndpoint.ReportSdkError,
typedRequest,
allFields,
) { result ->
when (result) {
is CaptureResult.Success -> {
Log.i("capture", "Successfully reported error to bitdrift service")
}

is CaptureResult.Failure ->
when (val error = result.error) {
is ApiError.ServerError -> {
Log.w(
"capture",
"Failed to report error to bitdrift service, got ${error.statusCode} response",
)
}

else -> {
Log.e("capture", "Failed to report error to bitdrift service: ${error.message}")
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
package io.bitdrift.capture.network.okhttp

import android.util.Log
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.bitdrift.capture.ApiError
Expand Down Expand Up @@ -52,19 +50,19 @@ internal class OkHttpApiClient(
try {
gson.toJson(body)
} catch (e: Exception) {
completion(Err(e.toSerializationError()))
completion(CaptureResult.Failure(e.toSerializationError()))
return
}

val url = apiBaseUrl.newBuilder().addPathSegments(endpoint.path).build()
val requestBuilder =
Request
.Builder()
.url(url)
.method("POST", jsonBody.toRequestBody(jsonContentType))
headers?.let {
requestBuilder.headers(it.toHeaders())
}
headers?.let { requestBuilder.headers(it.toHeaders()) }
requestBuilder.header("x-bitdrift-api-key", apiKey)

client.newCall(requestBuilder.build()).enqueue(
object : Callback {
override fun onResponse(
Expand All @@ -74,15 +72,14 @@ internal class OkHttpApiClient(
response.use {
if (response.isSuccessful) {
try {
val typedResponse =
gson.fromTypedJson<Rp>(response.body?.string().orEmpty())
completion(Ok(typedResponse))
val typedResponse = gson.fromTypedJson<Rp>(response.body?.string().orEmpty())
completion(CaptureResult.Success(typedResponse))
} catch (e: Exception) {
completion(Err(e.toSerializationError()))
completion(CaptureResult.Failure(e.toSerializationError()))
}
} else {
val responseBody = response.body?.string()
completion(Err(ApiError.ServerError(response.code, responseBody)))
completion(CaptureResult.Failure(ApiError.ServerError(response.code, responseBody)))
}
Log.e("bitdrift", "done")
}
Expand All @@ -92,7 +89,7 @@ internal class OkHttpApiClient(
call: Call,
e: IOException,
) {
completion(Err(e.toNetworkError()))
completion(CaptureResult.Failure(e.toNetworkError()))
}
},
)
Expand Down
Loading
Loading