-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathAbstractAnalyticsHandler.kt
More file actions
53 lines (44 loc) · 1.72 KB
/
Copy pathAbstractAnalyticsHandler.kt
File metadata and controls
53 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dev.tomdraper.apianalytics
import com.fasterxml.jackson.databind.ObjectMapper
abstract class AbstractAnalyticsHandler<R, C>(
open val apiKey: String?,
open val client: C,
open val loggingTimeout: Long,
open val serverUrl: String,
private val frameworkName: String,
open val privacyLevel: Int,
) {
//
private val requests: MutableList<PayloadHandler.RequestData> = emptyList<PayloadHandler.RequestData>().toMutableList()
//
private var lastPosted: Long = System.currentTimeMillis()
/** Universal [ObjectMapper] for implementations to use instead of creating their own. */
protected val objectMapper = ObjectMapper()
//
abstract fun send(payload: PayloadHandler.AnalyticsPayload, endpoint: String): R
//
fun logRequest(requestData: PayloadHandler.RequestData): R? {
this.requests += requestData
return if ((System.currentTimeMillis() - this.lastPosted) > this.loggingTimeout) forceSend()
else null
}
/** Force-sends a request to the API, ignoring any usage limits.
* Usually only called during shutdown or panic, but also used in [logRequest].
* This function should be used *very* carefully. */
fun forceSend(): R? {
if (this.apiKey == null) return null
val endpoint = getServerEndpoint()
val payload = PayloadHandler.AnalyticsPayload(
apiKey!!,
requests,
frameworkName,
privacyLevel
)
println(objectMapper.writeValueAsString(payload))
return send(payload, endpoint)
}
//
private fun getServerEndpoint(): String =
if (serverUrl.endsWith("/")) "${serverUrl}api/log-request"
else "$serverUrl/api/log-request"
}