Skip to content

Commit

Permalink
make buffered readers and writers
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Sep 15, 2023
1 parent d3860ec commit a4ee842
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
31 changes: 21 additions & 10 deletions posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.posthog.PostHogEvent
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.RequestBody
import okio.BufferedSink
import java.io.OutputStream
import java.util.Date

internal class PostHogApi(private val config: PostHogConfig) {
Expand All @@ -25,7 +27,6 @@ internal class PostHogApi(private val config: PostHogConfig) {

fun batch(events: List<PostHogEvent>) {
val batch = PostHogBatchEvent(config.apiKey, events)
val json = gson.toJson(batch, gsonBatchBodyType)
// """
// {
// "api_key": "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI",
Expand All @@ -41,7 +42,9 @@ internal class PostHogApi(private val config: PostHogConfig) {
// "timestamp": "2023-09-13T12:05:30.326Z"
// }
// """.trimIndent()
val request = makeRequest(json, "${config.host}/batch")
val request = makeRequest("${config.host}/batch") {
gson.toJson(batch, gsonBatchBodyType, it.bufferedWriter())
}

client.newCall(request).execute().use {
if (!it.isSuccessful) throw PostHogApiError(it.code, it.message, body = it.body)
Expand All @@ -53,35 +56,43 @@ internal class PostHogApi(private val config: PostHogConfig) {
}
}

private fun makeRequest(json: String, url: String): Request {
val body = json.toRequestBody(mediaType)
private fun makeRequest(url: String, serializer: (outputStream: OutputStream) -> Unit): Request {
val requestBody = object : RequestBody() {
override fun contentType() = mediaType

override fun writeTo(sink: BufferedSink) {
serializer(sink.outputStream())
}
}

return Request.Builder()
.url(url)
.header("User-Agent", config.userAgent)
.post(body)
.post(requestBody)
.build()
}

fun decide(properties: Map<String, Any>): Map<String, Any>? {
val map = mutableMapOf<String, Any>()
map.putAll(properties)
map["api_key"] = config.apiKey

val json = gson.toJson(map, gsonDecideBodyType)
// """
// {
// "distinct_id": "1fc77c1a-5f98-43b3-bb77-7a2dd15fd13a",
// "api_key": "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI"
// }
// """.trimIndent()
val request = makeRequest(json, "${config.host}/decide/?v=3")
val request = makeRequest("${config.host}/decide/?v=3") {
val writer = it.bufferedWriter()
gson.toJson(map, gsonDecideBodyType, writer)
writer.flush()
}

client.newCall(request).execute().use {
if (!it.isSuccessful) throw PostHogApiError(it.code, it.message, body = it.body)

it.body?.let { body ->
return gson.fromJson(body.string(), gsonDecideBodyType)
return gson.fromJson(body.charStream().buffered(), gsonDecideBodyType)
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ internal class PostHogQueue(private val config: PostHogConfig, private val stora
retryCount = 0
} catch (e: Throwable) {
config.logger?.log("Flushing failed: $e")

// TODO: when do we actually drop those events? maybe they are broken for good
// and the SDK will be stuck at them
retry = true
retryCount++
}
Expand Down

0 comments on commit a4ee842

Please sign in to comment.