All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deque.networking.usageservice.UsageService.kt Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.deque.networking.usageservice

import com.deque.networking.interfaces.DashboardService
import com.deque.networking.utils.DistinctId
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class UsageService(val config: UsageServiceConfig) {
    private var usageServiceClient: UsageServiceClient
    private val scope = MainScope()
    private var baseUrl: String = SERVICE_PROD_URL
    private var buildFlavor: String = "auth"

    init {
        baseUrl = when (config.buildEnv) {
            "QA" -> SERVICE_QA_URL
            "PROD" -> SERVICE_PROD_URL
            else -> SERVICE_DEV_URL
        }

        buildFlavor = config.buildFlavor
        usageServiceClient = UsageServiceClient(baseUrl)
    }

    internal constructor(config: UsageServiceConfig, client: UsageServiceClient) : this(config) {
        usageServiceClient = client
    }

    fun sendEvent(
        event: UsageServiceEvent
    ): Job? {
        return if (ignoreNoAuthRequestsOrNoOrgId()) {
            null
        } else {
            scope.launch(Dispatchers.IO) {
                usageServiceClient.sendEvent(event)
            }
        }
    }

    private fun ignoreNoAuthRequestsOrNoOrgId(): Boolean {
        return buildFlavor == "noauth" || orgId == null
    }

    companion object {
        internal val distinctId: String
            get() = DistinctId(DashboardService.userId, DashboardService.email).toString()

        var orgId: String? = null

        internal const val ANDROID = "Android"
        internal const val AXE_DEVTOOLS_MOBILE = "AxeDevtoolsMobile"
        internal const val EVENT_V2 = "v2"

        internal const val SERVICE_DEV_URL = "https://usage.dequelabs.com"
        internal const val SERVICE_QA_URL = "https://usage-qa.dequelabs.com"
        internal const val SERVICE_PROD_URL = "https://usage.deque.com"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy