Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ internal class NetworkClientImpl(
emptyMap<Any, Any>()
} else {
val responsePayload = read(stream)
serializer.deserialize(responsePayload)
if (responsePayload.isEmpty()) {
emptyMap<Any, Any>()
} else if (code.isSuccessHttpCode) {
try {
serializer.deserialize(responsePayload)
} catch (_: Exception) {
emptyMap<Any, Any>()
}
} else {
serializer.deserialize(responsePayload)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm afraid this can break the current logic anyhow. I'm not sure that falling back to an empty map in several cases is a good idea.

}
return RawResponse(code, response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import io.qonversion.nocodes.internal.logger.Logger
import io.qonversion.nocodes.internal.networkLayer.apiInteractor.ApiInteractor
import io.qonversion.nocodes.internal.networkLayer.requestConfigurator.RequestConfigurator
import io.qonversion.nocodes.internal.networkLayer.dto.Response
import io.qonversion.nocodes.internal.networkLayer.utils.isInternalServerErrorHttpCode
import java.util.UUID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand All @@ -33,12 +35,20 @@ internal class ScreenEventsServiceImpl(
private var cachedUserId: String? = null

override fun track(event: ScreenEvent) {
val enrichedEvent = if (event.data.containsKey("event_uid")) {
event
} else {
ScreenEvent(data = event.data.toMutableMap().apply {
put("event_uid", UUID.randomUUID().toString())
})
}

var shouldFlush = false
synchronized(lock) {
buffer.add(event)
buffer.add(enrichedEvent)
shouldFlush = buffer.size >= BATCH_SIZE
}
logger.verbose("ScreenEventsService -> tracked event: ${event.data["type"] ?: "unknown"}")
logger.verbose("ScreenEventsService -> tracked event: ${enrichedEvent.data["type"] ?: "unknown"}")
if (shouldFlush) {
flush()
}
Expand Down Expand Up @@ -118,11 +128,12 @@ internal class ScreenEventsServiceImpl(
val eventMaps = eventsToSend.map { it.toMap() }
val body = mapOf<String, Any?>("events" to eventMaps)
val request = requestConfigurator.configureScreenEventsRequest(uid, body)
logger.verbose("ScreenEventsService -> sending ${eventsToSend.size} events to ${request.url}")
val response = apiInteractor.execute(request)

if (response is Response.Error) {
logger.error("ScreenEventsService -> failed to send events: ${response.message}")
if (reBufferOnFailure) {
logger.error("ScreenEventsService -> failed to send events (code=${response.code}): ${response.message}")
if (reBufferOnFailure && response.code.isInternalServerErrorHttpCode) {
reBuffer(eventsToSend)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ internal class ScreenPresenter(
"happened_at" to currentUnixTimestamp()
))
screenEventsService.track(event)
screenEventsService.flush()
}

private fun currentUnixTimestamp(): Long =
Expand Down
Loading