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

com.deque.networking.interfaces.DashboardService.kt Maven / Gradle / Ivy

The newest version!
package com.deque.networking.interfaces

import com.deque.networking.analytics.AmplitudeEventProps
import com.deque.networking.analytics.AmplitudeEventType
import com.deque.networking.analytics.AnalyticsService
import com.deque.networking.models.devtools.TagsSet
import com.deque.networking.models.devtools.serializable.AxeDevToolsResult
import com.deque.networking.models.devtools.serializable.AxeDevToolsResultKey
import com.deque.networking.models.devtools.serializable.AxeDevToolsResultSummaryResponse
import com.deque.networking.models.devtools.serializable.HealthCheck
import com.deque.networking.models.devtools.serializable.UserInfo
import kotlinx.coroutines.runBlocking

object DashboardService {

    private var axeDevToolsClient: AxeDevToolsClient? = null
    private var serverConfig = ConnectionConfig.DEFAULT_CONNECTION_CONFIG
    @JvmStatic
    val isConnected: Boolean
        get() = ServerConnection.userAuthenticated && axeDevToolsClient != null

    @JvmStatic
    val userId: String?
        get() = ServerConnection.userId

    @JvmStatic
    val email: String?
        get() = ServerConnection.email

    @JvmStatic
    fun connect(
        username: String,
        password: String,
        config: ConnectionConfig?
    ) {
        config?.let { serverConfig = it }

        if (!isConnected) {
            axeDevToolsClient = ServerConnection.connect(
                username,
                password,
                serverConfig
            )
        }
    }

    @JvmStatic
    fun connect(
        username: String,
        password: String,
    ) {

        if (!isConnected) {
            axeDevToolsClient = ServerConnection.connect(
                username,
                password,
                serverConfig
            )
        }
    }

    @JvmStatic
    fun connect(
        apiKey: String,
        config: ConnectionConfig?
    ) {
        config?.let { serverConfig = it }

        if (!isConnected) {
            axeDevToolsClient = ServerConnection.connect(apiKey, serverConfig)
        }
    }

    @JvmStatic
    fun connect(
        apiKey: String,
    ) {

        if (!isConnected) {
            axeDevToolsClient = ServerConnection.connect(apiKey, serverConfig)
        }
    }

    @JvmStatic
    fun connectLocally(port: String) {
        if (!isConnected) {
            axeDevToolsClient = ServerConnection.connectLocally(port)
        }
    }

    @JvmStatic
    fun disconnect() {
        axeDevToolsClient = null
        ServerConnection.userAuthenticated = false
    }

    @JvmStatic
    suspend fun getResult(axeDevToolsResultKey: AxeDevToolsResultKey): Result? {
        AnalyticsService.sendEvent(AmplitudeEventType.SCAN_RETRIEVE)
        return axeDevToolsClient?.getResult(axeDevToolsResultKey)?.onFailure {
            AnalyticsService.sendEvent(
                AmplitudeEventType.SCAN_RETRIEVE_ERROR,
                AmplitudeEventProps(reason = it.message)
            )
        }
    }

    @JvmStatic
    suspend fun postResult(axeResult: com.deque.axe.android.AxeResult): Result? {
        AnalyticsService.sendEvent(AmplitudeEventType.SCAN_SEND)
        return axeDevToolsClient?.postResult(axeResult)?.onSuccess {
            val baseFrontendUrl = "https://axe-mobile.deque.com/"
            println("ADT result at: ${baseFrontendUrl}?userId=${it.userId}&packageName=${it.packageName}&resultId=${it.resultId}")
        }?.onFailure {
            AnalyticsService.sendEvent(
                AmplitudeEventType.SCAN_SEND_ERROR,
                AmplitudeEventProps(reason = it.message)
            )
        }
    }

    @JvmStatic
    suspend fun deleteResult(axeDevToolsResultKey: AxeDevToolsResultKey): Result? {
        AnalyticsService.sendEvent(AmplitudeEventType.SCAN_DELETE)
        return axeDevToolsClient?.deleteResult(axeDevToolsResultKey)?.onFailure {
            AnalyticsService.sendEvent(
                AmplitudeEventType.SCAN_DELETE_ERROR,
                AmplitudeEventProps(reason = it.message)
            )
        }
    }

    @Deprecated("This method will be removed in a future release", ReplaceWith("tag(axeDevToolsResultKey: AxeDevToolsResultKey, tags: List)"))
    @JvmStatic
    suspend fun tag(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        tags: TagsSet
    ): Result? {
        AnalyticsService.sendEvent(
            AmplitudeEventType.SCAN_TAG_DEPRECATED,
            eventProps = AmplitudeEventProps(
                tags = tags.toList(),
                reason = "Use tag(axeDevToolsResultKey: AxeDevToolsResultKey, tags: List instead."
            )
        )

        return axeDevToolsClient?.tag(axeDevToolsResultKey, tags.toSet())?.onFailure {
            AnalyticsService.sendEvent(
                AmplitudeEventType.SCAN_TAG_ERROR,
                AmplitudeEventProps(reason = it.message)
            )
        }
    }

    @JvmStatic
    suspend fun tag(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        tags: List
    ): Result? {
        AnalyticsService.sendEvent(
            AmplitudeEventType.SCAN_TAG,
            eventProps = AmplitudeEventProps(
                tags = tags.toList()
            )
        )

        return axeDevToolsClient?.tag(axeDevToolsResultKey, tags.toSet())?.onFailure {
            AnalyticsService.sendEvent(
                AmplitudeEventType.SCAN_TAG_ERROR,
                AmplitudeEventProps(reason = it.message)
            )
        }
    }

    @JvmStatic
    suspend fun setScanName(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        scanName: String
    ): Result? {
        AnalyticsService.sendEvent(
            AmplitudeEventType.SCAN_UPDATE_NAME,
            eventProps = AmplitudeEventProps(
                scan_name = scanName
            )
        )

        return axeDevToolsClient?.setScanName(axeDevToolsResultKey, scanName)?.onFailure {
            AnalyticsService.sendEvent(
                AmplitudeEventType.SCAN_UPDATE_NAME_ERROR,
                AmplitudeEventProps(
                    reason = it.message
                )
            )
        }
    }

    @JvmStatic
    suspend fun getUserInfo(): Result? {
        return axeDevToolsClient?.getUserInfo()
    }

    @JvmStatic
    suspend fun getHealthCheck(): Result? {
        return axeDevToolsClient?.getHealthCheck()
    }

    @JvmStatic
    fun getResultSync(axeDevToolsResultKey: AxeDevToolsResultKey): AxeDevToolsResult? =
        runBlocking {
            getResult(axeDevToolsResultKey)?.getOrNull()
        }

    @JvmStatic
    fun postResultSync(axeResult: com.deque.axe.android.AxeResult): AxeDevToolsResultKey? =
        runBlocking {
            postResult(axeResult)?.getOrNull()
        }

    @JvmStatic
    fun deleteResultSync(axeDevToolsResultKey: AxeDevToolsResultKey): Unit = runBlocking {
        deleteResult(axeDevToolsResultKey)?.getOrNull()
    }

    @Deprecated("This method will be removed in a future release", ReplaceWith("tagSync(axeDevToolsResultKey: AxeDevToolsResultKey, tags: List)"))
    @JvmStatic
    fun tagSync(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        tags: TagsSet,
    ): AxeDevToolsResultKey? = runBlocking {
        tag(axeDevToolsResultKey, tags)?.getOrNull()
    }


    @JvmStatic
    fun tagSync(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        tags: List,
    ): AxeDevToolsResultKey? = runBlocking {
        tag(axeDevToolsResultKey, tags)?.getOrNull()
    }

    @JvmStatic
    fun setScanNameSync(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        scanName: String
    ): AxeDevToolsResultSummaryResponse? = runBlocking {
        setScanName(axeDevToolsResultKey, scanName)?.getOrNull()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy