Skip to content

Commit

Permalink
feat: add client upload time (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 authored Oct 27, 2023
1 parent a2e09ff commit 9863b53
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions core/src/main/java/com/amplitude/core/utilities/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
import java.text.SimpleDateFormat
import java.util.Date
import java.util.TimeZone

internal class HttpClient(
private val configuration: Configuration
Expand All @@ -26,6 +29,7 @@ internal class HttpClient(
override fun close() {
try {
this.setApiKey(getApiKey())
this.setClientUploadTime(getClientUploadTime())
this.setMinIdLength(getMindIdLength())
this.setBody()
this.outputStream?.close()
Expand Down Expand Up @@ -80,6 +84,13 @@ internal class HttpClient(
return configuration.apiKey
}

internal fun getClientUploadTime(): String {
val currentTimeMillis = System.currentTimeMillis()
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
sdf.timeZone = TimeZone.getTimeZone("UTC")
return sdf.format(Date(currentTimeMillis))
}

internal fun getMindIdLength(): Int? {
return configuration.minIdLength
}
Expand All @@ -100,6 +111,7 @@ abstract class Connection(
) : Closeable {

private lateinit var apiKey: String
private lateinit var clientUploadTime: String
private lateinit var events: String
private var minIdLength: Int? = null
internal lateinit var response: Response
Expand All @@ -113,6 +125,10 @@ abstract class Connection(
this.apiKey = apiKey
}

internal fun setClientUploadTime(clientUploadTime: String) {
this.clientUploadTime = clientUploadTime
}

internal fun setMinIdLength(minIdLength: Int?) {
this.minIdLength = minIdLength
}
Expand All @@ -131,9 +147,9 @@ abstract class Connection(

private fun getBodyStr(): String {
if (minIdLength == null) {
return "{\"api_key\":\"$apiKey\",\"events\":$events}"
return "{\"api_key\":\"$apiKey\",\"client_upload_time\":\"$clientUploadTime\",\"events\":$events}"
}
return "{\"api_key\":\"$apiKey\",\"events\":$events,\"options\":{\"min_id_length\":$minIdLength}}"
return "{\"api_key\":\"$apiKey\",\"client_upload_time\":$clientUploadTime,\"events\":$events,\"options\":{\"min_id_length\":$minIdLength}}"
}
}

Expand Down

0 comments on commit 9863b53

Please sign in to comment.